Home C++ Maps Functions equal_range() C++ std::map equal_range() Function 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.
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 map. Both iterators then point at the next bigger key, or at end(), which is the place where the key would go.
Library declarations std::pair<iterator, iterator> equal_range(const Key& key);
std::pair<const_iterator, const_iterator> equal_range(const Key& key) const; Parameters Parameter Description keyThe key to find the range for.
Return Value Return Value Description 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.
Important
Do not dereference first unless first != second. For a missing key the range is empty and first may be end(). For a std::map, find() is usually simpler. equal_range() shines on a multimap, where one key can have many pairs.
C++ Examples 1. A key that exists 2. A key that is missing 3. Update through the range 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.
#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;
} pairs in range: 1
value: twenty How It Works equal_range(20) returns two iterators. first points at the pair with key 20, and second points at the next pair, key 30.std::distance(r.first, r.second) counts the pairs in between. A map holds the key only once, so the answer is 1.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.
#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;
} empty range: true
next bigger key: 30 How It Works There is no key 25, so nothing equals it and the range contains no pair. Both iterators point at the first key that is bigger than 25, which is 30. Because they are the same, first == second is true. 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.
#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;
} 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 What does std::map::equal_range() return? A std::pair of two iterators. They mark the start and the end of the range of pairs that have the given key.
How many pairs can the range have in a std::map? At most one, because map keys are unique. It has zero pairs if the key is missing.
How do I know if the key was found? Check r.first != r.second. If they are equal, the range is empty and the key does not exist.
What is the relation to lower_bound() and upper_bound()? equal_range(k) returns the pair {lower_bound(k), upper_bound(k)}. You get both bounds in one search.
Should I use equal_range() or find() on a map? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion