upper_bound() is a standard library function in C++ defined in the header
ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last, const T& val);
ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last, const T& val, Compare comp);
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 upper bound to search 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 type : An iterator to the upper bound of val in the range. If all the element in the range compare less than val, the function returns last.
Input : 10 20 30 30 40 50 Output : upper_bound for element 30 is at index 4 Input : 10 20 30 40 50 Output : upper_bound for element 45 is at index 4 Input : 10 20 30 40 50 Output : upper_bound for element 60 is at index 5
Below are some C++ programs to illustrate the use of std::upper_bound :
Vector contains : 10 20 30 40 50 upper_bound for element 35 is at position : 3 upper_bound for element 45 is at position : 4
Array contains : 10 20 30 40 50 upper_bound for element 35 is at position : 3 upper_bound for element 45 is at position : 4
Time Complexity : The number of comparisons performed is logarithmic in the distance between first and last. i.e, (at most log2(last – first) + O(1) comparisons).
- std::upper_bound() returns an iterator to the upper bound of the value passed to it.
- std::upper_bound() works only with sorted sequences. i.e. vector with sorted elements or array with sorted elements.
This article is contributed by Rohit Thapliyal. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Different types of range-based for loop iterators in C++
- Learn C++ Programming Step by Step - A 20 Day Curriculum!
- Check if Array elements can be maximized upto M by adding all elements from another array
- Why do we need reference variables if we have pointers
- Similarities and Difference between Java and C++
- Macros and its types in C/C++
- How to find index of a given element in a Vector in C++
- Print all lexicographical greater permutations of a given string
- Introduction to Complex Objects and Composition
- NULL undeclared error in C/C++ and how to resolve it
- std::remove_const in C++ with Examples
- All possible values of floor(N/K) for all values of K
- Finding Median of unsorted Array in linear time using C++ STL
- Average of Cubes of first N natural numbers
- Top 10 Programming Languages for Blockchain Development