यहां हम देखेंगे कि C++ STL मैप से लास्ट एलिमेंट को कैसे डिलीट किया जाए। नक्शा हैश तालिका आधारित डेटा प्रकार है, इसमें कुंजी और मूल्य है। हम अंतिम तत्व प्राप्त करने के लिए पिछली () विधि का उपयोग कर सकते हैं, और मिटाने के लिए मिटा () फ़ंक्शन निम्नानुसार कर सकते हैं।
उदाहरण
#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; }
आउटपुट
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