Home C++ Sets Functions upper_bound() C++ std::set upper_bound() Function 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().
auto it = s.upper_bound(30); // first value > 30
if (it != s.end()) {
std::cout << *it;
} Call What 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 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 Parameter Description valueThe limit to compare against. Equal values are skipped.
Return Value Return Value Description iteratorThe first value that is greater than value, or end() if no stored value is greater.
Important
The result is end() when no stored value is greater than the one you ask for. Check before you read it. Also, std::prev(s.upper_bound(v)) is the biggest value less than or equal to v, but it is only valid if upper_bound(v) is not s.begin().
C++ Examples 1. Strictly greater 2. Find the next value 3. Inclusive upper limit Example 1 — upper_bound() Skips Equal Values Compare what upper_bound() returns for a value that exists and for one that does not.
#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;
} upper_bound(20): 30
upper_bound(15): 20 How It Works The set holds 10, 20, 30. upper_bound(20) wants a value strictly greater than 20. Value 20 itself is skipped, so it returns 30.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.
#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;
} after 5: 10
after 30: 40
after 40: none How It Works The loop asks for the next value after 5, 30, and 40. For 5 (not in the set) the first bigger value is 10. For 30 the next one is 40. 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.
#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 lower_bound(60) starts the range at 60.upper_bound(80) points at the first value greater than 80, which is 90. The loop stops before that, so 80 is included.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 What does std::set::upper_bound() return? An iterator to the first value that is strictly greater than the value you give, or end() if there is none.
What is the difference between lower_bound() and upper_bound()? 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.
How do I find the next value after a given value? Call s.upper_bound(value) and check that the result is not end(). It gives the smallest stored value bigger than yours.
How do I find the biggest value that is less than or equal to v? Use std::prev(s.upper_bound(v)). Make sure upper_bound(v) is not s.begin() first, otherwise there is no such value.
Is upper_bound() fast on a set? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion