map.erase(map.end())? [英] 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!
}
(OK,所以标题谈论擦除end find
(OK, so the title talks abouting erasing an end() iterator, but find will return end() for a non-existent key.)
为什么要擦除不存在的键,但是擦除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.
擦除(it)
(其中 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屋!