c++11: erase using a const_iterator

核能气质少年 提交于 2019-12-19 16:47: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:


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



来源:https://stackoverflow.com/questions/15987893/c11-erase-using-a-const-iterator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!