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

前端 未结 3 1426
眼角桃花
眼角桃花 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:46

    Other answers point out that, in general, you can't rely on a container's past-the-end iterator remaining valid when the container is cleared. However, the past-the-end iterator of a list should indeed remain valid:

    C++11 23.3.5.4/3 Effects: Invalidates only the iterators and references to the erased elements.

    The past-the-end iterator does not refer to any element, so should not be invalidated.

提交回复
热议问题