map.erase(map.end())? [英] map.erase( map.end() )?

查看:123
本文介绍了map.erase(map.end())?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑:

#include <map>

int main()
{
    std::map< int, int > m;
    m[ 0 ] = 0;
    m[ 1 ] = 1;

    m.erase( 0 );  // ok
    m.erase( 2 );  // no-op
    m.erase( m.find( 2 ) );  // boom!
}

(好的,所以标题谈论擦除一个end()迭代器,但是find将返回一个不存在的键的end()。)

(OK, so the title talks abouting erasing an end() iterator, but find will return end() for a non-existent key.)

为什么要擦除一个不存在的键OK,而擦除end()会爆炸。我在标准中看不到任何明确的提及?

Why is erasing a non-existent key OK, yet erasing end() blows up. I couldn't see any explicit mention of this in the standard?

我在VS2005(在调试配置中抛出异常)和GCC 4.0.1( 100%CPU)。是否依赖于实现?

I've tried this on VS2005 (throws an exception in debug configuration) and GCC 4.0.1 (100% CPU). Is it implementation dependent?

谢谢。

推荐答案

c $ c> erase(key),该标准表示所有具有值键的元素都被删除。当然可能没有这样的价值观。

For erase(key), the standard says that all elements with value key are removed. There may of course be no such values.

对于erase(it)(其中是一个 std :: map :: iterator ),标准表示它指出的元素被删除 - 不幸的是,如果它是 end() 它不指向一个有效的元素,并且您在未定义的行为中关闭,就像您使用 end()用于任何其他地图操作。有关详细信息,请参见第23.1.2节。

For erase(it) (where it is a std::map::iterator), the standard says that the element pointed to by it is removed - unfortunately, if it is end() it does not point to a valid element and you are off in undefined behaviour land, as you would be if you used end() for any other map operation. See section 23.1.2 for more details.

这篇关于map.erase(map.end())?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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