erase()删除参数中迭代器所指的值,使迭代器无效化,返回值为迭代器所指的下一个迭代器,并且后面的元素不会前移覆盖所删除的位置
输出为2 3 4 5 6 7 8 9和一个未知数
#include<iostream>
#include<list>
using namespace std;
int main()
{
list<int> l;
for(int i=1;i <= 10;i++)
{
l.push_back(i);
}
for(list<int>::iterator it = l.begin();;)
{
if(it != l.end())
{
it = l.erase(l.begin());
cout<<*it<<endl;
}
}
return 0;
}
正确使用方法1 std::list< int> List;
std::list< int>::iterator itList;
for( itList = List.begin(); itList != List.end(); )
{
if( WillDelete( *itList) )
{
itList = List.erase( itList);
}
else
itList++;
}
正确使用方法