erase 获取 const_iterator 但使用迭代器调用(非常量)

erase gets const_iterator but is called with iterator (non-const)

在 C++11 中,std::vector::erase 作为第一个参数 const_iterator(较旧的是迭代器):

http://www.cplusplus.com/reference/vector/vector/erase/

但是正如您在示例中看到的那样,它使用了 not cbegin。相反,它与 begin 一起使用。 const_iteratoriterator之间是否存在隐式转换?

containers 的要求是 iterator 类型必须可以隐式转换为 const_iterator

参见:http://en.cppreference.com/w/cpp/concept/Container

所以,像下面这样的东西会起作用:

std::vector<int> v;
.....
std::vector<int>::const_iterator cit = v.begin();

是的,iterator 个 std 容器可以(并且必须)转换为 const_iterator

根据标准,.2.1/4 General container requirements [container.requirements.general] Table 100 — Container requirements

(强调我的)

X::iterator

any iterator category that meets the forward iterator requirements. convertible to X::const_iterator.