简体   繁体   中英

C++ list iterator arithmetic

I am aware that you cannot use iterators with list in the form "it +n" but why is that when I use ++it the program is able to compile ie:

//program compiles
list<int> v {1,2,3,4}; 
auto begin = v.begin(),
end = v.end(); 
while (begin != end) {
    ++begin;  
    begin = v.insert(begin, 42); 
    ++begin;  // advance begin past the element we just added
}

//program doesn't compile
list<int> v{1,2,3,4}; 
auto begin = v.begin(),
end = v.end(); 
while (begin != end) {
    begin+=1; //or alternatively begin = begin +1 
    begin = v.insert(begin, 42);  // insert the new value
    ++begin;  // advance begin past the element we just added
}

根据standart std :: list实现的双向迭代器http://www.cplusplus.com/reference/iterator/BidirectionalIterator/没有“ + =”运算符

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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