How to delete last element from a map in C++

If we wish to delete the last element from a map, we can use following methods :

  1. using prev(mp.end()) : what prev function do is, it goes back one step back from the given iterator. So using prev function with mp.end() will return an iterator which points the last element of the map.
    Implementation:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <bits/stdc++.h>
      
    using namespace std;
      
    int main()
    {
      
        map<int, int> mp;
      
        // Adding some elements in mp
        mp[1] = 10;
        mp[2] = 20;
        mp[3] = 30;
      
        cout << "Contents of mp before deleting"
               " the last element :\n";
        for (auto it = mp.begin(); it != mp.end(); it++)
            cout << it->first << " ==> "
                 << it->second << "\n";
      
        cout << "Deleting the last element from"
               " the map.\n";
        mp.erase(prev(mp.end()));
      
        cout << "Contents of mp after deleting the last"
                " element :\n";
        for (auto it = mp.begin(); it != mp.end(); it++)
            cout << it->first << " ==> "
                 << it->second << "\n";
    }

    chevron_right

    
    

    Output:



    Contents of mp before deleting the last element :
    1 ==> 10
    2 ==> 20
    3 ==> 30
    Deleting the last element from the map.
    Contents of mp after deleting the last element :
    1 ==> 10
    2 ==> 20
    
  2. using iterator– : Set an iterator to mp.end() and then use iterator– to get to the last element in the map and then delete it using erase function.
    Implementation:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <bits/stdc++.h>
      
    using namespace std;
      
    int main()
    {
      
        map<int, int> mp;
        // Adding some elements in mp
        mp[1] = 10;
        mp[2] = 20;
        mp[3] = 30;
      
        cout << "Contents of mp before deleting "
                "the last element :\n";
        for (auto it = mp.begin(); it != mp.end(); it++)
            cout << it->first << " ==> " 
                 << it->second << "\n";
      
        cout << "Deleting the last element from"
                " the map.\n";
        auto it = mp.end();
        it--;
        mp.erase(it);
      
        cout << "Contents of mp after deleting the"
                " last element :\n";
        for (auto it = mp.begin(); it != mp.end(); it++)
            cout << it->first << " ==> "
                 << it->second << "\n";
    }

    chevron_right

    
    

    Output:

    Contents of mp before deleting the last element :
    1 ==> 10
    2 ==> 20
    3 ==> 30
    Deleting the last element from the map.
    Contents of mp after deleting the last element :
    1 ==> 10
    2 ==> 20
    


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.