使用std :: vector :: erase和const_iterators

hov*_*ovo 0 c++ iterator vector c++11

我有一个返回的函数vector<string>::const_iterator.当我尝试使用此返回值作为参数vector<string>::erase,我得到一个错误之间的转换const_iteratoriterator是不可能的.有没有办法在不改变函数的返回值类型的情况下绕过这个问题?谢谢!

Cas*_*sey 5

在C++ 11中,两种变体都是std::vector::eraseaccept const_iterator,尽管在早期版本的标准中并非如此(C++11§23.3.6.5[vector.modifiers]).您可能(a)没有将代码编译为C++ 11,或者(b)使用尚未完全更新为C++ 11的标准库.

解决方法非常简单:const_iterator ci如果引用了vector的成员v,则可以将等效项派生iteratorv.begin() + (ci - v.begin()).