Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何从列表中删除奇数位置? #包括 #包括 使用名称空间std; 无效计算(整数) { 清单L; 列表::迭代器i; 列表::迭代器i2; INTP; coutp; INTA; 对于(int k=1;k>a; L.推回(k); } 从技术上讲,在这种情况下不能_C++_List_Visual C++_Stl_Listiterator - Fatal编程技术网

C++ 如何从列表中删除奇数位置? #包括 #包括 使用名称空间std; 无效计算(整数) { 清单L; 列表::迭代器i; 列表::迭代器i2; INTP; coutp; INTA; 对于(int k=1;k>a; L.推回(k); } 从技术上讲,在这种情况下不能

C++ 如何从列表中删除奇数位置? #包括 #包括 使用名称空间std; 无效计算(整数) { 清单L; 列表::迭代器i; 列表::迭代器i2; INTP; coutp; INTA; 对于(int k=1;k>a; L.推回(k); } 从技术上讲,在这种情况下不能,c++,list,visual-c++,stl,listiterator,C++,List,Visual C++,Stl,Listiterator,当您使用erase()时,会删除指向的节点,因此实际上会使所使用的迭代器无效。因此,当您增加它时,它是未定义的行为 最好创建第二个列表,其中只包含要删除的位置的迭代器,然后可以循环这些位置并在之后调用erase。您不会从第二个列表中删除迭代器,因此它可以工作 大概是这样的: #include<iostream> #include<list> using namespace std; void compute(int num) { list<int> L; l

当您使用erase()时,会删除指向的节点,因此实际上会使所使用的迭代器无效。因此,当您增加它时,它是未定义的行为

最好创建第二个列表,其中只包含要删除的位置的迭代器,然后可以循环这些位置并在之后调用erase。您不会从第二个列表中删除迭代器,因此它可以工作

大概是这样的:

#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;
列表删除列表;
//使用原始列表中的其他元素填充deleteList。
for(List::iterator iter=deleteList.begin();
iter!=deleteList.end;++iter)
{
原始列表删除(*iter);
}

除了wOOte所说的,您可能希望使用反向迭代器来解决这个问题。

擦除
使作为参数传入的迭代器无效-因为迭代器指向的位置的元素刚刚被擦除!在同一迭代器上,在c中的下一个for循环中尝试递增颂歌!这就是它失败的原因

但是,擦除它将返回一个指向新位置的迭代器,我们可以使用该迭代器;因此,从STL容器中擦除某些内容的循环应如下所示;我用您使用的类型列表显示它,但您也可以使用例如vector:

List<IteratorType> deleteList;

//Populate deleteList with every other element from original list.

for (List<IteratorType>::iterator iter = deleteList.begin();
         iter !=deleteList.end; ++iter)
{
    originalList.erase(*iter);
}

在您的情况下,如果不是不可能的话,这将很难使用,因为
谓词将需要状态信息(索引是奇数还是偶数的信息)因此,我建议使用上面提到的循环结构;请记住,如果在删除某个谓词返回true的所有元素的一般情况下,迭代器
I
通过调用
erase
无效;但是,在
for
循环的下一次迭代中,您尝试增加这是无效的

试一试

相反-只有在不擦除的情况下才增加迭代器(擦除基本上“推进”迭代器,因为它会生成一个迭代器到所擦除元素后面的元素)

实际上,您可以利用
erase
的这种行为来编写函数,而无需
k

for(i=L.begin() ; i!=L.end() ; )
{
    if(k%2!=0) //This is where I try and delete values in odd positions
    {
        i=L.erase(i);
    } else {
        ++i;
    }
    k++;
}

因此,删除第一个元素,跳过第二个元素,删除第三个元素,依此类推。

删除后是否删除?从何处获得“断言”?使用调试器查找它。如果您想知道它何时发生,请逐行遍历代码。为什么要将L.erase返回值指定给i2?erase/remove更为惯用,但在这种情况下,谓词需要某种状态—这种状态很快就会变得难看(和更多代码)“比普通循环好得多。@FrerichRaabe true,谢谢,我添加了相应的提示;但这就是为什么我在开始时也给出了循环形式;)
remove\u如果这里不太可靠的话。无法保证它将在整个范围内按顺序应用谓词,因此跟踪索引是奇数还是偶数是不可能的,或者至少是不可移植的。@BenjaminLindley这就是为什么我添加了“如果可能”部分,以及下面的注释;)。我只是觉得知道替代方法很好——对于一般情况,remove_if非常有用,感谢nyarlathotep和everyone:)+1——没有在std::list中尝试过,但这通常是我在其他语言中使用动态列表容器类型时所做的。
container.erase(std::remove_if(L.begin(), L.end(), predicate), L.end());
for(i=L.begin() ; i!=L.end() ; )
{
    if(k%2!=0) //This is where I try and delete values in odd positions
    {
        i=L.erase(i);
    } else {
        ++i;
    }
    k++;
}
i = L.begin();
while ( i != L.end() ) {
    i = L.erase( i );      // Delete one
    if ( i != L.end() ) {  // Skip next, if there's an element
      ++i;
    }
}