C++ std::unordered_set equal_range() Function

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

Definition

In C++, std::unordered_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::unordered_set values are unique, so the range holds one value if it exists and no value if it does not. Unlike std::set, there is no sorted lower_bound / upper_bound story here: the range is just the matching bucket element, if any. Include <unordered_set> to use it.

This is a member of std::unordered_set<T>, a hash table of unique values. Find, insert, and erase are usually average constant time. Unlike std::set, values are not sorted and iteration order is unspecified. Elements are const, so you cannot change a stored value through an iterator. There is no operator[] and no at(). Prefer find() or contains() to look up a value, and call reserve() before many inserts.

Syntax

Pass the value. Read first and second from the result.

C++
auto r = s.equal_range(20);
bool found = (r.first != r.second);
if (found) {
    std::cout << *r.first;
}
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 unordered_set. Both iterators then equal end(). There is no "insertion point" like in a sorted set.

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 an unordered_set.

C++ Examples

Example 1 — equal_range() for an Existing Value

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

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

int main() {
    std::unordered_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 is one past that single match.
  2. std::distance(r.first, r.second) counts the values in between. An unordered_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 and both iterators equal end().

C++
#include <iostream>
#include <unordered_set>

int main() {
    std::unordered_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 << "equals end: " << (r.first == s.end()) << '\n';
    return 0;
}

How It Works

  1. There is no value 25, so nothing equals it and the range contains no element.
  2. On an unordered_set both iterators equal end() for a miss. There is no sorted "next bigger" position.
  3. Because first == second, the demo never dereferences them.

Example 3 — equal_range() to Detect Presence

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

C++
#include <iostream>
#include <unordered_set>

void report(const std::unordered_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::unordered_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 unordered_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.
No. Values are not sorted. Use find(), contains(), or equal_range() for membership.
For a std::unordered_set find() is simpler. equal_range() is mostly useful on std::unordered_multiset, where a value can appear several times.

Next: erase()

Keep going through the std::unordered_set functions. Remove a value, one position, or a range from the unordered_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.

11 people found this page helpful