unordered_set equal_range in C++ STL

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// unordered_set::equal_range function
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{
    // declaration
    unordered_set<int> sample;
  
    // Insert some values
    sample.insert({ 20, 30, 40 });
  
    // Test the equal_range function for
    // a given key if it does exists
    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;
}

chevron_right


Output

20
filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// unordered_set::equal_range function
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{
  
    // declaration
    unordered_set<int> sample;
  
    // Insert some values
    sample.insert({ 20, 30, 40 });
  
    // Test the equal_range function 
    // for a given key if it does not exist
    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;
}

chevron_right


Output

Element does not exist


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.