Call erase () to remove the elements in the map. The C ++ Standard Library provides an example, which records the following:
#include <iostream>#include <map>#include "print.hpp"int main(){std::map<int, int> col1;col1.insert(std::make_pair(1, 1));col1.insert(std::make_pair(2, 2));PRINT_ELEMENTS2(col1, "before erase: ");std::map<int, int>::iterator pos;for (pos = col1.begin(); pos != col1.end(); ) {if ((*pos).first == 1) {col1.erase(pos++);}else {++pos;}}PRINT_ELEMENTS2(col1, "after erase: ");return 0;}
Running result:
Before erase: 1, 1, 2
After erase: 2, 2
Press any key to continue
Attach print. HPP
#include <iostream>template <class T>inline void PRINT_ELEMENTS(const T& col1, const char* optcstr="") {typename T::const_iterator pos;std::cout << optcstr;for (pos = col1.begin(); pos != col1.end(); ++pos) {std::cout << (int)*pos << ' ';}std::cout << std::endl;}template <class T>inline void PRINT_ELEMENTS2(T& col1, const char* optcstr="") {typename T::const_iterator pos;std::cout << optcstr;for (pos = col1.begin(); pos != col1.end(); ++pos) {std::cout << (*pos).first << "," << (*pos).second << " ";}std::cout << std::endl;}