Open In App
Related Articles

Different ways to delete elements in std::map (erase() and clear())

Improve Article
Improve
Save Article
Save
Like Article
Like

This article deals with the deletion part of Maps.

  1. Using erase() : erase() is used to erase the pair in map mentioned in argument, either its position, its value or a range of number.
    • erase(key) : Erases the key-value pair using key mentioned in its argument. reorders the map after deletion. It returns the number of entries deleted. If non-existing keys is deleted, 0 is returned.
      Time complexity : log(n) (n is size of map)
    • erase(iter) : Erases the pair at the position pointed by the iterator mentioned in its argument.
      Time complexity : log(n) (n is size of map)
    • erase(strt_iter, end_iter) : Erases the range of pairs starting from “strt_iter” to the “end_iter”.
      Time complexity : O(k) (k is size of map)




    // C++ code to demonstrate the working of erase()
      
    #include<iostream>
    #include<map> // for map operations
    using namespace std;
      
    int main()
    {
        // declaring map
        // of char and int
        map< char, int > mp;
          
        // declaring iterators
        map<char, int>::iterator it ;
        map<char, int>::iterator it1;
        map<char, int>::iterator it2;
          
        // inserting values 
        mp['a']=5;
        mp['b']=10;
        mp['c']=15;
        mp['d']=20;
        mp['e']=30;
      
        // printing initial map elements
        cout << "The initial map elements are : \n";
          
        for (it1 = mp.begin(); it1!=mp.end(); ++it1)
            cout << it1->first << "->" << it1->second << endl;
          
        it = mp.begin();
          
        cout << endl;
          
        // erasing element using iterator
        // erases 2nd element
        // 'b'
        ++it;
        mp.erase(it);
          
        // printing map elements after deletion
        cout << "The map elements after 1st deletion are : \n";
          
        for (it1 = mp.begin(); it1!=mp.end(); ++it1)
            cout << it1->first << "->" << it1->second << endl;
          
        cout << endl;
          
        // erasing element using value 
        int c = mp.erase('c');
          
        // printing map elements after deletion
        cout << "The map elements after 2nd deletion are : \n";
          
        for (it1 = mp.begin(); it1!=mp.end(); ++it1)
            cout << it1->first << "->" << it1->second << endl;
          
        cout << "The number of elements deleted in 2nd deletion are : ";
        cout << c << endl;
          
        cout << endl;
          
        // erasing element using value 
        // key not present
        int d = mp.erase('w');
          
        // printing map elements after deletion
        cout << "The map elements after 3rd deletion are : \n";
          
        for (it1 = mp.begin(); it1!=mp.end(); ++it1)
            cout << it1->first << "->" << it1->second << endl;
          
        cout << "The number of elements deleted in 3rd deletion are : ";
        cout << d << endl;
          
        cout << endl;
          
          
        ++it;
        ++it;
          
        // erasing element using range iterator
        // deletes "d" and "e" keys
        mp.erase(it, mp.end());
          
        // printing map elements 4th deletion
        cout << "The map elements after 4th deletion are : \n";
      
        for (it1 = mp.begin(); it1!=mp.end(); ++it1)
            cout << it1->first << "->" << it1->second << endl;
          
        cout << endl;
          
    }

    
    

    Output:

    The initial map elements are : 
    a->5
    b->10
    c->15
    d->20
    e->30
    
    The map elements after 1st deletion are : 
    a->5
    c->15
    d->20
    e->30
    
    The map elements after 2nd deletion are : 
    a->5
    d->20
    e->30
    The number of elements deleted in 2nd deletion are : 1
    
    The map elements after 3rd deletion are : 
    a->5
    d->20
    e->30
    The number of elements deleted in 3rd deletion are : 0
    
    The map elements after 4th deletion are : 
    a->5
    
  2. Using clear() : This function clears all the elements present in the map. After this function is called, the size of map becomes 0.




    // C++ code to demonstrate the working of clear()
      
    #include<iostream>
    #include<map> // for map operations
    using namespace std;
      
    int main()
    {
        // declaring map
        // of char and int
        map< char, int > mp;
          
        // declaring iterator
        map<char, int>::iterator it ;
          
        // inserting values 
         mp['a']=5;
         mp['b']=10;
         mp['c']=15;
         mp['d']=20;
         mp['e']=30;
      
        // printing initial map elements
        cout << "The initial map elements are : \n";
        for (it1 = mp.begin(); it1!=mp.end();  ++it1)
        cout << it1->first << "->" << it1->second << endl;
          
        // using clear() to erase all elements in map
        mp.clear();
          
        // printing map elements after deletion
        cout << "The map elements after clearing all elements are : \n";
        for (it1 = mp.begin(); it1!=mp.end();  ++it1)
        cout << it1->first << "->" << it1->second << endl;
          
    }

    
    

    Output:

    The initial map elements are : 
    a->5
    b->10
    c->15
    d->20
    e->30
    The map elements after clearing all elements are : 
    

If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 14 Sep, 2023
Like Article
Save Article
Previous
Next
Similar Reads