Different ways to delete elements in std::map (erase() and clear())
Last Updated :
23 Dec, 2023
This article deals with the deletion part of Maps. We can delete elements in std::map using two functions
1. Using erase()
The erase() is used to erase the pair in the map mentioned in the argument, either its position, its value, or a range of numbers. We can use the erase function in the following ways:
A. Remove using the Key
erase (key)
Erases the key-value pair using the key mentioned in its argument. reorders the map after deletion. It returns the number of entries deleted. If non-existing keys is deleted, 0 is returned.
- Time complexity: log(n) (n is size of the map)
B. Remove using Iterator
erase (iter)
Erases the pair at the position pointed by the iterator mentioned in its argument.
C. Remove Elements in a Range
erase (strt_iter, end_iter)
Erases the range of pairs starting from “strt_iter” to the “end_iter”.
- Time complexity: O(k) (where k is the linear distance between strt_iter and end_iter i.e. the number of elements between the given range).
Example
C++
#include <iostream>
#include <map> // for map operations
using namespace std;
int main()
{
map< char , int > mp;
map< char , int >::iterator it;
map< char , int >::iterator it1;
map< char , int >::iterator it2;
mp[ 'a' ] = 5;
mp[ 'b' ] = 10;
mp[ 'c' ] = 15;
mp[ 'd' ] = 20;
mp[ 'e' ] = 30;
cout << "The initial map elements are : \n" ;
for (it1 = mp.begin(); it1 != mp.end(); ++it1)
cout << it1->first << "->" << it1->second << endl;
it = mp.begin();
cout << endl;
++it;
mp.erase(it);
cout << "The map elements after 1st deletion are : \n" ;
for (it1 = mp.begin(); it1 != mp.end(); ++it1)
cout << it1->first << "->" << it1->second << endl;
cout << endl;
int c = mp.erase( 'c' );
cout << "The map elements after 2nd deletion are : \n" ;
for (it1 = mp.begin(); it1 != mp.end(); ++it1)
cout << it1->first << "->" << it1->second << endl;
cout << "The number of elements deleted in 2nd "
"deletion are : " ;
cout << c << endl;
cout << endl;
int d = mp.erase( 'w' );
cout << "The map elements after 3rd deletion are : \n" ;
for (it1 = mp.begin(); it1 != mp.end(); ++it1)
cout << it1->first << "->" << it1->second << endl;
cout << "The number of elements deleted in 3rd "
"deletion are : " ;
cout << d << endl;
cout << endl;
++it;
++it;
mp.erase(it, mp.end());
cout << "The map elements after 4th deletion are : \n" ;
for (it1 = mp.begin(); it1 != mp.end(); ++it1)
cout << it1->first << "->" << it1->second << endl;
cout << endl;
}
|
Output
The initial map elements are :
a->5
b->10
c->15
d->20
e->30
The map elements after 1st deletion are :
a->5
c->15
d->20
e->30
The map elements after 2nd deletion are :
a->5
d->20
e->30
The number of elements deleted in 2nd deletion are : 1
The map elements after 3rd deletion are :
a->5
d->20
e->30
The number of elements deleted in 3rd deletion are : 0
The map elements after 4th deletion are :
a->5
2. Using clear()
The clear function clears all the elements present in the map. After this function is called, the size of map becomes 0.
Syntax
map_name.clear()
Example
CPP
#include <iostream>
#include <map> // for map operations
using namespace std;
int main()
{
map< char , int > mp;
map< char , int >::iterator it;
mp[ 'a' ] = 5;
mp[ 'b' ] = 10;
mp[ 'c' ] = 15;
mp[ 'd' ] = 20;
mp[ 'e' ] = 30;
cout << "The initial map elements are : \n" ;
for ( auto it1 = mp.begin(); it1 != mp.end(); ++it1)
cout << it1->first << "->" << it1->second << endl;
mp.clear();
cout << "The map elements after clearing all elements "
"are : \n" ;
for ( auto it1 = mp.begin(); it1 != mp.end(); ++it1)
cout << it1->first << "->" << it1->second << endl;
}
|
Output
The initial map elements are :
a->5
b->10
c->15
d->20
e->30
The map elements after clearing all elements are :
Share your thoughts in the comments
Please Login to comment...