c.erase(p)无法编译成功,为什么?

0 c++

我曾尝试在列表中测试擦除功能,但我不能成功!我的编译器的错误消息是:

[Error]error: no matching function for call to
`std::list<int,std::allocator<int> >::erase(std::_List_const_iterator<int>&)'.
Run Code Online (Sandbox Code Playgroud)

我的代码如下:

#include <iostream>
#include <list>
using namespace std;

int main()
{
    int ia[] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89};
    list<int> ilst(ia, ia + 11);

    list<int>::const_iterator iter2 = ilst.begin();
    for(; iter2 != ilst.end(); ++iter2)
    {
        if(*iter2 % 2 != 0)
        {   
            iter2 = ilst.erase(iter2);  //error!
            --iter2;
        }
    }
    iter2 = ilst.begin();   
    for(; iter2 != ilst.end(); ++iter2)
    {
        cout << *iter << " ";
    }   
    cout << endl;
}
Run Code Online (Sandbox Code Playgroud)

谢谢你的导游!

Dew*_*wfy 6

看看这个示例,我认为你可以毫不费力地转移到list :: iterator而不是list :: const_iterator.这样编译的代码没有错误