Last Updated : 11 Jul, 2025
in C++, std::unordered_map::erase() is a built-in function used remove elements from the unordered_map container. It is a member function of std::unordered_map class defined inside <unordered_map> header file.
Example:
C++
// C++ Program to demonstrate the use of
// unordered_map::erase()
#include <bits/stdc++.h>
using namespace std;
int main() {
unordered_map<int, string> um = {{1, "Geeks"},
{3, "GeeksforGeeks"}, {2, "Geeksfor"}};
um.erase(3);
for (auto i : um)
cout << i.first << ": " << i.second << endl;
return 0;
}
2: Geeksfor 1: Geeksunordered_map::erase() Syntax
C++ provides 3 implementations of the std::unordered_map::erase() method:
um.erase(k); // Erases by key
um.erase(it); // Erases by iterator
um.erase(first, last); // Erases range of elements
These implementations can be used to do erase the elements in the following ways:
Erase an Element by KeyWith the help of std::unordered_map::erase() function, we can erase the specified key from the std::unordered_map container.
Syntaxum.erase(k);
Parameters
Return Value
// C++ program to remove element by key
// suing unordered_map::erase
#include <bits/stdc++.h>
using namespace std;
int main() {
unordered_map<int, string> um = {{1, "Geeks"},
{3, "GeeksforGeeks"}, {2, "Geeksfor"}};
// Remove the element with key 2
um.erase(2);
for (auto i: um)
cout << i.first << ": " << i.second << '\n';
return 0;
}
1: Geeks 3: GeeksforGeeks
Time Complexity: O(1) in average case, O(n) in worst case, where n is the number of elements in the unordered_map.
Auxiliary Space: O(1)
We can also use unordered_map::erase() to delete an element at a specific position using an iterator. This method is useful when we are iterating the map and removing the elements based on some given conditions.
Syntaxum.erase(it);
Parameters
Return Value
// C++ program to remove element by iterator
// using unordered_map::erase()
#include <bits/stdc++.h>
using namespace std;
int main() {
unordered_map<int, string> um = {{1, "Geeks"},
{3, "GeeksforGeeks"}, {2, "Geeksfor"}};
// Remove the second element
um.erase(next(um.begin(), 1));
for (auto i: um)
cout << i.first << ": " << i.second << '\n';
return 0;
}
2: Geeksfor 3: GeeksforGeeks
Time Complexity: O(1)
Auxiliary Space: O(1)
We can also remove a sequence of elements in one operation using std::unordered_map::erase() method by defining the range using iterators.
Syntaxum.erase(first, last);
Parameters
Return Value
// C++ program to remove range of elements
// using unordered_map::erase()
#include <bits/stdc++.h>
using namespace std;
int main() {
unordered_map<int, string> um = {{1, "Geeks"},
{3, "GeeksforGeeks"}, {2, "Geeksfor"}};
// Remove all elements between 2nd element to
// third element
um.erase(next(um.begin(), 1), next(um.begin(),
3));
for (auto i: um)
cout << i.first << ": " << i.second << '\n';
return 0;
}
Time Complexity: O(k) in average case, where k is number of elements between first and last, O(n) in worst case, where n is the number of elements in unordered_map.
Auxiliary Space: O(1)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4