Skip to main content

std::upper_bound() algorithm

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

// (2)
template< class ForwardIt, class T, class Compare >
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) such that value < element (or comp(value, element)) is true (i.e. that is strictly 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.

  • (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 (since 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 value < element (or comp(value, element)) is true, or last if no such element is found.

Complexity

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 upper_bound() functions should be preferred.

Exceptions

(none)

Possible implementation

upper_bound (1)
template<class ForwardIt, class T>
ForwardIt upper_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 (!(value < *it))
{
first = ++it;
count -= step + 1;
}
else
count = step;
}

return first;
}
upper_bound (2)
template<class ForwardIt, class T, class Compare>
ForwardIt upper_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(value, *it))
{
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 < 7; ++i)
{
// Search first element that is greater than i
auto upper = std::upper_bound(data.begin(), data.end(), i);

std::cout << i << " < ";
upper != data.end()
? std::cout << *upper << " at index " << std::distance(data.begin(), upper)
: 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::upper_bound(prices.begin(), prices.end(), to_find,
[](double value, const PriceInfo& info)
{
return value < info.price;
});

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 < 2 at index 1
2 < 4 at index 2
3 < 4 at index 2
4 < 5 at index 3
5 < 6 at index 5
6 < not found
107.3 at index 4
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::upper_bound() algorithm

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

// (2)
template< class ForwardIt, class T, class Compare >
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) such that value < element (or comp(value, element)) is true (i.e. that is strictly 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.

  • (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 (since 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 value < element (or comp(value, element)) is true, or last if no such element is found.

Complexity

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 upper_bound() functions should be preferred.

Exceptions

(none)

Possible implementation

upper_bound (1)
template<class ForwardIt, class T>
ForwardIt upper_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 (!(value < *it))
{
first = ++it;
count -= step + 1;
}
else
count = step;
}

return first;
}
upper_bound (2)
template<class ForwardIt, class T, class Compare>
ForwardIt upper_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(value, *it))
{
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 < 7; ++i)
{
// Search first element that is greater than i
auto upper = std::upper_bound(data.begin(), data.end(), i);

std::cout << i << " < ";
upper != data.end()
? std::cout << *upper << " at index " << std::distance(data.begin(), upper)
: 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::upper_bound(prices.begin(), prices.end(), to_find,
[](double value, const PriceInfo& info)
{
return value < info.price;
});

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 < 2 at index 1
2 < 4 at index 2
3 < 4 at index 2
4 < 5 at index 3
5 < 6 at index 5
6 < not found
107.3 at index 4
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.