【发布时间】:2012-09-05 12:15:18
【问题描述】:
#include<iostream>
#include<list>
using namespace std;
void compute(int num)
{
list<int> L;
list<int>::iterator i;
list<int>::iterator i2;
int p;
cout<<"Enter the number of numbers\n";
cin>>p;
int a;
for(int k=1;k<=p;k++)
{
cin>>a;
L.push_back(k);
}
cout<<endl;
for(i=L.begin() ; i!=L.end() ; ++i)
{
cout<<*i<<endl;
}
long int k=1;
for(i=L.begin() ; i!=L.end() ; ++i )
{
if(k%2!=0) //This is where I try and delete values in odd positions
{
i2=L.erase(i);
}
k++;
}
for(i=L.begin() ; i!=L.end() ; ++i )
{
cout<<*i<<endl;
}
}
int main()
{
// int testcases, sailors;
//cin>>testcases;
//for(int i=1 ; i<=testcases ; i++)
{
// cin>>sailors;
}
//for(int i=1;i<=testcases;i++)
{
// int num;
//cin>>num;
//compute(num);
}
compute(0);
return 0;
}
我正在尝试使用列表中的 L.erase() 函数擦除元素。但我得到一个错误说 “调试断言失败!......表达式:列表迭代器不可增加” 但是我们可以增加迭代器吗?
【问题讨论】:
-
你是在擦除后删除吗?
-
在哪里你得到“断言”?使用调试器找到它。如果您想知道何时发生,请逐行浏览代码。
-
为什么要将L.erase返回值赋给i2?
标签: c++ list visual-c++ stl listiterator