In this article we will discuss the different ways to delete a key-value pair from map.
std::map provides 3 overloaded version of erase() to remove elements from map i.e.
- Erase by key
- Erase by Iterator
- Erase a range
Lets discuss them one by one,
Erase Element from Map by Key
std::map provides a erase function that accepts the key and removes the elements (Key- Value pairs) whose key matches the passed key k.
size_type erase (const key_type& k);
It returns the number of elements deleted, but as there can be unique keys only in std::map. Therefore it will return 1 if element is deleted else it will return 0 if given key is not found in map.
Suppose we have a map of word and int i.e.
Frequently Asked:
// Map of string & int i.e. words as key & there // occurrence count as values std::map<std::string, int> wordMap = { { "is", 6 }, { "the", 5 }, { "hat", 9 }, { "at", 6 }, { "of", 2 }, { "hello", 1 } };
Now, lets delete the element with key “is” i.e.
// Removes the element from map with given key. int result = wordMap.erase("is");
Checkout Complete example as follows,
#include <iostream> #include <map> #include <string> int main() { // Map of string & int i.e. words as key & there // occurrence count as values std::map<std::string, int> wordMap = { { "is", 6 }, { "the", 5 }, { "hat", 9 }, { "at", 6 }, { "of", 2 }, { "hello", 1 } }; std::cout << "Map Entries Before Deletion" << std::endl; // Print the map elements for (auto elem : wordMap) std::cout << elem.first << " :: " << elem.second << std::endl; // Removes the element from map with given key. int result = wordMap.erase("is"); // Check if element is actually deleted from map if(result == 1) std::cout<<"Element with key 'is' deleted"<<std::endl; else std::cout<<"Element with key 'is' Not Found"<<std::endl; std::cout << "Map Entries After Deletion" << std::endl; // Print the map elements for (auto elem : wordMap) std::cout << elem.first << " :: " << elem.second << std::endl; return 0; }
Output:
Map Entries Before Deletion at :: 6 hat :: 9 hello :: 1 is :: 6 of :: 2 the :: 5 Element with key 'is' deleted Map Entries After Deletion at :: 6 hat :: 9 hello :: 1 of :: 2 the :: 5
Erase Element from Map by Iterator
std::map provides a erase function that accepts the Iterator and removes the element pointed by the iterator.
iterator erase (const_iterator position);
It returns the iterator of the next element.
Suppose we have a map of word and int i.e.
// Map of string & int i.e. words as key & there // occurrence count as values std::map<std::string, int> wordMap = { { "is", 6 }, { "the", 5 }, { "hat", 9 }, { "at", 6 }, { "of", 2 }, { "hello", 1 } };
Now, lets delete the element with key “of” i.e.
Let’s first find the iterator pointing to it i.e.
// Get the iterator of element with key 'of' std::map<std::string, int>::iterator it = wordMap.find("of");
Then check if iterator is valid or not. If its valid then only remove the element through it
if(it != wordMap.end()) { // Remove the element pointed by iterator wordMap.erase(it); }
Checkout Complete example as follows,
#include <iostream> #include <map> #include <string> int main() { // Map of string & int i.e. words as key & there // occurrence count as values std::map<std::string, int> wordMap = { { "is", 6 }, { "the", 5 }, { "hat", 9 }, { "at", 6 }, { "of", 2 }, { "hello", 1 } }; std::cout << "Map Entries Before Deletion" << std::endl; // Print the map elements for (auto elem : wordMap) std::cout << elem.first << " :: " << elem.second << std::endl; // Get the iterator of element with key 'of' std::map<std::string, int>::iterator it = wordMap.find("of"); // Check if iterator is valid. if(it != wordMap.end()) { // Remove the element pointed by iterator wordMap.erase(it); std::cout<<"Element Removed"<<std::endl; } else std::cout<<"Key Not Found"<<std::endl; std::cout << "Map Entries After Deletion" << std::endl; // Print the map elements for (auto elem : wordMap) std::cout << elem.first << " :: " << elem.second << std::endl; return 0; }
Output:
Map Entries Before Deletion at :: 6 hat :: 9 hello :: 1 is :: 6 of :: 2 the :: 5 Element Removed Map Entries After Deletion at :: 6 hat :: 9 hello :: 1 is :: 6 the :: 5
Erase Element from Map by Iterator Range
std::map provides a erase function that accepts the Iterator start and end and removes all the elements in the given range i.e. from start to end -1 i.e.
void erase( iterator first, iterator last );
Suppose we have a map of word and int i.e.
// Map of string & int i.e. words as key & there // occurrence count as values std::map<std::string, int> wordMap = { { "is", 6 }, { "the", 5 }, { "hat", 9 }, { "at", 6 }, { "of", 2 }, { "hello", 1 } };
Now, lets delete the elements in range of two iterator it1 & it2 i.e.
// Remove the element pointed by iterator wordMap.erase(it1, it2);
It will delete the elements from it1 to it2 -1.
Checkout Complete example as follows,
#include <iostream> #include <map> #include <string> int main() { // Map of string & int i.e. words as key & there // occurrence count as values std::map<std::string, int> wordMap = { { "is", 6 }, { "the", 5 }, { "hat", 9 }, { "at", 6 }, { "of", 2 }, { "hello", 1 } }; std::cout << "Map Entries Before Deletion" << std::endl; // Print the map elements for (auto elem : wordMap) std::cout << elem.first << " :: " << elem.second << std::endl; // Create an iterator pointing to begin of map std::map<std::string, int>::iterator it1 = wordMap.begin(); // Create an iterator pointing to begin of map std::map<std::string, int>::iterator it2 = wordMap.begin(); // Increment Iterator it2++; // Increment Iterator it2++; // Itr2 is now pointing to 3rd element // Check if iterator is valid. if (it1 != wordMap.end() && it2 != wordMap.end()) { // Remove the element pointed by iterator wordMap.erase(it1, it2); std::cout << "Elements Removed" << std::endl; } else std::cout << "Key Not Found" << std::endl; std::cout << "Map Entries After Deletion" << std::endl; // Print the map elements for (auto elem : wordMap) std::cout << elem.first << " :: " << elem.second << std::endl; return 0; }
Output:
Map Entries Before Deletion at :: 6 hat :: 9 hello :: 1 is :: 6 of :: 2 the :: 5 Elements Removed Map Entries After Deletion hello :: 1 is :: 6 of :: 2 the :: 5
Pointers in C/C++ [Full Course]
Pingback: C++ : Map Tutorial Part 1: Usage Detail with examples – thisPointer.com