简体   繁体   English

擦除获取const_iterator,但通过迭代器调用(非const)

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

In C++11 std::vector::erase gets as first argument const_iterator (older one was with iterator): 在C ++ 11中, std::vector::erase作为第一个参数const_iterator (旧参数与迭代器一起使用):

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

But as you can see in the example it uses with not cbegin . 但是正如您在示例中看到的那样,它与cbegin Instead it uses with begin . 相反,它与begin使用。 Is there an implicit conversion between const_iterator and iterator ? const_iteratoriterator之间是否存在隐式转换?

The requirement on containers is that iterator type must be implcitly convertible to const_iterator . containers的要求是, iterator类型必须隐式转换为const_iterator

See: http://en.cppreference.com/w/cpp/concept/Container 请参阅: http : //en.cppreference.com/w/cpp/concept/Container

So, something like below will work: 因此,类似下面的内容将起作用:

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

Yes, iterator of std containers is (and must be) convertible to const_iterator . 是的,std容器的iterator可以(并且必须)可以转换为const_iterator

From the standard, $23.2.1/4 General container requirements [container.requirements.general] Table 100 — Container requirements : 根据标准, $ 23.2.1 / 4常规容器要求[container.requirements.general]表100 —容器要求

(Emphasis mine) (强调我的)

X::iterator X:迭代

any iterator category that meets the forward iterator requirements. 满足正向迭代器要求的任何迭代器类别。 convertible to X::const_iterator . 可转换为X :: const_iterator

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM