【问题标题】:Missing const_iterator overload of std::vector::erase() with g++ 4.8使用 g++ 4.8 缺少 std::vector::erase() 的 const_iterator 重载
【发布时间】:2016-04-26 20:42:02
【问题描述】:

following example 将无法使用 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;
}

编译器会这样说。 (它的可读性不是很好,但它抱怨vector&lt;int&gt;::const_iteratorvector&lt;int&gt;::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。 (释义为herehere。)

假设我必须使用const_iterator,有什么好的解决方法?

【问题讨论】:

  • 它还没有实现。
  • 这很烦人...问题已编辑;有什么好的解决方法?
  • 解决方法是使用非常量迭代器。
  • begin代替cbegin?
  • @WeaselFox 这对于 const 地图对象是不可能的(至少在没有强制转换的情况下是不可能的)

标签: c++ c++11 stl g++


【解决方案1】:

这是 gcc 中的一个已知错误:http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57158

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

【讨论】:

  • 嗯。该错误被标记为已解决错误的重复,但针对deque
  • @thirtythreeforty “目标里程碑:4.9.0”已解决并不意味着“在每个版本中都已修复”。
  • @thirtythreeforty 看到你需要破解特定的编译器和库版本,你可以查看他们的数据布局,看看你是否可以通过指针reinterpret_cast(或使用union { const_iterator ci; iterator i; }) .
  • 请注意,我可以使用reinterpret_cast(我试过了,它有效),但是这绝对是一个hack,如果需要可靠性/便携性,则不要使用。
  • reinterpret_cast 是未定义的行为,这样做违反了基于类型的别名分析,并可能导致奇怪的结果。
【解决方案2】:

你可以通过指针算法得到一个非常量迭代器,这里有一个辅助函数来做到这一点:

template<typename T>
  typename std::vector<T>::iterator
  const_iterator_cast(std::vector<T>& v, typename std::vector<T>::const_iterator iter)
  {
    return v.begin() + (iter - v.cbegin());
  }

这样使用:

std::vector<T> v(1);
auto citer = v.cbegin();
v.erase( const_iterator_cast(v, citer) );

【讨论】:

  • @rabensky:试试return std::next(v.begin(), std::distance(v.cbegin(), iter));
  • @rabensky 这很好,因为这个问题专门针对std::vector 并且具有随机访问迭代器。并且辅助函数显然接受std::vector&lt;T&gt;&amp; 类型的参数,所以它只适用于std::vector
猜你喜欢
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-06
  • 2015-03-24
  • 2015-04-01
相关资源
最近更新 更多