C++ std::set upper_bound() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::set

Definition

In C++, std::set::upper_bound() returns an iterator to the first value that is strictly greater than the value you give. Equal values are skipped.

Use it to find the next value after a limit, or to end a range that should include that limit. It takes logarithmic time. Include <set> to use it.

This is a member of std::set<T>, a sorted container of unique values. A set never keeps a duplicate and always holds its values in sorted order, so it can find, insert, and erase a value in logarithmic time. A set has no operator[] and no at(), because the value is its own key. Every element is const, so you cannot change a value through an iterator.

Syntax

Pass a value. The result is an iterator that may be end().

C++
auto it = s.upper_bound(30);    // first value > 30
if (it != s.end()) {
    std::cout << *it;
}
CallWhat it does
s.upper_bound(value)Returns an iterator to the first value > value, or end() if there is none.

If value exists, upper_bound(value) points to the element after it. If value does not exist, it gives the same answer as lower_bound(value).

Library declarations
C++
iterator upper_bound(const key_type& value);
const_iterator upper_bound(const key_type& value) const;
template<class K> iterator upper_bound(const K& x);   // transparent comparator

Parameters

ParameterDescription
valueThe limit to compare against. Equal values are skipped.

Return Value

Return ValueDescription
iteratorThe first value that is greater than value, or end() if no stored value is greater.

C++ Examples

Example 1 — upper_bound() Skips Equal Values

Compare what upper_bound() returns for a value that exists and for one that does not.

C++
#include <iostream>
#include <set>

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

    auto a = s.upper_bound(20);
    auto b = s.upper_bound(15);

    std::cout << "upper_bound(20): " << *a << '\n';
    std::cout << "upper_bound(15): " << *b << '\n';
    return 0;
}

How It Works

  1. The set holds 10, 20, 30.
  2. upper_bound(20) wants a value strictly greater than 20. Value 20 itself is skipped, so it returns 30.
  3. upper_bound(15) finds no 15. The first value greater than 15 is 20, which is what a missing value would also give with lower_bound().

Example 2 — upper_bound() to Find the Next Value

For several queries, print the value that comes right after each one, or a message when there is none.

C++
#include <iostream>
#include <set>

int main() {
    std::set<int> s = {10, 20, 30, 40};

    for (int v : {5, 30, 40}) {
        auto it = s.upper_bound(v);
        if (it == s.end()) {
            std::cout << "after " << v << ": none\n";
        } else {
            std::cout << "after " << v << ": " << *it << '\n';
        }
    }
    return 0;
}

How It Works

  1. The loop asks for the next value after 5, 30, and 40.
  2. For 5 (not in the set) the first bigger value is 10. For 30 the next one is 40.
  3. For 40 there is no bigger value, so upper_bound() returns end(). The check prints none instead of reading an invalid iterator.

Example 3 — upper_bound() for an Inclusive Range

Print every score from 60 up to and including 80.

C++
#include <iostream>
#include <set>

int main() {
    std::set<int> scores = {50, 60, 70, 80, 90};

    auto first = scores.lower_bound(60);
    auto last = scores.upper_bound(80);   // stop after 80

    for (auto it = first; it != last; ++it) {
        std::cout << *it << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. lower_bound(60) starts the range at 60.
  2. upper_bound(80) points at the first value greater than 80, which is 90. The loop stops before that, so 80 is included.
  3. Together they form the closed range of values from 60 to 80. Using lower_bound(80) as the end would have skipped 80.

Frequently Asked Questions

An iterator to the first value that is strictly greater than the value you give, or end() if there is none.
lower_bound(v) returns the first value >= v. upper_bound(v) returns the first value > v. They give different results only when v exists in the set.
Call s.upper_bound(value) and check that the result is not end(). It gives the smallest stored value bigger than yours.
Use std::prev(s.upper_bound(v)). Make sure upper_bound(v) is not s.begin() first, otherwise there is no such value.
Yes. It takes logarithmic time, because the set is a balanced tree.

Next: value_comp()

Keep going through the std::set functions. Get the function object that compares two set elements.

value_comp() tutorial →

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.

7 people found this page helpful