C++ std::set equal_range() Function

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

Definition

In C++, std::set::equal_range() returns two iterators in a std::pair. Together they mark the range of values equal to the one you ask for.

In a std::set values are unique, so the range holds one value if it exists and no value if it does not. The first iterator is the same as lower_bound(value), and the second is the same as upper_bound(value). 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 the value. Read first and second from the result.

C++
auto r = s.equal_range(20);
// r.first  : first value >= 20
// r.second : first value  > 20
bool found = (r.first != r.second);
CallWhat it does
s.equal_range(value)Returns std::pair<iterator, iterator>: the start and the end of the range that matches value.

If the range is empty (first == second), the value is not in the set. Both iterators then point at the next bigger value, or at end(), which is the place where the value would go.

Library declarations
C++
std::pair<iterator, iterator> equal_range(const key_type& value);
std::pair<const_iterator, const_iterator> equal_range(const key_type& value) const;

Parameters

ParameterDescription
valueThe value to find the range for.

Return Value

Return ValueDescription
pair<iterator, iterator>first is the start of the range, second is one past its end. The range has at most one value for a set.

C++ Examples

Example 1 — equal_range() for an Existing Value

Ask for a value that is in the set and see that the range holds exactly one element.

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

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

    auto r = s.equal_range(20);

    std::cout << "values in range: " << std::distance(r.first, r.second) << '\n';
    std::cout << "value: " << *r.first << '\n';
    return 0;
}

How It Works

  1. equal_range(20) returns two iterators. first points at 20, and second points at the next value, 30.
  2. std::distance(r.first, r.second) counts the values in between. A set holds the value only once, so the answer is 1.
  3. Since the range is not empty, *r.first is safe to read and gives 20.

Example 2 — equal_range() for a Missing Value

Ask for a value that is not stored. The range is empty, but it shows where the value would go.

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

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

    auto r = s.equal_range(25);

    std::cout << std::boolalpha;
    std::cout << "empty range: " << (r.first == r.second) << '\n';
    std::cout << "next bigger: " << *r.first << '\n';
    return 0;
}

How It Works

  1. There is no value 25, so nothing equals it and the range contains no element.
  2. Both iterators point at the first value that is bigger than 25, which is 30. Because they are the same, first == second is true.
  3. Reading *r.first is only safe here because 30 exists. If the value had been bigger than every element, both iterators would equal end().

Example 3 — equal_range() to Detect Presence

Use an empty-range test to decide whether a score is stored.

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

void report(const std::set<int>& scores, int target) {
    auto r = scores.equal_range(target);
    if (r.first == r.second) {
        std::cout << target << " not found\n";
        return;
    }
    std::cout << target << " is stored\n";
}

int main() {
    std::set<int> scores = {40, 55, 70};

    report(scores, 55);
    report(scores, 60);
    return 0;
}

How It Works

  1. The helper asks equal_range(target) for the range that matches the score.
  2. For 55 the range has one value, so first != second and the function prints that it is stored.
  3. For 60 the range is empty, so first == second and the function prints a not-found message.

Frequently Asked Questions

A std::pair of two iterators. They mark the start and the end of the range of values equal to the one you give.
At most one, because set values are unique. It has zero values if the value is missing.
Check r.first != r.second. If they are equal, the range is empty and the value does not exist.
equal_range(v) returns the pair {lower_bound(v), upper_bound(v)}. You get both bounds in one search.
For a std::set find() is simpler. equal_range() is mostly useful on std::multiset, where a value can appear several times.

Next: erase()

Keep going through the std::set functions. Remove a value, one position, or a range from the set.

erase() 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