map erase() function in C++ STL


In this article we will be discussing the working, syntax and examples of map::erase() function in C++ STL.

What is a Map in C++ STL?

Maps are the associative container, which facilitates to store the elements formed by a combination on key value and mapped value in a specific order. In a map container the data is internally always sorted with the help of its associated keys. The values in map container is accessed by its unique keys.

What is map::erase()?

The map::erase( ) is a function which comes under <map&t; header file. This function is used to remove an element or the range of elements from the map container associated with it. We can also remove the element of the map by its key.

This function effectively decreases the size of the map container by the number of elements removed from the container.

Syntax

map_name.erase(iterator pos);
map_name.erase(key_type &k);
map_name.erase(iterator start, iterator end);

Parameters

This function accepts the following

parameters

  • pos − An iterator which can be considered as the position of the element which is to be removed.
  • k − This is the key value which we want to remove from the map container.
  • start, end − The iterator ‘start’ and ‘end’ is used to give the start position and the end position of the range which we are wishing to erase from the deque container.

Return value

If the deletion is successful the function returns 1 else 0.

Example

Input

map<char, int> newmap;
newmap[‘a’]
= 1;
newmap[‘b’] = 2;
newmap.erase(b);

Output

a

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   map<int, int> TP_Map;
   TP_Map.emplace(3, 50);
   TP_Map.emplace(2, 30);
   TP_Map.emplace(1, 10);
   TP_Map.emplace(4, 70);
   cout<<"TP Map is : \n";
   cout << "MAP_KEY\tMAP_ELEMENT\n";
   for (auto i = TP_Map.begin(); i!= TP_Map.end(); i++) {
      cout << i->first << "\t" << i->second << endl;
   }
   //to erase the map values
   TP_Map.erase(1);
   TP_Map.erase(2);
   cout<<"\n\nAfter erasing the element: \n";
   cout << "MAP_KEY\tMAP_ELEMENT\n";
   for (auto i = TP_Map.begin(); i!= TP_Map.end(); i++) {
      cout << i->first << "\t" << i->second << endl;
   }
   return 0;
}

Output

TP Map is:
MAP_KEY    MAP_ELEMENT
1             10
2             30
3             50
4             70
After erasing the element:
MAP_KEY    MAP_ELEMENT
3             50
4             70

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   map<int, int> TP_Map;
   TP_Map.insert({3, 50});
   TP_Map.insert({2, 30});
   TP_Map.insert({1, 10});
   TP_Map.insert({4, 70});
   cout<<"TP Map is : \n";
   cout << "MAP_KEY\tMAP_ELEMENT\n";
   for (auto i = TP_Map.begin(); i!= TP_Map.end(); i++) {
      cout << i->first << "\t" << i->second << endl;
   }
   //to erase the map values
   auto var = TP_Map.find(1);
   TP_Map.erase(var);
   auto var_1 = TP_Map.find(2);
   TP_Map.erase(var_1);
   cout<<"\n\nAfter erasing the element: \n";
   cout << "MAP_KEY\tMAP_ELEMENT\n";
   for (auto i = TP_Map.begin(); i!= TP_Map.end(); i++) {
      cout << i->first << "\t" << i->second << endl;
   }
   return 0;
}

Output

TP Map is:
MAP_KEY    MAP_ELEMENT
1             10
2             30
3             50
4             70
After erasing the element:
MAP_KEY    MAP_ELEMENT
3             50
4             70

Updated on: 15-Apr-2020

517 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements