Delete map c++ – C++ Map : Erase Element by Key or Iterator or Range

Delete map c++: In the previous article, we have discussed about C++ : Map Tutorial Part 1: Usage Detail with examples. Let us learn Map: Erase Element by Key or Iterator or Range in C++ Program.

Deleting a Key-value Pair From Map

std map.erase: In this article we will discuss about 3 different ways how we can delete a key-value pair from map. So, let’s explore the concept.

3 overloaded version of erase() function to remove elements in std::map i.e.

Approach-1 : Erasing element by Key

Map erase c++: erase() function accepts the key and removes that key-value pair or element from that map whose key match with the passed key.

Synatx- size_type erase (const key_type& key);

Where,

  • key represents the key to be deleted
  • returns number of elements deleted as there is always unique key in map
  • So, returns 1 if the key is deleted and 0 if key is not found

Let’s see the below code to understand it clearly of c++ map erase.

#include <iostream>
#include <map>
#include <string>

int main() 
{
    // sampleMap as map which contains string & int
    // where strings are the keys and integers are the values
    std::map<std::string, int> sampleMap = { { "A", 1 }, { "B", 2 },
            { "C", 3 }, { "D", 4 }, { "E", 4 }, { "F", 5 }, { "G", 6 } };
     
    // Printing the map elements before erase       
    std::cout << "Map before erase" << std::endl;
    for (auto elem : sampleMap)
        std::cout << elem.first << " :: " << elem.second << std::endl;
        
    // Deleting/Erasing the element from map with given key.
    int result = sampleMap.erase("C");
    // Checking if element is actually deleted from map or not
    if(result == 1)
        std::cout<<"Element associated with Key 'C' is deleted"<<std::endl;
    else
        std::cout<<"Element associated with Key 'C' is deleted"<<std::endl;
      
    // Printing the map elements after erase    
    std::cout << "Map Entries After Deletion" << std::endl;
    for (auto elem : sampleMap)
        std::cout << elem.first << " :: " << elem.second << std::endl;
    return 0;
}
Output :

Map before erase
A :: 1
B :: 2
C :: 3
D :: 4
E :: 4
F :: 5
G :: 6
Element associated with Key 'C' is deleted
Map after erase
A :: 1
B :: 2
D :: 4
E :: 4
F :: 5
G :: 6

Approach-2 : Erasing element by Iterator

erase() function accepts the Iterator and removes the element pointed by that iterator.

iterator erase (const_iterator position);

Where,

  • iterator position is passed in erase() function
  • If the iterator is found valid then it is removed

Let’s see the below code to understand it clearly.

#include <iostream>
#include <map>
#include <string>

int main() 
{
    // sampleMap as map which contains string & int
    // where strings are the keys and integers are the values
    std::map<std::string, int> sampleMap = { { "A", 1 }, { "B", 2 },
            { "C", 3 }, { "D", 4 }, { "E", 4 }, { "F", 5 }, { "G", 6 } };
     
    // Printing the map elements before erase       
    std::cout << "Map before erase" << std::endl;
    for (auto elem : sampleMap)
        std::cout << elem.first << " :: " << elem.second << std::endl;
        
    // Finding the iterator of element with key 'C'
    std::map<std::string, int>::iterator it = sampleMap.find("C");
    // Checkingh if iterator is valid.
    if(it != sampleMap.end())
    {
        // If the iterator is valid or found then
        // Delete the element pointed by iterator
        sampleMap.erase(it);
        std::cout<<"That key-value deleted"<<std::endl;
    }
    else
        std::cout<<"That Key Not Found"<<std::endl;
      
    // Printing the map elements after erase    
    std::cout << "Map after erase" << std::endl;
    for (auto elem : sampleMap)
        std::cout << elem.first << " :: " << elem.second << std::endl;
    return 0;
}
Output :

Map before erase
A :: 1
B :: 2
C :: 3
D :: 4
E :: 4
F :: 5
G :: 6
That key-value deleted
Map after erase
A :: 1
B :: 2
D :: 4
E :: 4
F :: 5
G :: 6

Approach-3 : Erasing element by Iterator Range

erase() function accepts the Iterator range and deletes all the elements within that start and end range.

void erase( iterator start, iterator end );

Where,

  • start represents start iterator position
  • end represents end iterator position

Let’s see the below code to understand it clearly.

#include <iostream>
#include <map>
#include <string>
int main() 
{
    // sampleMap as map which contains string & int
    // where strings are the keys and integers are the values
    std::map<std::string, int> sampleMap = { { "A", 1 }, { "B", 2 },
            { "C", 3 }, { "D", 4 }, { "E", 4 }, { "F", 5 }, { "G", 6 } };
     
    // Printing the map elements before erase       
    std::cout << "Map before erase" << std::endl;
    for (auto elem : sampleMap)
        std::cout << elem.first << " :: " << elem.second << std::endl;
        
    // Creating an iterator pointing to start of map
    std::map<std::string, int>::iterator it1 = sampleMap.begin();
    // Creating an iterator pointing to start of map
    std::map<std::string, int>::iterator it2 = sampleMap.begin();
    // Increment Iterator
    it2++;
    // Increment Iterator
    it2++;
    // Itr2 is now pointing to 3rd element
    it2++;
    // Checking if iterator is valid.
    if (it1 != sampleMap.end() && it2 != sampleMap.end())
    {
        // Deleting the element pointed by iterator
        sampleMap.erase(it1, it2);
        std::cout << "Elements Removed within range" << std::endl;
    }
    else
        std::cout << "That Key Not Found" << std::endl;
    std::cout << "Map Entries After Deletion" << std::endl;
    // Printing the map elements
    for (auto elem : sampleMap)
        std::cout << elem.first << " :: " << elem.second << std::endl;
    return 0;
}
Output :

Map before erase
A :: 1
B :: 2
C :: 3
D :: 4
E :: 4
F :: 5
G :: 6
Elements Removed within range
Map Entries After Deletion
D :: 4
E :: 4
F :: 5
G :: 6