简体   繁体   中英

c++11: erase using a const_iterator

I believe that since C++11, the erase function of most containers (eg 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;
}

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++ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM