C++ Standard Library C++ STL Library

C++ unordered_multimap - erase() Function



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

Syntax

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

//Version 2 - Delete specified key from the unordered_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 unordered_multimap to be removed.
k Key of the element to be removed from the unordered_multimap.
first Iterator specifying position of the first element of the range in the unordered_multimap. Elements in [first, last) position range will be deleted.
last Iterator specifying position of the last element of the range in the unordered_multimap. Elements in [first, last) position range will be deleted.

Return Value

For version 2: the function returns number of elements erased. 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 unordered_multimap::end.

Time Complexity

Average case: Linear in the number of elements removed for version 1 and version 2.
Worst case: Linear in the container size..

Example:

In the example below, the unordered_multimap::erase function is used to delete elements from uMMap.

#include <iostream>
#include <unordered_map>
using namespace std;
 
int main (){
  unordered_multimap<string, string> uMMap;
  unordered_multimap<string, string>::iterator it;

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

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

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

  //version 2: deletes key="IND" from the unordered_multimap
  uMMap.erase("IND");

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

  return 0;
}

The output of the above code will be:

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

Example:

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

#include <iostream>
#include <unordered_map>
using namespace std;
 
int main (){
  unordered_multimap<string, string> uMMap;
  unordered_multimap<string, string>::iterator start_it;
  unordered_multimap<string, string>::iterator stop_it;
  unordered_multimap<string, string>::iterator it;

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

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

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

  //setting the stop position at end of the unordered_multimap
  stop_it = uMMap.end();

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

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

  return 0;
}

The output of the above code will be:

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

❮ C++ <unordered_map> Library