C++ std::unordered_map equal_range() Function

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

Definition

In C++, std::unordered_map::equal_range() returns two iterators in a std::pair. Together they mark the range of pairs whose key equals the one you ask for.

In a std::unordered_map keys are unique, so the range holds one pair if the key exists and no pair if it does not. Include <unordered_map> to use it.

This is a member of std::unordered_map<Key, T>, a hash table of key and value pairs. Each key is unique. Find, insert, and erase are usually average constant time. Unlike std::map, keys are not sorted and iteration order is unspecified. 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 the key. Read first and second from the result.

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

If the range is empty (first == second), the key is not in the unordered_map. Both iterators then point at the insertion point iterator, or at end(), which is the place where the key would go.

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

Parameters

ParameterDescription
keyThe key 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 pair for an unordered_map.

C++ Examples

Example 1 — equal_range() for an Existing Key

Ask for a key that is in the unordered_map and see that the range holds exactly one pair.

C++
#include <iostream>
#include <iterator>
#include <unordered_map>
#include <string>

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

    auto r = m.equal_range(20);

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

How It Works

  1. equal_range(20) returns two iterators. first points at the pair with key 20, and second is one past that single match.
  2. std::distance(r.first, r.second) counts the pairs in between. An unordered_map holds the key only once, so the answer is 1.
  3. Since the range is not empty, r.first->second is safe to read and gives twenty.

Example 2 — equal_range() for a Missing Key

Ask for a key that is not stored. The range is empty.

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

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

    auto r = m.equal_range(25);

    std::cout << std::boolalpha;
    std::cout << "empty range: " << (r.first == r.second) << '\n';
    std::cout << "equals end: " << (r.first == m.end()) << '\n';
    std::cout << "size unchanged: " << m.size() << '\n';
    return 0;
}

How It Works

  1. There is no key 25, so nothing equals it and the range contains no pair.
  2. On an unordered_map, an empty equal_range usually has both iterators equal to end(). Never dereference them.
  3. The size stays 3. Looking up a missing key with equal_range() does not insert anything.

Example 3 — equal_range() to Find and Change

Loop over the range to update a value, and use the range test to detect a missing key.

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

void addBonus(std::unordered_map<std::string, int>& pts, const std::string& name) {
    auto r = pts.equal_range(name);
    if (r.first == r.second) {
        std::cout << name << " not found\n";
        return;
    }
    for (auto it = r.first; it != r.second; ++it) {
        it->second += 10;
        std::cout << name << " now has " << it->second << '\n';
    }
}

int main() {
    std::unordered_map<std::string, int> pts = {{"Ann", 40}, {"Ben", 55}};

    addBonus(pts, "Ben");
    addBonus(pts, "Cy");
    return 0;
}

How It Works

  1. The helper asks equal_range(name) for the range that matches the name.
  2. For "Ben" the range has one pair. The loop visits it once, adds 10 to the value, and prints 65.
  3. For "Cy" the range is empty, so first == second and the function prints a message and returns before the loop.

Frequently Asked Questions

A std::pair of two iterators. They mark the start and the end of the range of pairs that have the given key.
At most one, because map keys are unique. It has zero pairs if the key is missing.
Check r.first != r.second. If they are equal, the range is empty and the key does not exist.
Both find a key. find() returns one iterator (or end). equal_range() returns a pair of iterators that mark a half-open range of matching pairs. On unordered_map that range has 0 or 1 element.
Prefer find() for a single key. Use equal_range() when you share generic code with multimap-style containers.

Next: erase()

Keep going through the std::unordered_map functions. Remove a pair by key, by iterator, or a whole range.

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.

12 people found this page helpful