vector :
iter = container.erase(iter); //erase的返回值是删除元素下一个元素的迭代器
vector<int>::iterator it = a.begin();
for(;it !=a.end();)
{
printf("%d
",*it);
it = a.erase(it);
}
map:
dataMap.erase(iter++)
注:map erase 没有返回下一个迭代器的方法
#include <cstdio> #include <iostream> #include <vector> #include <map> using namespace std; int main(int argc, char *argv[]) { vector<int>a; a.push_back(1); a.push_back(2); a.push_back(3); a.push_back(4); vector<int>::iterator it = a.begin(); for(;it !=a.end();) { printf("%d ",*it); it=a.erase(it); } map<int,int>b; b[1]=1; b[2]=2; b[3]=3; b[4]=4; map<int,int>::iterator it1 = b.begin(); for(;it1!=b.end();) { printf("%d ",it1->first); b.erase(it1++); } }