Different ways to delete elements in std::map (erase() and clear())
This article deals with the deletion part of Maps.
- Using erase() : erase() is used to erase the pair in map mentioned in argument, either its position, its value or a range of number.
- erase(key) : Erases the key-value pair using 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 map) - erase(iter) : Erases the pair at the position pointed by the iterator mentioned in its argument.
Time complexity : log(n) (n is size of map) - erase(strt_iter, end_iter) : Erases the range of pairs starting from “strt_iter” to the “end_iter”.
Time complexity : O(k) (k is size of map)
// C++ code to demonstrate the working of erase()#include<iostream>#include<map> // for map operationsusingnamespacestd;intmain(){// declaring map// of char and intmap<char,int> mp;// declaring iteratorsmap<char,int>::iterator it ;map<char,int>::iterator it1;map<char,int>::iterator it2;// inserting valuesmp['a']=5;mp['b']=10;mp['c']=15;mp['d']=20;mp['e']=30;// printing initial map elementscout <<"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;// erasing element using iterator// erases 2nd element// 'b'++it;mp.erase(it);// printing map elements after deletioncout <<"The map elements after 1st deletion are : \n";for(it1 = mp.begin(); it1!=mp.end(); ++it1)cout << it1->first <<"->"<< it1->second << endl;cout << endl;// erasing element using valueintc = mp.erase('c');// printing map elements after deletioncout <<"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;// erasing element using value// key not presentintd = mp.erase('w');// printing map elements after deletioncout <<"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;// erasing element using range iterator// deletes "d" and "e" keysmp.erase(it, mp.end());// printing map elements 4th deletioncout <<"The map elements after 4th deletion are : \n";for(it1 = mp.begin(); it1!=mp.end(); ++it1)cout << it1->first <<"->"<< it1->second << endl;cout << endl;}chevron_rightfilter_noneOutput:
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
- erase(key) : Erases the key-value pair using 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.
- Using clear() : This function clears all the elements present in the map. After this function is called, the size of map becomes 0.
// C++ code to demonstrate the working of clear()#include<iostream>#include<map> // for map operationsusingnamespacestd;intmain(){// declaring map// of char and intmap<char,int> mp;// declaring iteratormap<char,int>::iterator it ;// inserting valuesmp['a']=5;mp['b']=10;mp['c']=15;mp['d']=20;mp['e']=30;// printing initial map elementscout <<"The initial map elements are : \n";for(it1 = mp.begin(); it1!=mp.end(); ++it1)cout << it1->first <<"->"<< it1->second << endl;// using clear() to erase all elements in mapmp.clear();// printing map elements after deletioncout <<"The map elements after clearing all elements are : \n";for(it1 = mp.begin(); it1!=mp.end(); ++it1)cout << it1->first <<"->"<< it1->second << endl;}chevron_rightfilter_noneOutput:
The initial map elements are : a->5 b->10 c->15 d->20 e->30 The map elements after clearing all elements are :
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- vector erase() and clear() in C++
- deque::clear() and deque::erase() in C++ STL
- How to erase an element from a vector using erase() and reverse_iterator?
- Delete elements in C++ STL list
- Minimum delete operations to make all elements of array same
- set::erase in C++ STL
- multimap::erase() in C++ STL
- multiset erase() in C++ STL
- std::string::erase in C++
- unordered_map erase in C++ STL
- map erase() function in C++ STL
- unordered_multimap erase in C++ STL
- list erase() function in C++ STL
- unordered_set erase() function in C++ STL
- unordered_multiset erase() function in C++ STL


