Home C++ Sets Functions equal_range() C++ std::set equal_range() Function Definition In C++, std::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::set values are unique, so the range holds one value if it exists and no value if it does not. The first iterator is the same as lower_bound(value), and the second is the same as upper_bound(value). Include <set> to use it.
This is a member of std::set<T>, a sorted container of unique values . A set never keeps a duplicate and always holds its values in sorted order, so it can find, insert, and erase a value in logarithmic time. A set has no operator[] and no at(), because the value is its own key. Every element is const, so you cannot change a value through an iterator.
Syntax Pass the value. Read first and second from the result.
auto r = s.equal_range(20);
// r.first : first value >= 20
// r.second : first value > 20
bool found = (r.first != r.second); 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 set. Both iterators then point at the next bigger value, or at end(), which is the place where the value would go.
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 a set.
Important
Do not dereference first unless first != second. For a missing value the range is empty and first may be end(). For a std::set, find() is usually simpler. equal_range() shines on a 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 set and see that the range holds exactly one element.
#include <iostream>
#include <iterator>
#include <set>
int main() {
std::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 points at the next value, 30.std::distance(r.first, r.second) counts the values in between. A 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, but it shows where the value would go.
#include <iostream>
#include <set>
int main() {
std::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 << "next bigger: " << *r.first << '\n';
return 0;
} empty range: true
next bigger: 30 How It Works There is no value 25, so nothing equals it and the range contains no element. Both iterators point at the first value that is bigger than 25, which is 30. Because they are the same, first == second is true. Reading *r.first is only safe here because 30 exists. If the value had been bigger than every element, both iterators would equal end(). Example 3 — equal_range() to Detect Presence Use an empty-range test to decide whether a score is stored.
#include <iostream>
#include <set>
void report(const std::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::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::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::set? At most one, because 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.
What is the relation to lower_bound() and upper_bound()? equal_range(v) returns the pair {lower_bound(v), upper_bound(v)}. You get both bounds in one search.
Should I use equal_range() or find() on a set? For a std::set find() is simpler. equal_range() is mostly useful on std::multiset, where a value can appear several times.
Next: erase() Keep going through the std::set functions. Remove a value, one position, or a range from the 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.
10 people found this page helpful
Helpful Share Copy link Suggestion