c++11: erase using a const_iterator

2019-02-21 18:11发布

I believe that since C++11, the erase function of most containers (e.g. std::vector) accepts a const_iterator as parameter:

iterator erase (const_iterator position);

Still my compilers (GCC 4.8 and Clang 3.2, both using GCC libstdc++) won't allow me to use such function, even when compiling with --std=c++11.

Is it a compiler/libstdc++ bug, or did I do something bad? This is a sample code:

#include <vector>

int main( )
{
    std::vector<int> v;
    v.push_back( 1 );
    v.push_back( 2 );
    v.push_back( 3 );

    std::vector<int>::const_iterator i = v.begin();
    while( i != v.end() ) {
        i = v.erase( i );
    }

    return 0;
}

1条回答
beautiful°
2楼-- · 2019-02-21 18:51

This issue is documented here and it's reported as a partial implementation for now.

CTRL + F with your browser and search for N2350.

If you are on Linux it's possible to build a development version of the libcxx library from the LLVM project that you can download from here; I don't know if this solves any of the issues that you are experiencing but I'm proposing it as an alternative to the libstdc++.

查看更多
登录 后发表回答