Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


The C++ multiset::equal_range function is used to return the bounds of a range which includes all elements in the multiset container that are equivalent to specified value. It returns a pair, with pair::first member as the lower_bound of the range, and pair::second member as the upper_bound of the range. This contains all elements in the range [pair::first, pair::second).

If no match is found, it will return the range with zero length and both iterators will point to the first element that is considered to go after the specified value.

Syntax

pair<iterator,iterator> 
  equal_range (const value_type& val) const;
pair<const_iterator,const_iterator> 
  equal_range (const value_type& val) const;

pair<iterator,iterator> 
  equal_range (const value_type& val);

Parameters

val Specify value to compare.

Return Value

Returns a pair, with pair::first member as the lower_bound of the range, and pair::second member as the upper_bound of the range.

Time Complexity

Logarithmic i.e, Θ(log(n))

Example:

In the below example, the multiset::equal_range function is used to return the bounds of a range for specified value in MSet.

#include <iostream>
#include <set>
using namespace std;
 
int main (){
  multiset<int> MSet{10, 20, 20, 20, 30, 40, 50};
  multiset<int>::iterator it;

  cout<<"MSet contains:";
  for(it = MSet.begin(); it != MSet.end(); ++it)
    cout<<" "<<*it;

  //finding bound range of 20  
  pair<multiset<int>::iterator,
       multiset<int>::iterator> pit;
  pit = MSet.equal_range(20);

  cout<<"\n\nThe lower bound point is: "<<*pit.first<<"\n";
  cout<<"The upper bound point is: "<<*pit.second<<"\n";

  return 0;
}

The output of the above code will be:

MSet contains: 10 20 20 20 30 40 50

The lower bound point is: 20
The upper bound point is: 30 

Example:

Lets consider another example where equal_range() function is used to specify bound range to delete elements from the multiset.

#include <iostream>
#include <set>
using namespace std;
 
int main (){
  multiset<int> MSet{10, 20, 20, 20, 30, 40, 50};
  multiset<int>::iterator it;

  cout<<"MSet contains:";
  for(it = MSet.begin(); it != MSet.end(); ++it)
    cout<<" "<<*it;

  //finding bound range of 20  
  pair<multiset<int>::iterator,
       multiset<int>::iterator> pit;
  pit = MSet.equal_range(20);

  //erasing the elements from the multiset
  MSet.erase(pit.first, pit.second);

  cout<<"\nMSet contains:";
  for(it = MSet.begin(); it != MSet.end(); ++it)
    cout<<" "<<*it;

  return 0;
}

The output of the above code will be:

MSet contains: 10 20 20 20 30 40 50
MSet contains: 10 30 40 50

❮ C++ - Multiset