如果是顺序容器,如vector,string,deque,删除,插入操作会影响当前还有以后的迭代器,节点容器map,set,list就不会
#include<iostream>
#include<memory>
#include<vector>
#include<map>
using namespace std;
int main()
{
map<int,int>mymap;
for(int i=1;i!=5;i++)
{
mymap[i] = i;
}
for(auto i = mymap.begin();i!=mymap.end();i++)
{
mymap.erase(i);
cout<<1<<endl;
}
return 0;
}
#include<iostream>
#include<memory>
#include<vector>
#include<map>
using namespace std;
int main()
{
vector<int>a = {1,2,3,4,5};
for(auto i = a.begin();i!=a.end();)
{
i = a.erase(i);
cout<<1<<endl;
}
return 0;
}