Home C++ Unordered Sets Functions equal_range() C++ std::unordered_set equal_range() Function 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.
auto r = s.equal_range(20);
bool found = (r.first != r.second);
if (found) {
std::cout << *r.first;
} Call What 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 std::pair<iterator, iterator> equal_range(const key_type& value);
std::pair<const_iterator, const_iterator> equal_range(const key_type& value) const; Parameters Parameter Description valueThe value 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 value for an unordered_set.
Important
Do not dereference first unless first != second. For a missing value the range is empty and both iterators are end(). For a std::unordered_set, find() is usually simpler. equal_range() shines on a unordered_multiset, where one value can appear many times.
C++ Examples 1. A value that exists 2. A value that is missing 3. Test membership with the range 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.
#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;
} values in range: 1
value: 20 How It Works equal_range(20) returns two iterators. first points at 20, and second is one past that single match.std::distance(r.first, r.second) counts the values in between. An unordered_set holds the value only once, so the answer is 1.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().
#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;
} empty range: true
equals end: true How It Works There is no value 25, so nothing equals it and the range contains no element. On an unordered_set both iterators equal end() for a miss. There is no sorted "next bigger" position. 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.
#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;
} 55 is stored
60 not found How It Works The helper asks equal_range(target) for the range that matches the score. For 55 the range has one value, so first != second and the function prints that it is stored. For 60 the range is empty, so first == second and the function prints a not-found message. Frequently Asked Questions What does std::unordered_set::equal_range() return? A std::pair of two iterators. They mark the start and the end of the range of values equal to the one you give.
How many values can the range have in a std::unordered_set? At most one, because unordered_set values are unique. It has zero values if the value is missing.
How do I know if the value was found? Check r.first != r.second. If they are equal, the range is empty and the value does not exist.
Does unordered_set have lower_bound()? No. Values are not sorted. Use find(), contains(), or equal_range() for membership.
Should I use equal_range() or find() on an unordered_set? 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 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