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:
|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():
Time taken by program is : 0.000046 sec
Program to illustrate of std::set::lower_bound():
Time taken by program is : 0.000039 sec
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Difference between C and C++
- Difference between Python and C++
- Difference between Abstraction and Encapsulation in C++
- Difference between Inheritance and Polymorphism
- Difference between Private and Protected in C++ with Example
- Difference between for and while loop in C, C++, Java
- Difference between for and do-while loop in C, C++, Java
- Difference Between Constructor and Destructor in C++
- Similarities and Difference between Java and C++
- Difference between Inline and Macro in C++
- Difference between namespace and class
- Difference between strncmp() and strcmp in C/C++
- Difference between Definition and Declaration
- Difference between std::quick_exit and std::abort
- Difference between while and do-while loop in C, C++, Java
- Difference between set, multiset, unordered_set, unordered_multiset
- Sort an array according to absolute difference with given value
- Type difference of character literals in C and C++
- Difference between strlen() and sizeof() for string in C
- Difference between Argument and Parameter in C/C++ with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.