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 :
- 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:#include <bits/stdc++.h>usingnamespacestd;intmain(){map<int,int> mp;// Adding some elements in mpmp[1] = 10;mp[2] = 20;mp[3] = 30;cout <<"Contents of mp before deleting"" the last element :\n";for(autoit = 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(autoit = mp.begin(); it != mp.end(); it++)cout << it->first <<" ==> "<< it->second <<"\n";}chevron_rightfilter_noneOutput: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
- 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:#include <bits/stdc++.h>usingnamespacestd;intmain(){map<int,int> mp;// Adding some elements in mpmp[1] = 10;mp[2] = 20;mp[3] = 30;cout <<"Contents of mp before deleting ""the last element :\n";for(autoit = mp.begin(); it != mp.end(); it++)cout << it->first <<" ==> "<< it->second <<"\n";cout <<"Deleting the last element from"" the map.\n";autoit = mp.end();it--;mp.erase(it);cout <<"Contents of mp after deleting the"" last element :\n";for(autoit = mp.begin(); it != mp.end(); it++)cout << it->first <<" ==> "<< it->second <<"\n";}chevron_rightfilter_noneOutput: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
Recommended Posts:
- How to delete last element from a set in C++
- How to delete last element from a List in C++ STL
- How to delete an element from the Set by passing its value in C++
- How to delete an array element based on key in PHP?
- PHP | Program to delete an element from array using unset() function
- Queries to insert, delete one occurrence of a number and print the least and most frequent element
- Delete the array elements in JavaScript | delete vs splice
- Replace each element by the difference of the total size of the array and frequency of that element
- delete() in C++
- "delete this" in C++
- Replace every element with the greatest element on its left side
- Replace every array element by Bitwise Xor of previous and next element
- Replace every element with the smallest element on its left side
- delete and free() in C++
- How to delete a file using PHP ?
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.



