std::list<int> myList;
std::list<int> myIter = myList.begin();
イテレータの値は、myList.end()
で初期化した場合と同じです。イテレータは、終わりを過ぎた位置に初期化されます。要素をリストにプッシュした後でも、イテレータはまだ1つの終わりを指しています。インクリメントすると、未定義の動作が呼び出されます。
UPDATE:
例えば、あなたが-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
これは私に、実際には、本当に考えさせてくれたので、これは良い質問だと思います。それはリストです...おそらくこれはうまくいくかもしれません...(それはもちろんありません) – jcoder