How to Erase / Remove an element from an unordered_map

In this article we will discuss the different ways to remove an element from an unordered_map.

Unordered_map provides different overloaded version of erase() member function to delete key value pairs from map.

Erasing an element from an unordered_map by key

Unordered_map provides following overloaded version of erase() that accepts a key by value and check if any element with given key exists and if yes then deletes that element.

size_type erase ( const key_type& k );

It returns the number of elements deleted. But as unordered_map can conain only unique elements, so it will return either 0 or 1.

Lets see an example,

#include <iostream>
#include <unordered_map>
#include <string>

int main() {
// Initialize an unordered_map through initializer_list
	std::unordered_map<std::string, int> wordMap( { { "First", 1 }, { "Second",
			2 }, { "Third", 3 }, { "Fourth", 4 }, { "Fifth", 5 } });

//Erase element by key
	if (wordMap.erase("Second") == 1)
		std::cout << "Element Deleted" << std::endl;

	return 0;
}

Output

Element Deleted

Erasing an element from an unordered_map through an iterator

Unordered_map also provides an overloaded version of erase() that accepts an iterator an deletes it i.e.

iterator erase ( const_iterator pos );

It accepts an iterator and deletes the corresponding element. Also, it returns the iterator pointing to an element i.e. next to last deleted element.

Let’s see an example, where it searches for an element with key “Fourth” and then deletes that using iterator,

#include <iostream>
#include <unordered_map>
#include <string>

int main() {
	// Initialize an unordered_map through initializer_list
	std::unordered_map<std::string, int> wordMap( { { "First", 1 }, { "Second",
			2 }, { "Third", 3 }, { "Fourth", 4 }, { "Fifth", 5 } });

	// Iterator pointing to first element of unordered_map
	std::unordered_map<std::string, int>::iterator it = wordMap.find("Fourth");

	// Erase the element pointed by iterator it
	if (it != wordMap.end())
		wordMap.erase(it);

	// Dsiplay the map contents
	for (std::pair<std::string, int> element : wordMap)
		std::cout << element.first << " :: " << element.second << std::endl;

	return 0;
}

Ouput:

Fifth :: 5
First :: 1
Second :: 2
Third :: 3

In the above output entry with key “Fourth” is not present because that’s deleted.

Erasing an element by value from an unordered_map

Unordered_map store values in a key value pair and erase() function of unordered_map can take either iterator or key.

So, to delete an element by value we need to, iterate over all the elements in map and search for element whose value matches the given value. Then  we will use its iterator to delete that element.

Let’s see an example, it searches for an element with value 2 and then deletes that element i.e.

#include <iostream>
#include <unordered_map>
#include <string>

int main() {
	// Initialize an unordered_map through initializer_list
	std::unordered_map<std::string, int> wordMap( { { "First", 1 }, { "Second",
			2 }, { "Third", 3 }, { "Fourth", 4 }, { "Fifth", 5 } });

	// Iterator pointing to first element of unordered_map
	std::unordered_map<std::string, int>::iterator it = wordMap.begin();

	// Search for an element with value 2
	while(it != wordMap.end())
	{
		if(it->second == 2)
			break;
		it++;
	}

	// Erase the element pointed by iterator it
	if (it != wordMap.end())
		wordMap.erase(it);

	// Display the map contents
	for (std::pair<std::string, int> element : wordMap)
		std::cout << element.first << " :: " << element.second << std::endl;

	return 0;
}

Output:

Fifth :: 5
First :: 1
Fourth :: 4
Third :: 3

 

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top