Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

map erase() function in C++ STL

  • Difficulty Level : Easy
  • Last Updated : 19 Nov, 2020

map::erase() is a built-in function in C++ STL which is used to erase element from the container. It can be used to erase keys, elements at any specified position or a given range. 

  • Syntax for erasing a key:
map_name.erase(key)

Parameters: The function accepts one mandatory parameter key which specifies the key to be erased in the map container. 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Return Value: The function returns 1 if the key element is found in the map else returns 0. 

Below program illustrate the above syntax: 



C++




// C++ program to illustrate
// map::erase(key)
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
 
    // initialize container
    map<int, int> mp;
 
    // insert elements in random order
    mp.insert({ 2, 30 });
    mp.insert({ 1, 40 });
    mp.insert({ 3, 60 });
    mp.insert({ 5, 50 });
 
    // prints the elements
    cout << "The map before using erase() is : \n";
    cout << "KEY\tELEMENT\n";
    for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
        cout << itr->first
             << '\t' << itr->second << '\n';
    }
 
    // function to erase given keys
    mp.erase(1);
    mp.erase(2);
 
    // prints the elements
    cout << "\nThe map after applying erase() is : \n";
    cout << "KEY\tELEMENT\n";
    for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
        cout << itr->first
             << '\t' << itr->second << '\n';
    }
    return 0;
}
Output: 
The map before using erase() is : 
KEY    ELEMENT
1    40
2    30
3    60
5    50

The map after applying erase() is : 
KEY    ELEMENT
3    60
5    50



 

  • Syntax for removing a position: 
map_name.erase(iterator position)

Parameters: The function accepts one mandatory parameter position which specifies the iterator that is the reference to the position of the element to be erased. 

Return Value: The function does not return anything. 

Below program illustrate the above syntax: 

C++




// C++ program to illustrate
// map::erase(iteratorposition)
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
 
    // initialize container
    map<int, int> mp;
    // insert elements in random order
    mp.insert({ 2, 30 });
    mp.insert({ 1, 40 });
    mp.insert({ 3, 60 });
    mp.insert({ 5, 50 });
 
    // prints the elements
    cout << "The map before using erase() is : \n";
    cout << "KEY\tELEMENT\n";
    for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
        cout << itr->first
             << '\t' << itr->second << '\n';
    }
 
    // function to erase given position
    auto it = mp.find(2);
    mp.erase(it);
 
    auto it1 = mp.find(5);
    mp.erase(it1);
 
    // prints the elements
    cout << "\nThe map after applying erase() is : \n";
    cout << "KEY\tELEMENT\n";
    for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
        cout << itr->first
             << '\t' << itr->second << '\n';
    }
    return 0;
}
Output: 
The map before using erase() is : 
KEY    ELEMENT
1    40
2    30
3    60
5    50

The map after applying erase() is : 
KEY    ELEMENT
1    40
3    60



 

  • Syntax for erasing a given range: 
map_name.erase(iterator position1, iterator position2)

Parameters: The function accepts two mandatory parameters which are described below: 

  • position1 – specifies the iterator that is the reference to the element from which removal is to be done.
  • position2 – specifies the iterator that is the reference to the element upto which removal is to be done.

Return Value: The function does not returns anything. It removes all the elements in the given range of iterators.

Program below illustrates the above syntax: 

C++




// C++ program to illustrate
// map::erase(iteratorposition1, iteratorposition2)
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
 
    // initialize container
    map<int, int> mp;
    // insert elements in random order
    mp.insert({ 2, 30 });
    mp.insert({ 1, 40 });
    mp.insert({ 3, 60 });
    mp.insert({ 2, 20 });
    mp.insert({ 5, 50 });
 
    // prints the elements
    cout << "The map before using erase() is : \n";
    cout << "KEY\tELEMENT\n";
    for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
        cout << itr->first
             << '\t' << itr->second << '\n';
    }
 
    // function to erase in a given range
    // find() returns the iterator reference to
    // the position where the element is
    auto it1 = mp.find(2);
    auto it2 = mp.find(5);
    mp.erase(it1, it2);
 
    // prints the elements
    cout << "\nThe map after applying erase() is : \n";
    cout << "KEY\tELEMENT\n";
    for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
        cout << itr->first
             << '\t' << itr->second << '\n';
    }
    return 0;
}
Output: 
The map before using erase() is : 
KEY    ELEMENT
1    40
2    30
3    60
5    50

The map after applying erase() is : 
KEY    ELEMENT
1    40
5    50



 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!