64,654
社区成员
发帖
与我相关
我的任务
分享
list<int> lst;
lst.push_back(1);
for (list<int>::iterator it = lst.begin();it!=lst.end();++it)
{
list<int>::iterator itemp = it;
--it;
lst.erase(itemp);
}
#include <list>
#include <iostream>
using namespace std;
int main()
{
list<int> lst;
lst.push_back(1);
lst.push_back(1);
lst.push_back(1);
lst.push_back(1);
lst.push_back(1);
list<int>::iterator it1 = lst.begin(),
it2 = lst.end();
for (;it1 != it2;)
{
list<int>::iterator temp = it1;
it1++;
lst.erase(temp);
}
cout << lst.size() << endl;
return 0;
}
for (list<int>::iterator it = lst.begin();it!=lst.end();/*++it*/)
{
if(删除元素的条件满足)
{
it = lst.erase(it);
}
else
{
++it;
}
}
list<int> lst;
lst.push_back(1);
for (list<int>::iterator it = lst.begin();it!=lst.end();++it)
{
it = lst.erase(it);
}
这两种会有问题么?