Does std::list::clear invalidate std::list::end iterator?

前端 未结 3 1449
眼角桃花
眼角桃花 2021-02-20 01:01

Check this code:

#include \"stdafx.h\"
#include 

int _tmain(int argc, _TCHAR* argv[])
{
    std::list mylist;
    mylist.push_back(1);
          


        
3条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-20 01:56

    This is in fact invalid. Iterators are only valid on the current state container. Once you add or remove items, the iterator is no longer valid.

    The article you link does not say what you are doing is valid. They get a new iterator after the clear.

    The reason it doesn't show up in release code is because the debugging that picks up the issue is disabled.

提交回复
热议问题