C++ std::map upper_bound() Function

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

Definition

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;
}
CallWhat 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).

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

Parameters

ParameterDescription
keyThe value to compare keys against. Keys equal to it are skipped.

Return Value

Return ValueDescription
iteratorThe 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;
}

How It Works

  1. The map holds the keys 10, 20, 30.
  2. upper_bound(20) wants a key strictly greater than 20. Key 20 itself is skipped, so it returns the pair for 30.
  3. 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;
}

How It Works

  1. The loop asks for the next key after 5, 30, and 40.
  2. For 5 (not in the map) the first bigger key is 10. For 30 the next one is 40.
  3. 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;
}

How It Works

  1. Each key is the lowest score of a grade. A score belongs to the biggest key that is less than or equal to it.
  2. 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.
  3. 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.

Next: value_comp()

Keep going through the std::map functions. Get the function object that compares two whole pairs by key.

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.

10 people found this page helpful