std :: list :: clear是否会使std :: list :: end迭代器无效? [英] Does std::list::clear invalidate std::list::end iterator?

查看:190
本文介绍了std :: list :: clear是否会使std :: list :: end迭代器无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检查此代码:

#include "stdafx.h"
#include <list>

int _tmain(int argc, _TCHAR* argv[])
{
    std::list<int> mylist;
    mylist.push_back(1);
    std::list<int>::iterator i = mylist.end();
    if( i == mylist.end() )
        printf( "end is end\n" );

    mylist.clear();
    if( i == mylist.end() )
        printf( "never get here because Microsoft seems to "
                "think the iterator is no longer safe.\n" );

    return 0;
}

现在,根据 cplusplus.com 来说,这不应该是问题,并且在发布模式下,我认为这很好,并且实际上不会引起任何问题,但是调试变得不可能,因为这在不让我继续的情况下无法解决.有指针吗?

Now, according to cplusplus.com this shouldn't be a problem, and in release mode, I think this is fine and doesn't cause any issues really, but debugging becomes impossible as this just bails without letting me continue. Any pointers?

推荐答案

其他答案指出,通常,您不能依赖于清除容器时容器的过去的迭代器仍然有效.但是,列表的过去迭代器确实应该仍然有效:

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 效果:仅使迭代器和对已删除元素的引用无效.

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.

这篇关于std :: list :: clear是否会使std :: list :: end迭代器无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆