unordered_multimap equal_range() function in C++ STL

unordered_multimap::equal_range() is a built-in function in C++ STL which returns the range in which all the element’s key is equal to a key. 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 past the end of the container or unordered_multimap.end().

Syntax:

unordered_multimap_name.equal_range(k)

Parameters: The function accepts a mandatory parameter k. The range returned will have the elements with key k.

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_multimap::equal_range()
#include <iostream>
#include <unordered_map>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap<int, int> sample;
  
    // inserts key and element
    sample.insert({ 1, 2 });
    sample.insert({ 1, 2 });
    sample.insert({ 2, 3 });
    sample.insert({ 3, 4 });
    sample.insert({ 2, 6 });
  
    // iterator of pairs pointing to range
    // which includes 1 and print by iterating in range
    auto itr = sample.equal_range(1);
    cout << "Elements with Key 1: ";
    for (auto it = itr.first; it != itr.second; it++) {
        cout << it->second << " ";
    }
  
    cout << endl;
  
    // iterator of pairs pointing to range
    // which includes 2 and print by iterating in range
    itr = sample.equal_range(2);
    cout << "Elements with Key 2: ";
    for (auto it = itr.first; it != itr.second; it++) {
        cout << it->second << " ";
    }
  
    return 0;
}

chevron_right


Output:

Elements with Key 1: 2 2 
Elements with Key 2: 6 3

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// unordered_multimap::equal_range()
#include <iostream>
#include <unordered_map>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap<char, char> sample;
  
    // inserts key and element
    sample.insert({ 'a', 'b' });
    sample.insert({ 'a', 'b' });
    sample.insert({ 'a', 'd' });
    sample.insert({ 'b', 'e' });
    sample.insert({ 'b', 'd' });
  
    // iterator of pairs pointing to range
    // which includes b and print by iterating in range
    auto itr = sample.equal_range('b');
    cout << "Elements with Key b: ";
    for (auto it = itr.first; it != itr.second; it++) {
        cout << it->second << " ";
    }
  
    cout << endl;
  
    // iterator of pairs pointing to range
    // which includes a and print by iterating in range
    itr = sample.equal_range('a');
    cout << "Elements with Key a: ";
    for (auto it = itr.first; it != itr.second; it++) {
        cout << it->second << " ";
    }
  
    return 0;
}

chevron_right


Output:

Elements with Key b: d e 
Elements with Key a: d b b


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.