C++ std::set lower_bound() Function

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

Definition

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

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

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

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

Parameters

ParameterDescription
valueThe limit to compare against.

Return Value

Return ValueDescription
iteratorThe first value that is not less than value, or end() if every stored value is smaller.

C++ Examples

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

Search for a value that exists and one that does not.

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

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

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

    std::cout << "lower_bound(30): " << *a << '\n';
    std::cout << "lower_bound(25): " << *b << '\n';
    return 0;
}

How It Works

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

Example 2 — lower_bound() and end()

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

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

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

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

How It Works

  1. The loop searches twice: once with 1 and once with 55.
  2. For 1, every stored value is bigger, so the first value in the set, 10, is the answer.
  3. For 55, every stored value 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 values from 20 up to but not including 50, using two lower_bound() calls.

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

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

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

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

How It Works

  1. lower_bound(20) points at 20. That is where the range starts.
  2. lower_bound(50) points at 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 value that is greater than or equal to the value you give. It is end() if all stored values are smaller.
find() returns end() unless the exact value exists. lower_bound() also works when the value is missing: it gives the next bigger value.
lower_bound(v) finds the first value >= v. upper_bound(v) finds the first value > v. They differ only when v itself exists in the set.
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 set 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::set functions. Get the largest number of values a set 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.

9 people found this page helpful