C ++列表迭代器算术

时间:2015-08-02 05:04:18

标签: c++ c++11 iterator containers arithmetic-expressions

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

//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
}

1 个答案:

答案 0 :(得分:1)

根据标准std :: list实现的双向迭代器http://www.cplusplus.com/reference/iterator/BidirectionalIterator/没有&#34; + =&#34;操作