C++ std::map lower_bound() Function

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

Definition

In C++, std::map::lower_bound() returns an iterator to the first pair whose key is not less than the key you give. In plain words: the first key that is greater than or equal to it.

Because a std::map keeps its keys sorted, this is the tool for ordered searches: find the next key at or after a value, or select a range of keys. 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.lower_bound(25);    // first key >= 25
if (it != m.end()) {
    std::cout << it->first;
}
CallWhat it does
m.lower_bound(key)Returns an iterator to the first pair with a key >= key, or end() if there is none.

If key exists, lower_bound(key) points right at it. If not, it points at the next bigger key. Compare the result with end() before you read it.

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

Parameters

ParameterDescription
keyThe value to compare keys against.

Return Value

Return ValueDescription
iteratorThe first pair whose key is not less than key, or end() if every key is smaller.

C++ Examples

Example 1 — lower_bound() Finds the First Key Not Smaller

Search for a key that exists and 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"}, {40, "forty"}, {50, "fifty"}
    };

    auto a = m.lower_bound(30);
    auto b = m.lower_bound(25);

    std::cout << "lower_bound(30): " << a->first << " " << a->second << '\n';
    std::cout << "lower_bound(25): " << b->first << " " << b->second << '\n';
    return 0;
}

How It Works

  1. The map holds the keys 10, 20, 30, 40, 50 in sorted order.
  2. lower_bound(30) looks for the first key that is >= 30. Key 30 exists, so it points right at it.
  3. lower_bound(25) finds no 25. The first key >= 25 is 30, so both searches end on the same pair.

Example 2 — lower_bound() and end()

Handle the case where every key is smaller than the one you search for.

C++
#include <iostream>
#include <map>
#include <string>

int main() {
    std::map<int, std::string> m = {{10, "ten"}, {20, "twenty"}, {30, "thirty"}};

    for (int k : {1, 55}) {
        auto it = m.lower_bound(k);
        if (it == m.end()) {
            std::cout << "no key >= " << k << '\n';
        } else {
            std::cout << "first key >= " << k << " is " << it->first << '\n';
        }
    }
    return 0;
}

How It Works

  1. The loop searches twice: once with 1 and once with 55.
  2. For 1, every key is bigger, so the first key in the map, 10, is the answer.
  3. For 55, every key is smaller, so lower_bound() returns end(). The check prevents a read from end() and prints a message instead.

Example 3 — lower_bound() for a Range Query

Print all keys from 20 up to but not including 50, using two lower_bound() calls.

C++
#include <iostream>
#include <map>
#include <string>

int main() {
    std::map<int, std::string> m = {
        {10, "ten"}, {20, "twenty"}, {30, "thirty"}, {40, "forty"}, {50, "fifty"}
    };

    auto first = m.lower_bound(20);
    auto last = m.lower_bound(50);

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

How It Works

  1. lower_bound(20) points at the pair with key 20. That is where the range starts.
  2. lower_bound(50) points at key 50. Iterator ranges stop before their end, so 50 is not included.
  3. The loop walks from first until it reaches last, and prints 20 30 40. This is the usual half-open range [20, 50).

Frequently Asked Questions

An iterator to the first pair whose key is greater than or equal to the key you give. It is end() if all keys are smaller.
find() returns end() unless the exact key exists. lower_bound() also works when the key is missing: it gives the next bigger key.
lower_bound(k) finds the first key >= k. upper_bound(k) finds the first key > k. They differ only when k itself exists in the map.
Use lower_bound(low) as the start and lower_bound(high) or upper_bound(high) as the end, depending on whether high should be included.
You can, but it is slow because map iterators are not random access. The member function lower_bound() walks the tree and is much faster.

Next: max_size()

Keep going through the std::map functions. Get the largest number of pairs the map could ever hold.

max_size() 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.

11 people found this page helpful