簡體   English   中英

擦除獲取const_iterator,但通過迭代器調用(非const)

[英]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/

但是正如您在示例中看到的那樣,它與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();

是的,std容器的iterator可以(並且必須)可以轉換為const_iterator

根據標准, $ 23.2.1 / 4常規容器要求[container.requirements.general]表100 —容器要求

(強調我的)

X:迭代

滿足正向迭代器要求的任何迭代器類別。 可轉換為X :: const_iterator

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM