std::lower_bound (3) Linux Manual Page
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
Example
// Run this code
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
template <class ForwardIt, class T, class Compare = std::less<>>
ForwardIt binary_find(ForwardIt first, ForwardIt last, const T &value, Compare comp = {})
{
// Note: BOTH type T and the type after ForwardIt is dereferenced
// must be implicitly convertible to BOTH Type1 and Type2, used in Compare.
// This is stricter than lower_bound requirement (see above)
first = std::lower_bound(first, last, value, comp);
return first != last && !comp(value, *first) ? first : last;
}
int main()
{
std::vector<int> data = {1, 1, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 6};
auto lower = std::lower_bound(data.begin(), data.end(), 4);
auto upper = std::upper_bound(data.begin(), data.end(), 4);
std::copy(lower, upper, std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
// classic binary search, returning a value only if it is present
data = {1, 2, 4, 6, 9, 10};
auto it = binary_find(data.cbegin(), data.cend(), 4); //< choosing '5' will return end()
if (it != data.cend())
std::cout << *it << " found at index " << std::distance(data.cbegin(), it);
return 0;
}
Output:
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
LWG_270 C++98 Compare was required to be a strict weak ordering only a partitioning is needed; heterogeneous comparisons permitted
See also
equal_range (function template)
partition (function template)
upper_bound (function template)
