In C++, std::map::upper_bound() returns an iterator to the first pair whose key is strictly greater than the key you give. Equal keys are skipped.
Use it to find the next key after a value, or to look up in a table of ranges, such as grade boundaries. It takes logarithmic time. Include <map> to use it.
This is a member of std::map<Key, T>, a sorted container of key and value pairs. Each key is unique and the pairs are always kept in key order, so a map can find, insert, and erase a key in logarithmic time. Read a value with at() or find(). The m[key] operator also works, but it adds a default value when the key is missing.
Syntax
Pass a key. The result is an iterator that may be end().
C++
auto it = m.upper_bound(30); // first key > 30
if (it != m.end()) {
std::cout << it->first;
}
Call
What it does
m.upper_bound(key)
Returns an iterator to the first pair with a key > key, or end() if there is none.
If key exists, upper_bound(key) points to the pair after it. If key does not exist, it gives the same answer as lower_bound(key).
The value to compare keys against. Keys equal to it are skipped.
Return Value
Return Value
Description
iterator
The first pair whose key is greater than key, or end() if no key is greater.
C++ Examples
Example 1 — upper_bound() Skips Equal Keys
Compare what upper_bound() returns for a key that exists and for one that does not.
C++
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<int, std::string> m = {{10, "ten"}, {20, "twenty"}, {30, "thirty"}};
auto a = m.upper_bound(20);
auto b = m.upper_bound(15);
std::cout << "upper_bound(20): " << a->first << '\n';
std::cout << "upper_bound(15): " << b->first << '\n';
return 0;
}
Output
upper_bound(20): 30
upper_bound(15): 20
How It Works
The map holds the keys 10, 20, 30.
upper_bound(20) wants a key strictly greater than 20. Key 20 itself is skipped, so it returns the pair for 30.
upper_bound(15) finds no 15. The first key greater than 15 is 20, which is what a missing key would also give with lower_bound().
Example 2 — upper_bound() to Find the Next Key
For several queries, print the key that comes right after each one, or a message when there is none.
C++
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<int, std::string> m = {{10, "a"}, {20, "b"}, {30, "c"}, {40, "d"}};
for (int k : {5, 30, 40}) {
auto it = m.upper_bound(k);
if (it == m.end()) {
std::cout << "after " << k << ": none\n";
} else {
std::cout << "after " << k << ": " << it->first << '\n';
}
}
return 0;
}
Output
after 5: 10
after 30: 40
after 40: none
How It Works
The loop asks for the next key after 5, 30, and 40.
For 5 (not in the map) the first bigger key is 10. For 30 the next one is 40.
For 40 there is no bigger key, so upper_bound() returns end(). The check prints none instead of reading an invalid iterator.
Example 3 — upper_bound() for Ranges Like Grades
Store the lowest score of each grade as a key. upper_bound() then finds the grade for any score.
C++
#include <iostream>
#include <iterator>
#include <map>
char gradeFor(const std::map<int, char>& table, int score) {
auto it = table.upper_bound(score); // first boundary above the score
return std::prev(it)->second; // the boundary just below or equal
}
int main() {
std::map<int, char> table = {{0, 'F'}, {60, 'D'}, {70, 'C'}, {80, 'B'}, {90, 'A'}};
for (int score : {55, 70, 85, 100}) {
std::cout << score << " -> " << gradeFor(table, score) << '\n';
}
return 0;
}
Output
55 -> F
70 -> C
85 -> B
100 -> A
How It Works
Each key is the lowest score of a grade. A score belongs to the biggest key that is less than or equal to it.
upper_bound(score) finds the first boundary strictly above the score. std::prev() steps back one pair to the boundary at or below it. For 70, upper_bound gives 80 and the step back gives 70, grade C.
The key 0 guarantees that upper_bound() is never begin(), so the step back is always safe. For 100 the iterator is end(), and the step back gives 90, grade A.
Frequently Asked Questions
An iterator to the first pair whose key is strictly greater than the key you give, or end() if there is none.
lower_bound(k) returns the first key >= k. upper_bound(k) returns the first key > k. They give different results only when k exists in the map.
Call m.upper_bound(key) and check that the result is not end(). It gives the smallest key bigger than yours.
Use std::prev(m.upper_bound(k)). Make sure upper_bound(k) is not m.begin() first, otherwise there is no such key.
Yes. It takes logarithmic time, because the map is a balanced tree.