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
}
根据标准 std::list 实现没有“+=”运算符的双向迭代器 http://www.cplusplus.com/reference/iterator/BidirectionalIterator/
我知道您不能将迭代器与 "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
}
根据标准 std::list 实现没有“+=”运算符的双向迭代器 http://www.cplusplus.com/reference/iterator/BidirectionalIterator/