C++ unordered_map - erase() Function
The C++ unordered_map::erase function is used to delete either a single element or a range of elements from the unordered_map. It reduces the size of the unordered_map by number of elements deleted from the container.
Syntax
//Version 1 - Delete element at position iterator erase (const_iterator position); //Version 2 - Delete specified key from the unordered_map size_type erase (const key_type& k); //Version 3 - Delete a range of elements in [first, last) iterator erase (const_iterator first, const_iterator last);
Parameters
position |
Iterator specifying position of the element in the unordered_map to be removed. |
k |
Key of the element to be removed from the unordered_map. |
first |
Iterator specifying position of the first element of the range in the unordered_map. Elements in [first, last) position range will be deleted. |
last |
Iterator specifying position of the last element of the range in the unordered_map. Elements in [first, last) position range will be deleted. |
Return Value
For version 2: the function returns number of elements erased. For other versions, the function returns an iterator pointed to the element that follows the last element removed. If the last element is removed, then the iterator will point to the unordered_map::end.
Time Complexity
Average case: Linear in the number of elements removed for version 1 and version 2.
Worst case: Linear in the container size..
Example:
In the example below, the unordered_map::erase function is used to delete a single element from uMap.
#include <iostream> #include <unordered_map> using namespace std; int main (){ unordered_map<int, string> uMap; unordered_map<int, string>::iterator it; //populating unordered_map uMap[101] = "John"; uMap[102] = "Marry"; uMap[103] = "Kim"; uMap[104] = "Jo"; uMap[105] = "Ramesh"; cout<<"uMap contains: \n "; for(it = uMap.begin(); it != uMap.end(); ++it) cout<<it->first<<" "<<it->second<<"\n "; //version 1: deletes element at position = 2 it = uMap.begin(); it++; uMap.erase(it); //version 2: deletes key=103 from the unordered_map uMap.erase(103); cout<<"\nuMap contains: \n "; for(it = uMap.begin(); it != uMap.end(); ++it) cout<<it->first<<" "<<it->second<<"\n "; return 0; }
The output of the above code will be:
uMap contains: 105 Ramesh 104 Jo 103 Kim 102 Marry 101 John uMap contains: 105 Ramesh 102 Marry 101 John
Example:
A range of elements can also be deleted from the unordered_map. Consider the example below:
#include <iostream> #include <unordered_map> using namespace std; int main (){ unordered_map<int, string> uMap; unordered_map<int, string>::iterator start_it; unordered_map<int, string>::iterator stop_it; unordered_map<int, string>::iterator it; //populating map uMap[101] = "John"; uMap[102] = "Marry"; uMap[103] = "Kim"; uMap[104] = "Jo"; uMap[105] = "Ramesh"; cout<<"uMap contains: \n "; for(it = uMap.begin(); it != uMap.end(); ++it) cout<<it->first<<" "<<it->second<<"\n "; //setting the start position at //third element of the map start_it = uMap.begin(); start_it++; start_it++; //setting the stop position at end of the map stop_it = uMap.end(); //version 3: erase a range of elements uMap.erase(start_it, stop_it); cout<<"\nuMap contains: \n "; for(it = uMap.begin(); it != uMap.end(); ++it) cout<<it->first<<" "<<it->second<<"\n "; return 0; }
The output of the above code will be:
uMap contains: 105 Ramesh 104 Jo 103 Kim 102 Marry 101 John uMap contains: 105 Ramesh 104 Jo
❮ C++ <unordered_map> Library