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


The C++ multiset::erase function is used to delete either a single element or a range of elements from the multiset. It reduces the size of the multiset by number of elements deleted from the container.

Syntax

//Version 1 - Delete element at position
void erase (iterator position);

//Version 2 - Delete all occurrences of val
size_type erase (const value_type& val);

//Version 3 - Delete a range of elements in [first, last)
void erase (iterator first, iterator last);
//Version 1 - Delete element at position
iterator  erase (const_iterator position);

//Version 2 - Delete all occurrences of val
size_type erase (const value_type& val);

//Version 3 - Delete a range of elements in [first, last)
iterator  erase (const_iterator first, const_iterator last);

Parameters

position Iterator specifying position of the element in the multiset to be removed.
val Value to be removed from the multiset.
first Iterator specifying position of the first element of the range in the multiset. Elements in [first, last) position range will be deleted.
last Iterator specifying position of the last element of the range in the multiset. Elements in [first, last) position range will be deleted.

Return Value

For version 2: the function returns number of elements erased.
C++ 98: For other versions, the function returns nothing.
C++ 11: For other versions, the function returns an iterator pointed to the element that follows the last element removed. If the last element is removed, then the iterator will point to the multiset::end.

Time Complexity

For version 1: Constant.
For version 2: Logarithmic in container size plus linear in number of elements removed.
For version 3: Linear in the distance between first and last..

Example:

In the below example, the multiset::erase function is used to delete elements from MSet.

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

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

  //version 1: deletes element at position = 2
  it = MSet.begin();
  it++;
  MSet.erase(it);

  //version 2: deletes 40 from the multiset
  MSet.erase(40);

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

  return 0;
}

The output of the above code will be:

MSet contain: 10 20 30 30 40 40 40 50 
MSet contain: 10 30 30 50 

Example:

A range of elements can also be deleted from the multiset. Consider the below example:

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

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

  //setting the start position at 
  //third element of the multiset
  start_it = MSet.begin();
  start_it++;
  start_it++;

  //setting the stop position at end of the multiset
  stop_it = MSet.end();

  //version 3: erase a range of elements
  MSet.erase(start_it, stop_it);

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

  return 0;
}

The output of the above code will be:

MSet contain: 10 20 20 30 40 40 50 
MSet contain: 10 20 

❮ C++ - Multiset