multiset equal_range() function in C++ STL

The multiset::equal_range() is a built-in function in C++ STL which returns an iterator of pairs. The pair refers to the range that includes all the elements in the container which have a key equivalent to k. The lower bound will be the element itself and the upper bound will point to the next element after key k. If there are no elements matching key K, the range returned is of length 0 with both iterators pointing to the first element which is greater than k according to the container’s internal comparison object (key_comp). If the key exceeds the maximum element in the set container, it returns an iterator pointing to the past the last element in the multiset container.

Syntax:

multiset_name.equal_range(key) 

Parameters: The function accepts one mandatory parameter key which specifies the key whose range in the multiset container is to be returned.



Return Value: The function returns an iterator of pair.

Below program illustrates the above function.

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to demonstrate the
// multiset::equal_range() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
  
    multiset<int> s;
  
    // Inserts elements
    s.insert(1);
    s.insert(6);
    s.insert(2);
    s.insert(5);
    s.insert(3);
    s.insert(3);
    s.insert(5);
    s.insert(3);
  
    // prints the multiset elements
    cout << "The multiset elements are: ";
    for (auto it = s.begin(); it != s.end(); it++)
        cout << *it << " ";
  
    // Function returns lower bound and upper bound
    auto it = s.equal_range(3);
    cout << "\nThe lower bound of 3 is " << *it.first;
    cout << "\nThe upper bound of 3 is " << *it.second;
  
    // Function returns the last element
    it = s.equal_range(10);
    cout << "\nThe lower bound of 10 is " << *it.first;
    cout << "\nThe upper bound of 10 is " << *it.second;
  
    // Function returns the range where the
    // element greater than 0 lies
    it = s.equal_range(0);
    cout << "\nThe lower bound of 0 is " << *it.first;
    cout << "\nThe upper bound of 0 is " << *it.second;
  
    return 0;
}

chevron_right


Output:

The multiset elements are: 1 2 3 3 3 5 5 6 
The lower bound of 3 is 3
The upper bound of 3 is 5
The lower bound of 10 is 8
The upper bound of 10 is 8
The lower bound of 0 is 1
The upper bound of 0 is 1

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to demonstrate the
// multiset::equal_range() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
  
    multiset<int> s;
  
    // Inserts elements
    s.insert(1);
    s.insert(6);
    s.insert(2);
    s.insert(5);
    s.insert(3);
    s.insert(3);
    s.insert(5);
    s.insert(3);
  
    // prints the multiset elements
    cout << "The multiset elements are: ";
    for (auto it = s.begin(); it != s.end(); it++)
        cout << *it << " ";
  
    // Function returns lower bound and upper bound
    auto it = s.equal_range(3);
    cout << "\nThe lower bound of 3 is " << *it.first;
    cout << "\nThe upper bound of 3 is " << *it.second;
  
    s.erase(it.first, it.second);
  
    // prints the multiset elements after erasing the
    // range
    cout << "\nThe multiset elements are: ";
    for (auto it = s.begin(); it != s.end(); it++)
        cout << *it << " ";
  
    return 0;
}

chevron_right


Output:

The multiset elements are: 1 2 3 3 3 5 5 6 
The lower bound of 3 is 3
The upper bound of 3 is 5
The multiset elements are: 1 2 5 5 6


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.