std::list<int> myList;
std::list<int> myIter = myList.begin();
迭代器的值与使用myList.end()
. 迭代器被初始化到结束位置。即使您将一个元素推入列表后,迭代器仍然指向过去的一端。如果你增加它,你正在调用未定义的行为。
更新:
例如,如果您使用带有 -D_GLIBCXX_DEBUG 的 GCC 编译代码片段,则生成的可执行文件将中止:
/usr/include/c++/4.6/debug/safe_iterator.h:236:error: attempt to increment
a past-the-end iterator.
Objects involved in the operation:
iterator "this" @ 0x0x7fffc9548fb0 {
type = N11__gnu_debug14_Safe_iteratorINSt9__cxx199814_List_iteratorIiEENSt7__debug4listIiSaIiEEEEE (mutable iterator);
state = past-the-end;
references sequence with type `NSt7__debug4listIiSaIiEEE' @ 0x0x7fffc9548fb0
}
zsh: abort (core dumped) ./listiter