A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/cpp/unordered_map-erase-in-c-stl/ below:

unordered_map erase in C++ STL

unordered_map erase in C++ STL

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;
}

Output
2: Geeksfor
1: Geeks
unordered_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 Key

With the help of std::unordered_map::erase() function, we can erase the specified key from the std::unordered_map container.

Syntax

um.erase(k);

Parameters

Return Value

Example C++
// 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;
}

Output
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)

Erase an Element by Iterator

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.

Syntax

um.erase(it);

Parameters

Return Value

Example C++
// 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;
}

Output
2: Geeksfor
3: GeeksforGeeks

Time Complexity: O(1)
Auxiliary Space: O(1)

Erase a Range of Elements

We can also remove a sequence of elements in one operation using std::unordered_map::erase() method by defining the range using iterators.

Syntax

um.erase(first, last);

Parameters

Return Value

Example C++
// 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