C++ std::upper_bound() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

Definition

In C++, std::upper_bound() does a binary search in a sorted range and returns an iterator to the first element that is strictly greater than the value.

Together with lower_bound(), it lets you count how many times a value appears or find the end of a group of equal values.

Syntax

Add #include <algorithm>, then call std::upper_bound(). Start with these two lines.

C++
std::upper_bound(v.begin(), v.end(), 20);
std::upper_bound(v.begin(), v.end(), 70, std::greater<int>());
CallWhat it does
std::upper_bound(v.begin(), v.end(), 20);First element greater than 20.
std::upper_bound(v.begin(), v.end(), 70, std::greater<int>());Search a descending range.
Library declarations
C++
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);

Parameters

ParameterDescription
first, lastRequired. A sorted range.
valueRequired. The value to compare with.
compOptional. The same comparison that was used to sort the range.

Return Value

Return ValueDescription
IteratorPoints at the first element greater than value.
lastReturned when no element is greater.

C++ Examples

Example 1 — First greater value

Find the first number above 20.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {10, 20, 20, 30};

    auto it = std::upper_bound(v.begin(), v.end(), 20);

    std::cout << "First value above 20 is " << *it << " at index " << (it - v.begin()) << '\n';
    return 0;
}

How It Works

  1. Both 20s are skipped, because they are not greater than 20.
  2. 30 is the first greater value, at index 3.
  3. Check against v.end() first if there might be no greater value.

Example 2 — Count duplicates

Use both bounds to count equal values.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {1, 2, 2, 2, 3};

    auto lo = std::lower_bound(v.begin(), v.end(), 2);
    auto hi = std::upper_bound(v.begin(), v.end(), 2);

    std::cout << "2 appears " << (hi - lo) << " times\n";
    return 0;
}

How It Works

  1. lower_bound() points at the first 2, at index 1.
  2. upper_bound() points just after the last 2, at index 4.
  3. The distance between them, 3, is the number of 2s.

Example 3 — Descending order

Use std::greater for data sorted from large to small.

C++
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {90, 70, 70, 50};

    auto it = std::upper_bound(v.begin(), v.end(), 70, std::greater<int>());

    std::cout << "First value below 70 is " << *it << " at index " << (it - v.begin()) << '\n';
    return 0;
}

How It Works

  1. The data is sorted from large to small, so we must pass the same std::greater<int>().
  2. In that order, "greater" means "comes later", so the search skips both 70s.
  3. The first value after them is 50.

Frequently Asked Questions

In C++, std::upper_bound() finds the first element that is greater than a value in a sorted range. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns an iterator to the first element that is greater than the value, or the last iterator if there is none.
Subtract lower_bound() from upper_bound(), or call std::equal_range() and subtract its two iterators.
The search must use the same order as the sort. Otherwise the binary search goes the wrong way.

Browse every algorithm

Open the function index to pick your next C++ algorithm.

Function index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful