- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- C++ Remove an Entry Using Value from HashMap while Iterating over It
- Remove entry with specified key from OrderedDictionary in C#
- Remove entry with specified key from the StringDictionary in C#
- Remove the entry with specified key from ListDictionary in C#
- Java Program to remove key value pair from HashMap?
- Java Program to remove a key from a HashMap
- Java Program to remove a key from a HashMap only if it is associated with a given value
- Delete items from dictionary while iterating in Python
- Java Program to Get key from HashMap using the value
- Removing the specified key entry from HybridDictionary in C#
- How to remove Lua table entry by its key?
- Iterating over array in Java
- Iterating over ArrayLists in Java
- Iterating over Arrays in Java
- How do you use ‘foreach’ loop for iterating over an array in C#?
