Article directory
std::lower_bound()
1) Function:
Returns an iterator over the first element not less than the given value
2) Links:
C++ official website: https://en.cppreference.com/w/cpp/algorithm/lower_bound
is defined in the header file:middle
3) Definition:
The second form of the definition is as follows, and the other forms are similar
(1)
template< class ForwardIt, class T >
ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value );
(2)
template< class ForwardIt, class T, class Compare >
ForwardIt lower_bound( ForwardIt first, ForwardIt last,
const T& value, Compare comp );
Perform a binary search in the range [first, last), find the iterator of the first element that does not satisfy element < value [or comp(element, value)) (that is, greater than or equal to)], if no such element is found, Then return last.
4) Parameters:
first, last - iterators defining the partial order ranges to check
value - the value of the compared elements
comp - a binary predicate that returns true if the first argument is less than (ie comes before the second).
5) Return value:
Iterator to the first element in the range [first, last) such that element < value [or comp(element, value)] is false, or last if no such element is found.
6) The algorithm of the function:
first edition
template<class ForwardIt, class T>
ForwardIt lower_bound(ForwardIt first, ForwardIt last, const T& value)
{
ForwardIt it;
typename std::iterator_traits<ForwardIt>::difference_type count, step;
count = std::distance(first, last);
while (count > 0)
{
it = first;
step = count / 2;
std::advance(it, step);
if (*it < value)
{
first = ++it;
count -= step + 1;
}
else
count = step;
}
return first;
}
second edition
template<class ForwardIt, class T, class Compare>
ForwardIt lower_bound(ForwardIt first, ForwardIt last, const T& value, Compare comp)
{
ForwardIt it;
typename std::iterator_traits<ForwardIt>::difference_type count, step;
count = std::distance(first, last);
while (count > 0)
{
it = first;
step = count / 2;
std::advance(it, step);
if (comp(*it, value))
{
first = ++it;
count -= step + 1;
}
else
count = step;
}
return first;
}
7) Example of function:
#include <algorithm>
#include <iostream>
#include <vector>
struct PriceInfo { double price; };
int main()
{
const std::vector<int> data = {1, 2, 4, 5, 5, 6};
for (int i = 0; i < 8; ++i)
{
// Search for first element x such that i ≤ x
auto lower = std::lower_bound(data.begin(), data.end(), i);
std::cout << i << " ≤ ";
lower != data.end()
? std::cout << *lower << " at index " << std::distance(data.begin(), lower)
: std::cout << "not found";
std::cout << '\n';
}
std::vector<PriceInfo> prices = {
{100.0}, {101.5}, {102.5}, {102.5}, {107.3}};
for (double to_find : {102.5, 110.2})
{
auto prc_info = std::lower_bound(prices.begin(), prices.end(), to_find,
[](const PriceInfo& info, double value)
{
return info.price < value;
});
prc_info != prices.end()
? std::cout << prc_info->price << " at index " << prc_info - prices.begin()
: std::cout << to_find << " not found";
std::cout << '\n';
}
}
printout:
0 ≤ 1 at index 0
1 ≤ 1 at index 0
2 ≤ 2 at index 1
3 ≤ 4 at index 2
4 ≤ 4 at index 2
5 ≤ 5 at index 3
6 ≤ 6 at index 5
7 ≤ not found
102.5 at index 2
110.2 not found
std::upper_bound()
std::lower_bound()
Similar to
the official website link: https://en.cppreference.com/w/cpp/algorithm/upper_bound
Function: Return an iterator pointing to the first element in the range [first, last), so that value < element [or comp(value , element)] is true (i.e. strictly greater than ), if no such element is found, last is returned.
definition:
template< class ForwardIt, class T >
ForwardIt upper_bound( ForwardIt first, ForwardIt last, const T& value );
或
template< class ForwardIt, class T, class Compare >
ForwardIt upper_bound( ForwardIt first, ForwardIt last,
const T& value, Compare comp );