Przejdź do głównej zawartości

std::lower_bound() algorithm

// (1)
template< class ForwardIt, class T >
constexpr ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value );

// (2)
template< class ForwardIt, class T, class Compare >
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) such that element < value (or comp(element, value)) is false, (i.e. that is greater than or equal to value), or last if no such element is found.

important

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.

  • (1) Uses operator< to compare the elements.
  • (2) Uses the given comparison function comp.

Parameters

first
last

The partially-ordered range to examine.

value

The value to compare the elements to.

comp

Comparison function object (i.e. an object that satisfies the requirements of Compare). The signature of the comparison function should be equivalent to the following:

bool cmp(const Type1 &a, const Type2 &b);
  • The signature does not need to have const&, but must not modify arguments.
  • Must accept all values of type (possibly const) Type and Type2, regardless of value category (so Type1& is not allowed, nor is Type1 unless for Type1 a move is equivalent to a copy (od C++11))
  • The types Type1 and Type2 must be such that an object of type RandomIt can be implicitly converted to both of them.

Type requirements

ForwardItLegacyForwardIterator
CompareBinaryPredicate

Compare is not required to satisfy Compare.

Return value

Iterator pointing 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.

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.

Notably, std::map, std::multimap, std::set, and std::multiset iterators are not random access, and so their member lower_bound() functions should be preferred.

Exceptions

(none)

Possible implementation

lower_bound (1)
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;
}
lower_bound (2)
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;
}

Examples

Main.cpp
#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';
}
}
Output
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
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::lower_bound() algorithm

// (1)
template< class ForwardIt, class T >
constexpr ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value );

// (2)
template< class ForwardIt, class T, class Compare >
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) such that element < value (or comp(element, value)) is false, (i.e. that is greater than or equal to value), or last if no such element is found.

important

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.

  • (1) Uses operator< to compare the elements.
  • (2) Uses the given comparison function comp.

Parameters

first
last

The partially-ordered range to examine.

value

The value to compare the elements to.

comp

Comparison function object (i.e. an object that satisfies the requirements of Compare). The signature of the comparison function should be equivalent to the following:

bool cmp(const Type1 &a, const Type2 &b);
  • The signature does not need to have const&, but must not modify arguments.
  • Must accept all values of type (possibly const) Type and Type2, regardless of value category (so Type1& is not allowed, nor is Type1 unless for Type1 a move is equivalent to a copy (od C++11))
  • The types Type1 and Type2 must be such that an object of type RandomIt can be implicitly converted to both of them.

Type requirements

ForwardItLegacyForwardIterator
CompareBinaryPredicate

Compare is not required to satisfy Compare.

Return value

Iterator pointing 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.

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.

Notably, std::map, std::multimap, std::set, and std::multiset iterators are not random access, and so their member lower_bound() functions should be preferred.

Exceptions

(none)

Possible implementation

lower_bound (1)
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;
}
lower_bound (2)
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;
}

Examples

Main.cpp
#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';
}
}
Output
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
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.