여기서는 C++ STL 맵에서 마지막 요소를 삭제하는 방법을 살펴보겠습니다. 맵은 해시 테이블 기반 데이터 유형이며 키와 값이 있습니다. 다음과 같이 prev() 메서드를 사용하여 마지막 요소를 가져오고 erase() 함수를 사용하여 삭제할 수 있습니다.
예시
#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