64,646
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <list>
using namespace std;
int main()
{
int ia[] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89};
list<int> ilist (ia, ia+11);
int nLen = ilist.size();
list<int>::iterator it = ilist.begin();
for(int index=0; index < nLen; index++)
{
if((index+1)%2 != 0)
{
ilist.erase(it);
}
it++;
}
return 0;
}
#include <iostream>
#include <list>
using namespace std;
int main()
{
int ia[] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89};
list<int> ilist (ia, ia+11);
int nLen = ilist.size();
list<int>::iterator it = ilist.begin();
list<int>::iterator it2 = it;
for(int index=0; index < nLen; index++)
{
list<int>::iterator it3 = (++it2);
cout<<(*it3)<<endl;
if((index+1)%2 != 0)
{
ilist.erase(it);
}
it = it3;
it2 = it3;
list<int>::iterator itmp= ilist.begin();
for(;itmp !=ilist.end(); itmp++)
{
cout<<(*itmp)<<" ";
}
cout<<endl;
}
it = ilist.begin();
for(;it !=ilist.end(); it++)
{
cout<<(*it)<<" ";
}
return 0;
}
这样就对了,但是具体不知道第一次代码问题的原因。