std::lower_bound (3) - Linux Manuals
std::lower_bound: std::lower_bound
NAME
std::lower_bound - std::lower_bound
Synopsis
Defined in header <algorithm>
template< class ForwardIt, class T > (until C++20)
ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value );
template< class ForwardIt, class T > (since C++20)
constexpr ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value ); (1)
template< class ForwardIt, class T, class Compare > (until C++20)
ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value, Compare comp ); (2)
template< class ForwardIt, class T, class Compare > (since C++20)
constexpr ForwardIt lower_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 not less than (i.e. greater or equal to) value, or last if no such element is found.
The range [first, last) must be partitioned with respect to the expression element < value or comp(element, value), 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 - iterators defining the partially-ordered range 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 not less 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