玩弄 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