C++ Standard Library C++ STL Library

C++ map - erase() Function



The C++ map::erase function is used to delete either a single element or a range of elements from the map. It reduces the size of the map 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 map
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 map
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 map to be removed.
k Key of the element to be removed from the map.
first Iterator specifying position of the first element of the range in the map. Elements in [first, last) position range will be deleted.
last Iterator specifying position of the last element of the range in the map. 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 map::end.

Time Complexity

For version 1: Constant.
For version 2: Logarithmic in container size.
For version 3: Linear in the distance between first and last..

Example:

In the example below, the map::erase function is used to delete a single element from MyMap.

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

  //populating map
  MyMap[101] = "John";
  MyMap[102] = "Marry";
  MyMap[103] = "Kim";
  MyMap[104] = "Jo";
  MyMap[105] = "Ramesh";

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

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

  //version 2: deletes key=104 from the map
  MyMap.erase(104);

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

  return 0;
}

The output of the above code will be:

MyMap contains: 
 101  John
 102  Marry
 103  Kim
 104  Jo
 105  Ramesh
 
MyMap contains: 
 101  John
 103  Kim
 105  Ramesh

Example:

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

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

  //populating map
  MyMap[101] = "John";
  MyMap[102] = "Marry";
  MyMap[103] = "Kim";
  MyMap[104] = "Jo";
  MyMap[105] = "Ramesh";

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


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

  //setting the stop position at end of the map
  stop_it = MyMap.end();

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

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

  return 0;
}

The output of the above code will be:

MyMap contains: 
 101  John
 102  Marry
 103  Kim
 104  Jo
 105  Ramesh
 
MyMap contains: 
 101  John
 102  Marry

❮ C++ <map> Library