map::erase() is a built-in function in C++ STL that is used to erase elements from the container. It can be used to erase keys and elements at any specified position or a given range.
- The 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.
Return Value: The function returns 1 if the key element is found in the map else returns 0.
The below program illustrate the above syntax:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
map< int , int > mp;
mp.insert({ 2, 30 });
mp.insert({ 1, 40 });
mp.insert({ 3, 60 });
mp.insert({ 5, 50 });
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' ;
}
mp.erase(1);
mp.erase(2);
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
Time Complexity: O(log N)
- The 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.
The below program illustrate the above syntax:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
map< int , int > mp;
mp.insert({ 2, 30 });
mp.insert({ 1, 40 });
mp.insert({ 3, 60 });
mp.insert({ 5, 50 });
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' ;
}
auto it = mp.find(2);
mp.erase(it);
auto it1 = mp.find(5);
mp.erase(it1);
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
Time Complexity: O(N) or Amortized constant
- The 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 up to which removal is to be done.
Return Value: The function does not return anything. It removes all the elements in the given range of iterators.
The program below illustrates the above syntax:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
map< int , int > mp;
mp.insert({ 2, 30 });
mp.insert({ 1, 40 });
mp.insert({ 3, 60 });
mp.insert({ 2, 20 });
mp.insert({ 5, 50 });
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' ;
}
auto it1 = mp.find(2);
auto it2 = mp.find(5);
mp.erase(it1, it2);
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
Time Complexity: O(last – first)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
29 Sep, 2023
Like Article
Save Article