C++ Standard Library C++ STL Library

C++ multimap - erase() Function



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

Syntax

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

//Version 2 - Delete specified key from the multimap
size_type erase (const key_type& k);

//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 specified key from the multimap
size_type erase (const key_type& k);

//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 multimap to be removed.
k Key of the element to be removed from the multimap.
first Iterator specifying position of the first element of the range in the multimap. Elements in [first, last) position range will be deleted.
last Iterator specifying position of the last element of the range in the multimap. 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 multimap::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 example below, the multimap::erase function is used to delete elements from MMap.

#include <iostream>
#include <map>
using namespace std;
 
int main (){
  multimap<string, string> MMap;
  multimap<string, string>::iterator it;

  //populating multimap
  MMap.insert(pair<string, string>("CAN", "Montreal"));
  MMap.insert(pair<string, string>("IND", "Mumbai"));
  MMap.insert(pair<string, string>("IND", "Delhi"));
  MMap.insert(pair<string, string>("USA", "New York"));
  MMap.insert(pair<string, string>("USA", "Washington"));  

  cout<<"MMap contains: \n ";
  for(it = MMap.begin(); it != MMap.end(); ++it)
     cout<<it->first<<"  "<<it->second<<"\n ";

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

  //version 2: deletes key="USA" from the multimap
  MMap.erase("USA");

  cout<<"\nMMap contains: \n ";
  for(it = MMap.begin(); it != MMap.end(); ++it)
     cout<<it->first<<"  "<<it->second<<"\n ";

  return 0;
}

The output of the above code will be:

MMap contains: 
 CAN  Montreal
 IND  Mumbai
 IND  Delhi
 USA  New York
 USA  Washington
 
MMap contains: 
 CAN  Montreal
 IND  Delhi

Example:

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

#include <iostream>
#include <map>
using namespace std;
 
int main (){
  multimap<string, string> MMap;
  multimap<string, string>::iterator start_it;
  multimap<string, string>::iterator stop_it;
  multimap<string, string>::iterator it;

  //populating multimap
  MMap.insert(pair<string, string>("CAN", "Montreal"));
  MMap.insert(pair<string, string>("IND", "Mumbai"));
  MMap.insert(pair<string, string>("IND", "Delhi"));
  MMap.insert(pair<string, string>("USA", "New York"));
  MMap.insert(pair<string, string>("USA", "Washington"));  

  cout<<"MMap contains: \n ";
  for(it = MMap.begin(); it != MMap.end(); ++it)
     cout<<it->first<<"  "<<it->second<<"\n ";

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

  //setting the stop position at end of the multimap
  stop_it = MMap.end();

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

  cout<<"\nMMap contains: \n ";
  for(it = MMap.begin(); it != MMap.end(); ++it)
     cout<<it->first<<"  "<<it->second<<"\n ";

  return 0;
}

The output of the above code will be:

MMap contains: 
 CAN  Montreal
 IND  Mumbai
 IND  Delhi
 USA  New York
 USA  Washington
 
MMap contains: 
 CAN  Montreal
 IND  Mumbai

❮ C++ <map> Library