equal_range() in general returns range that includes all elements equal to given value. For unordered_set where all keys are distinct, the returned range contains at-most one element.
Syntax
setname.equal_range(key name)
Arguments It takes the key to be searched as parameter.
Return Value It returns two iteraters—- lower and upper bound of the range that contains the key.
Example
CPP
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{
unordered_set< int > sample;
sample.insert({ 20, 30, 40 });
auto range1 = sample.equal_range(20);
if (range1.first != sample.end()) {
for (; range1.first != range1.second; ++range1.first)
cout << *range1.first << endl;
}
else
cout << "Element does not exist" ;
return 0;
}
|
Output
20
CPP
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{
unordered_set< int > sample;
sample.insert({ 20, 30, 40 });
auto range1 = sample.equal_range(60);
if (range1.first != sample.end()) {
for (; range1.first != range1.second; ++range1.first)
cout << *range1.first << endl;
}
else
cout << "Element does not exist" ;
return 0;
}
|
Output
Element does not exist
Time complexity: O(n)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
05 Jun, 2023
Like Article
Save Article