C++ list 迭代器中删除元素
#include <iostream>
#include <list>
#include <map>
#include <afxwin.h>
using namespace std;
int main ()
{
list<int> listInts;
listInts.push_back(1);
listInts.push_back(2);
listInts.push_back(3);
listInts.push_back(4);
listInts.push_back(5);
list<int>::iterator iter;
for(iter = listInts.begin();iter !=listInts.end();){
cout << *iter << endl;
if ( *iter == 3){
iter = listInts.erase(iter);
}
else{
iter++;
}
}
for(iter = listInts.begin();iter !=listInts.end();iter++){
cout << *iter << endl;
}
return 0;
}
std::map<int,int>::iterator iter;
for (iter=maps.begin();iter!=maps.end();)
{
if (expression) {
maps.erase(iter++);
}
else {
iter++;
}
}