std::upper_bound (3) - Linux Manuals
std::upper_bound: std::upper_bound
NAME
std::upper_bound - std::upper_bound
Synopsis
Defined in header <algorithm>
template< class ForwardIt, class T > (until C++20)
ForwardIt upper_bound( ForwardIt first, ForwardIt last, const T& value );
template< class ForwardIt, class T > (since C++20)
constexpr ForwardIt upper_bound( ForwardIt first, ForwardIt last, const T& value ); (1)
template< class ForwardIt, class T, class Compare > (until C++20)
ForwardIt upper_bound( ForwardIt first, ForwardIt last, const T& value, Compare comp ); (2)
template< class ForwardIt, class T, class Compare > (since C++20)
constexpr ForwardIt upper_bound( ForwardIt first, ForwardIt last, const T& value, Compare comp );
Returns an iterator pointing to the first element in the range [first, last) that is greater than value, or last if no such element is found.
The range [first, last) must be partitioned with respect to the expression !(value < element) or !comp(value, element), i.e., all elements for which the expression is true must precede all elements for which the expression is false. A fully-sorted range meets this criterion.
The first version uses operator< to compare the elements, the second version uses the given comparison function comp.
Parameters
first, last - the range of elements to examine
value - value to compare the elements to
comp - While the signature does not need to have const &, the function must not modify the objects passed to it and must be able to accept all values of type (possibly const) Type1 and Type2 regardless of value_category (thus, Type1 & is not allowed
Type requirements
-
ForwardIt must meet the requirements of LegacyForwardIterator.
-
Compare must meet the requirements of BinaryPredicate. it is not required to satisfy Compare
Return value
iterator pointing to the first element that is greater than value, or last if no such element is found.
Complexity
The number of comparisons performed is logarithmic in the distance between first and last (At most log
2(last - first) + O(1) comparisons). However, for non-LegacyRandomAccessIterators, the number of iterator increments is linear.
Possible implementation
First version
Second version