缺少std :: vector :: erase()的const_iterator重载与g ++ 4.8 [英] Missing const_iterator overload of std::vector::erase() with g++ 4.8

查看:525
本文介绍了缺少std :: vector :: erase()的const_iterator重载与g ++ 4.8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下示例无法使用g ++ 4.8.2编译:

The following example will not compile using g++ 4.8.2:

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

int main() {
    vector<int> v {1, 2, 3};

    v.erase(v.cbegin()); // Compiler complains

    return 0;
}

编译器说如下。 (它不是很可读,但它抱怨在向量< int> :: const_iterator 向量< int> :iterator 。)

The compiler says the following. (It isn't very readable, but it's complaining that there's not a known conversion between vector<int>::const_iterator and vector<int>::iterator.)

prog.cpp: In function ‘int main()’:
prog.cpp:8:20: error: no matching function for call to ‘std::vector<int>::erase(std::vector<int>::const_iterator)’
  v.erase(v.cbegin());
                    ^
prog.cpp:8:20: note: candidates are:
In file included from /usr/include/c++/4.8/vector:69:0,
                 from prog.cpp:2:
/usr/include/c++/4.8/bits/vector.tcc:134:5: note: std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(std::vector<_Tp, _Alloc>::iterator) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = int*]
     vector<_Tp, _Alloc>::
     ^
/usr/include/c++/4.8/bits/vector.tcc:134:5: note:   no known conversion for argument 1 from ‘std::vector<int>::const_iterator {aka __gnu_cxx::__normal_iterator<const int*, std::vector<int> >}’ to ‘std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}’
/usr/include/c++/4.8/bits/vector.tcc:146:5: note: std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(std::vector<_Tp, _Alloc>::iterator, std::vector<_Tp, _Alloc>::iterator) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = int*]
     vector<_Tp, _Alloc>::
     ^
/usr/include/c++/4.8/bits/vector.tcc:146:5: note:   candidate expects 2 arguments, 1 provided

C ++ 11标准在§23.3.6.5中明确指出 vector :: erase 函数接受 const_iterator 。 (转译为此处这里。)

Why? The C++11 standard plainly states, in §23.3.6.5, that the vector::erase function takes a const_iterator. (Paraphrases are here and here.)

有什么好的解决方法,必须使用 const_iterator

What's a good workaround, assuming that I must use a const_iterator?

推荐答案

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57158

擦除需要一个迭代器,而不是当前gcc的const_iterator。

erase requires an iterator instead of a const_iterator with current gcc's.

这篇关于缺少std :: vector :: erase()的const_iterator重载与g ++ 4.8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆