map中operator[]插入的指针类型的值是否总是NULL?
Is inserted value of pointer type by operator[] in map always NULL?
以下代码的预期行为是什么,
#include <map>
...
std::map<int, A *> myMap;
myMap[0];
if(myMap[0] == NULL) {// true or false?
}
if 语句的计算结果是否为真?
插入的值将被初始化为空指针。
std::map::operator[] will perform an insertion if key does not exist; the mapped value will be value-initialized, for pointer type it's zero-initialization,结果为空指针。
顺便说一句:最好使用 nullptr(C++11 起)而不是 NULL
。
以下代码的预期行为是什么,
#include <map>
...
std::map<int, A *> myMap;
myMap[0];
if(myMap[0] == NULL) {// true or false?
}
if 语句的计算结果是否为真?
插入的值将被初始化为空指针。
std::map::operator[] will perform an insertion if key does not exist; the mapped value will be value-initialized, for pointer type it's zero-initialization,结果为空指针。
顺便说一句:最好使用 nullptr(C++11 起)而不是 NULL
。