2

玩弄 C++ 标准库的 std::map 类,并注意到如果我删除一个元素,然后尝试引用它(下面代码中注释掉的行),该元素将返回值为 0。这是预期的吗? 你真的必须使用 find 函数来访问一个元素,而不会意外创建一个元素(如果它不存在)?

编译器设置:我正在使用 g++ i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1(基于 Apple Inc. build 5658)在 osx 10.8.3 上编译(LLVM build 2336.11.00)

using namespace std;

map<int,int> myMap;
map<int,int>::iterator it;

myMap[1] = 5;

for (it=myMap.begin(); it!=myMap.end(); ++it)
    std::cout << it->first << " => " << it->second << '\n';

printf("map test result: %d\n", myMap[1]);

it = myMap.find(1);
myMap.erase( it );

// If I uncomment this it will cause a 0 value to occur at key 1.
//printf("map test result: %d\n", myMap[1]);

if (myMap.find(1) == myMap.end())
    puts("element key 1 is null, ok");
else
    puts("element @ key 1 exists, error");

if (myMap.empty())
    puts("map is empty");
else
    puts("map is not empty, error");

for (it=myMap.begin(); it!=myMap.end(); ++it)
    std::cout << it->first << " => " << it->second << '\n';

只是为了澄清一下,如果我在第二个 printf 行注释的情况下运行它,它会按预期运行:

1 => 5
map test result: 5
element key 1 is null, ok
map is empty

如果我在未注释行的情况下运行,则在 printf 语句中访问 myMap[1] 会创建另一个元素,并留下以下结果:

1 => 5
map test result: 5
map test result: 0
element @ key 1 exists, error
map is not empty, error
1 => 0
4

2 回答 2

5

是的,这是应该做的operator[]std::map来自标准(C++11,§23.5.4.3):

mapped_type& operator[](const key_type& k);
mapped_type& operator[](key_type&& k);

[...]

效果:如果 unordered_map 还没有包含其键等于 k ​​的元素,则第一个运算符插入值value_type(k, mapped_type()),第二个运算符插入值value_type(std::move(k), mapped_type())

请注意,即使元素从未被插入然后被删除,也会发生这种情况。当您将元素访问operator[]应用到不存在的键时,使用简单地插入新的、默认构造的值。

如果你不想这样,最好使用 的find功能std::mapend如果键不存在,这将返回一个-iterator。

于 2013-06-11T04:24:03.500 回答
2

是的,这是预期的行为。除了阅读规范之外,您还可以从类型签名中推断出这一点:

T& operator[] (const key_type& k);

它无法判断您是否最终会分配给键,因为在操作员已经完成执行operator =之后,它会在其返回值上被调用。[]该方法也无法表示空值:它返回引用,而不是指针。

于 2013-06-11T04:31:27.127 回答