C++ Remove an Entry Using Key from HashMap while Iterating Over It


This tutorial will discuss how to remove an entry from HashMap using the key while traversing through it. for example,

Input: HashMap: { 1: “Tutorials”,
                  2: “Tutorials”,
                  3: “Point” }, key=1

Output: HashMap: { 2: “Tutorials”,
                   3: “Point” }.

Explanation: The first element is removed using key ‘1’.

Input: HashMap: { 1: “God”,
                  2: “is”,
                  3: “Great” }, key=2

Output: HashMap: { 1: “God”,
                   3: “Great” }.

Approach to Find the Solution

In C++, We can use the key name in the .erase() function to remove entries using a key. But here, we need to remove it while iterating over it, so we need an iterator too.

Here we will iterate through the hashmap and check whether every key is removed and remove the entry when the key matches.

Example

C++ Code for the Above Approach

Without iteration

Below is the code for removing elements without iterating over HashMap.

#include<iostream>
#include<map> // for map operations
using namespace std;
int main(){  
    // Creating HashMap.
    map< int, string > mp;
    // Inserting key-value pair in Hashmap.
    mp[1]="Tutorials";
    mp[2]="Tutorials";
    mp[3]="Point";
    int key = 2;
    // Creating iterator.
    map<int, string>::iterator it ;
    // Printing the initial Hashmap.
    cout<< "HashMap before Deletion:\n";
    for (it = mp.begin(); it!=mp.end(); ++it)
        cout << it->first << "->" << it->second << endl;
    mp.erase(key);
    // Printing Hashmap after deletion.
    cout<< "HashMap After Deletion:\n";
    for (it = mp.begin(); it!=mp.end(); ++it)
        cout << it->first << "->" << it->second << endl;
    return 0;
}

Output

HashMap before Deletion:
1->Tutorials
2->Tutorials
3->Point

HashMap After Deletion:
1->Tutorials
3->Point

Example

Remove Element while Iterating over HashMap

#include<iostream>
#include<map> // for map operations
using namespace std;
int main(){  
    // Creating HashMap.
    map< int, string > mp;
    // Inserting key-value pair in Hashmap.
    mp[1]="Tutorials";
    mp[2]="Tutorials";
    mp[3]="Point";
    int key = 2;
    // Creating iterator.
    map<int, string>::iterator it ;
    // Printing the initial Hashmap.
    cout<< "HashMap before Deletion:\n";
    for (it = mp.begin(); it!=mp.end(); ++it)
        cout << it->first << "->" << it->second << endl;
        // Iterating over HashMap.
    for (it = mp.begin(); it!=mp.end(); ++it){
        int a=it->first;
        // Checking iterator key with required key.
        if(a==key){
            // erasing Element.
            mp.erase(it);
        }
    }
    // Printing Hashmap after deletion.
    cout<< "HashMap After Deletion:\n";
    for (it = mp.begin(); it!=mp.end(); ++it)
        cout << it->first << "->" << it->second << endl;
    return 0;
}

Output

HashMap before Deletion:
1->Tutorials
2->Tutorials
3->Point

HashMap After Deletion:
1->Tutorials
3->Point

Conclusion

In this tutorial, we discussed How to remove an entry from HashMap.We discussed two ways to remove an entry which are iterating over it and without iterating over it. We also discussed the C++ program for this problem which we can do with programming languages like C, Java, Python, etc. We hope you find this tutorial helpful.

Updated on: 26-Nov-2021

620 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements