What is the unordered_map::erase() function in C++?
In this shot, we will learn how to use the unordered_map::erase() function in C++.
Introduction
The unordered_map::erase() function is available in the <unordered_map> header file in C++.
The unordered_map::erase() removes some particular key-value pairs from the map. It has three variations:
- Specifying the key: It erases the key-value pair of the specified key from the unordered map.
- Specifying the iterator: It erases the key-value pair present at that iterator.
- Specifying the range: It erases the key-value pairs present in the specified range of the two iterators (including the starting iterator but excluding the last).
Syntax
The syntax of the three variations of the unordered_map::erase() function is given below:
iterator erase(iterator position);
size_type erase(K key);
iterator erase (iterator first, iterator last);
Parameter
The three variations of the unordered_map::erase() function accepts the following parameters:
-
Specifying the key: In this case, the function accepts only one parameter, i.e., the key whose equivalent key-value pair needs to be erased from the map.
-
Specifying the iterator: In this case, the function accepts only one parameter, i.e., the iterator, which points to the key-value pair to be erased.
-
Specifying the range: In this case, the function accepts two parameters:
- First: The starting iterator of the range from where the key-value pairs are to be erased.
- Last: The ending iterator of the range till the key-value pairs are to be erased.
Return value
The three variations of the unordered_map::erase() function returns in the following manner:
-
Specifying the key: It returns the number of elements erased in this case. In other words, it would return if the specified key exists (as unordered maps have only one unique key) and otherwise.
-
Specifying the iterator and specifying the range: In this case, it returns an iterator that immediately points to the position following the last elements erased in the unordered map.
Code
Let’s have a look at the code below:
#include <iostream>#include <unordered_map>using namespace std;int main(){unordered_map<int,string> umap ={{12, "unordered"},{16, "map"},{89, "in"},{66, "C++"}};cout << "After erasing by iterator : \n";umap.erase(umap.begin());for (auto p : umap)cout << p.first << "->" << p.second << endl;cout << "After erasing by key : \n";umap.erase(16);for (auto p : umap)cout << p.first << "->" << p.second << endl;cout << "After erasing by range : \n";auto i = umap.begin();i++;umap.erase(i, umap.end());for (auto p : umap)cout << p.first << "->" << p.second << endl;return 0;}
Explanation
-
In lines 1 and 2, we import the required header files.
-
In line 5, we make a
main()function. -
From lines 7 to 12, we initialize an unordered map with integer type keys and string type values.
-
From lines 14 to 18, we erase elements using an iterator and display the remaining elements.
-
From lines 20 to 24, we erase the elements using the key and display the remaining elements.
-
From lines 26 to 32, we erase elements using a range and display the remaining elements.
This is how we use the unordered_map::erase() function in C++ to remove some particular key-value pairs from the map.