unordered_multiset equal_range() function in C++STL

The unordered_multiset::equal_range() is a built-in function in C++ STL which returns the range in which all the elements are equal to a given value. It returns a pair of iterators where the first is an iterator pointing to the lower bound of the range and second is an iterator pointing to the upper bound of the range. If there is no element equal to a given value in the container, then it returns a pair where both lower and upper bound points to the position after the end of the container.

Syntax:

unordered_multiset_name.equal_range(value)

Parameters: The function accepts an element val whose range is to be returned.

Return Value: It returns a pair of iterators.

Below programs illustrates the above function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// unordered_multiset::equal_range() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multiset<int> sample;
  
    // inserts element
    sample.insert(100);
    sample.insert(100);
    sample.insert(100);
    sample.insert(200);
    sample.insert(500);
    sample.insert(500);
    sample.insert(600);
  
    // iterator of pairs pointing to range
    // which includes 500 and print by iterating in range
    auto itr = sample.equal_range(500);
    for (auto it = itr.first; it != itr.second; it++) {
        cout << *it << " ";
    }
  
    cout << endl;
  
    // iterator of pairs pointing to range
    // which includes 100 and print by iterating in range
    itr = sample.equal_range(100);
    for (auto it = itr.first; it != itr.second; it++) {
        cout << *it << " ";
    }
  
    return 0;
}

chevron_right


Output:

500 500 
100 100 100

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// unordered_multiset::equal_range() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multiset<char> sample;
  
    // inserts element
    sample.insert('a');
    sample.insert('a');
    sample.insert('b');
    sample.insert('c');
    sample.insert('d');
    sample.insert('d');
    sample.insert('d');
  
    // iterator of pairs pointing to range
    // which includes 'a' and print by iterating in range
    auto itr = sample.equal_range('a');
    for (auto it = itr.first; it != itr.second; it++) {
        cout << *it << " ";
    }
  
    cout << endl;
  
    // iterator of pairs pointing to range
    // which includes 'd' and print by iterating in range
    itr = sample.equal_range('d');
    for (auto it = itr.first; it != itr.second; it++) {
        cout << *it << " ";
    }
  
    return 0;
}

chevron_right


Output:

a a 
d d d


My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.