繁体   English   中英

C ++列表迭代器算法

[英]C++ list iterator arithmetic

我知道您不能以“ it + n”的形式使用带列表的迭代器,但是为什么当我使用++ it时程序可以编译,即:

//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/没有“ + =”运算符

暂无
暂无

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

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