Difference between std::set::lower_bound and std::lower_bound in C++
Last Updated :
07 May, 2020
Prerequisite: Random-access Iterators in C++,
Bidirectional Iterators in C++.
std::lower_bound in C++:
The
lower_bound() method in
C++ is used to return an iterator pointing to the first element in the range
[first, last) which has a value not less than the given value. This means that the function returns the index of the next smallest number just greater than that number.
std::set::lower_bound in C++:
The
set::lower_bound() is a built-in function in
C++ STL which returns an iterator pointing to the element in the container which is equivalent to
K passed in the parameter. In case K is not present in the
set container, the function returns an iterator pointing to the immediate next element which is just greater than
K. If the key passed in the parameter exceeds the maximum value in the container, then the iterator returned points to the last element in the set container.
Below is the difference between
std::lower_bound and
std::set::lower_bound:
S.No. |
std::lower_bound() |
std::set::lower_bound() |
1 |
Syntax: std::lower_bound(ForwardIterator first, ForwardIterator last, const T& val). |
Syntax: std::lower_bound(const value_type& val). |
2 |
The std::lower_bound has Random Access Iterators and Non Random Access Iterators. |
The std::set::lower_bound has Bidirectional Iterators. |
3 |
This function optimizes the number of comparisons which is efficient for Random Access Iterators. |
This function optimises the number of comparisons using Bidirectional Iterators |
4 |
The running time complexity is O(log2N) for random-access iterators but for non random-access iterators it is O(N). |
The running time complexity is always O(log2N) due to it's Binary Search Tree implementation. |
Below is the program with
CPU execution time which will illustrate the running time of both functions.
Program to illustrate of
std::lower_bound():
CPP
// C++ program to illustrate
// std::set::lower_bound
#include <bits/stdc++.h>
#include <sys/time.h>
using namespace std;
// Function whose time is to
// be measured
void fun()
{
// Initialise the set
set<int> s;
// Insert element in the set
for (int i = 0; i < 10; i++) {
s.insert(i);
}
// Use lower_bound() function
// to find 5
set<int>::iterator it;
it = lower_bound(s.begin(), s.end(), 5);
}
// Driver Code
int main()
{
// Use function gettimeofday()
// can get the time
struct timeval start, end;
// Start timer
gettimeofday(&start, NULL);
// unsync the I/O of C and C++.
ios_base::sync_with_stdio(false);
// Function Call
fun();
// Stop timer
gettimeofday(&end, NULL);
// Calculating total time taken
// by the program.
double time_taken;
time_taken = (end.tv_sec
- start.tv_sec)
* 1e6;
time_taken = (time_taken
+ (end.tv_usec
- start.tv_usec))
* 1e-6;
cout << "Time taken by program is : "
<< fixed
<< time_taken << setprecision(6);
cout << " sec" << endl;
return 0;
}
Output:
Time taken by program is : 0.000046 sec
Program to illustrate of
std::set::lower_bound():
CPP
// C++ program to illustrate
// std::lower_bound
#include <bits/stdc++.h>
#include <sys/time.h>
using namespace std;
// Function whose time is to
// be measured
void fun()
{
// Initialise the set
set<int> s;
// Insert element in the set
for (int i = 0; i < 10; i++) {
s.insert(i);
}
// Use set::lower_bound() function
// to find 5
set<int>::iterator it;
it = s.lower_bound(5);
}
// Driver Code
int main()
{
// Use function gettimeofday()
// can get the time
struct timeval start, end;
// Start timer
gettimeofday(&start, NULL);
// unsync the I/O of C and C++.
ios_base::sync_with_stdio(false);
fun();
// Stop timer
gettimeofday(&end, NULL);
// Calculating total time taken
// by the program.
double time_taken;
time_taken = (end.tv_sec
- start.tv_sec)
* 1e6;
time_taken = (time_taken
+ (end.tv_usec
- start.tv_usec))
* 1e-6;
cout << "Time taken by program is : "
<< fixed
<< time_taken << setprecision(6);
cout << " sec" << endl;
return 0;
}
Output:
Time taken by program is : 0.000039 sec
Similar Reads
Difference between std::set::upper_bound and std::upper_bound in C++ Prerequisites: Random-access Iterators, Bidirectional Iterators Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and
4 min read
Difference between std::set and std::list Set: Set is a type of associative container which stores elements in a sorted manner. All the elements of a set are unique and can not be modified but can be removed or inserted. It is a template of Standard Template Library or STL in C++. Syntax: set <data_type> sBelow is the program to illus
3 min read
Difference Between STL and Standard Library in C++ In C++, the term "Standard Library" and "Standard Template Library" are often misinterpreted as the same. Although they sound same with only a single word difference, they refer to the different part of the C++ programming language. In this article, we will learn what's the difference between the C+
3 min read
std::upper_bound and std::lower_bound for Vector in C++ STL The std::upper_bound() and std::lower_bound() functions are used for binary search operations STL containers that provide random access. They both are defined inside <algorithm> header file. In this article, we will learn how to use the std::upper_bound and std::lower_bound for vector in C++ S
5 min read
Binary Search functions in C++ STL (binary_search, lower_bound and upper_bound) In C++, STL provide various functions like std::binary_search(), std::lower_bound(), and std::upper_bound() which uses the the binary search algorithm for different purposes. These function will only work on the sorted data.There are the 3 binary search function in C++ STL:Table of Contentbinary_sea
3 min read