- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to delete last element from a map in C++
Here we will see how to delete the last element from C++ STL map. The map is the hash table based data type, it has key and value. We can use the prev() method to get last element, and erase() function to delete as follows.
Example
#include<iostream> #include<map> using namespace std; int main() { map<string, int> my_map; my_map["first"] = 10; my_map["second"] = 20; my_map["third"] = 30; cout << "Map elements before deleting the last element:"<<endl; for (auto it = my_map.begin(); it != my_map.end(); it++) cout << it->first << " ==> " << it->second << endl; cout << "removing the last element from the map"<<endl; my_map.erase(prev(my_map.end())); cout << "Map elements after deleting the last element :"<<endl; for (auto it = my_map.begin(); it != my_map.end(); it++) cout << it->first << " ==> " << it->second << endl; }
Output
Map elements before deleting the last element: first ==> 10 second ==> 20 third ==> 30 removing the last element from the map Map elements after deleting the last element : first ==> 10 second ==> 20
Advertisements