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 val. This means that the function returns the index of the next smallest number just greater than that number.
The elements in the range shall already be sorted or at least partitioned with respect to val.
Templates:
Syntax 1:
ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last, const T& val);
Syntax 2:
ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last, const T& val, Compare comp);
Parameters: The above methods accept the following parameters.
- first, last: The range used is [first, last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
- val: Value of the lower bound to be searched for in the range.
- comp: Binary function that accepts two arguments (the first of the type pointed by ForwardIterator, and the second, always val), and returns a value convertible to bool. The function shall not modify any of its arguments. This can either be a function pointer or a function object.
Return Value: An iterator to the lower bound of val in the range. If all the elements in the range compare less than val, the function returns last. If all the elements in the range are larger than val, the function returns a pointer to the first element.
Examples:
Input: 10 20 30 40 50 Output: lower_bound for element 35 at index 3 Input: 10 20 30 40 50 Output: lower_bound for element 45 is index 4
// CPP program to illustrate // std :: lower_bound #include <bits/stdc++.h> // Driver code int main() { // Input vector std::vector<int> v{ 10, 20, 30, 40, 50 }; // Print vector std::cout << "Vector contains :"; for (unsigned int i = 0; i < v.size(); i++) std::cout << " " << v[i]; std::cout << "\n"; std::vector<int>::iterator low1, low2; // std :: lower_bound low1 = std::lower_bound(v.begin(), v.end(), 35); low2 = std::lower_bound(v.begin(), v.end(), 55); // Printing the lower bounds std::cout << "\nlower_bound for element 35 at position : " << (low1 - v.begin()); std::cout << "\nlower_bound for element 55 at position : " << (low2 - v.begin()); return 0; } |
Vector contains : 10 20 30 40 50 lower_bound for element 35 at position : 3 lower_bound for element 55 at position : 5
This article is contributed by Sachin Bisht. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Operator Overloading '<<' and '>>' operator in a linked list class
- How to find the Entry with largest Value in a C++ Map
- Visibility Modes in C++ with Examples
- Program to implement Separate Chaining in C++ STL without the use of pointers
- Speed up Code executions with help of Pragma in C/C++
- Vector of Vectors in C++ STL with Examples
- Array of Vectors in C++ STL
- Difference between Virtual function and Pure virtual function in C++
- cin get() in C++ with Examples
- Difference between Increment and Decrement Operators
- Map of Vectors in C++ STL with Examples
- Problem in comparing Floating point numbers and how to compare them correctly?
- exit(0) vs exit(1) in C/C++ with Examples
- C++ program to print all Even and Odd numbers from 1 to N
- Reverse the content of a file and store it in another

