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);
Call
What 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.
There is no key 25, so nothing equals it and the range contains no pair.
On an unordered_map, an empty equal_range usually has both iterators equal to end(). Never dereference them.
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;
}
Output
Ben now has 65
Cy not found
How It Works
The helper asks equal_range(name) for the range that matches the name.
For "Ben" the range has one pair. The loop visits it once, adds 10 to the value, and prints 65.
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.