Open In App

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:




// 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;
}

Output:
500 500 
100 100 100

Program 2:




// 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;
}

Output:
a a 
d d d

Article Tags :
C++