C++ std::map equal_range() Function

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

Definition

In C++, std::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::map keys are unique, so the range holds one pair if the key exists and no pair if it does not. The first iterator is the same as lower_bound(key), and the second is the same as upper_bound(key). 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 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 map. Both iterators then point at the next bigger key, 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 a map.

C++ Examples

Example 1 — equal_range() for an Existing Key

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

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

int main() {
    std::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 points at the next pair, key 30.
  2. std::distance(r.first, r.second) counts the pairs in between. A 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, but it shows where the key would go.

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

int main() {
    std::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 << "next bigger key: " << r.first->first << '\n';
    return 0;
}

How It Works

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

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 <map>
#include <string>

void addBonus(std::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::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.
equal_range(k) returns the pair {lower_bound(k), upper_bound(k)}. You get both bounds in one search.
For a std::map find() is simpler. equal_range() is mostly useful on std::multimap, where a key can have several pairs.

Next: erase()

Keep going through the std::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.

11 people found this page helpful