c++ - 使用 operator[] 时映射中的默认指针值

标签 c++

<分区>

由于缺少索引而插入到映射中的指针是否始终为 NULL?

std::map<int, Foo*> myMap;
Foo* myFoo = myMap[0];

myFoo 是否保证为 NULL?

最佳答案

来自 cppreference :

If an insertion is performed, the mapped value is value-initialized (default-constructed for class types, zero-initialized otherwise) and a reference to it is returned

所以是的,这保证为 NULL

关于c++ - 使用 operator[] 时映射中的默认指针值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29758339/

相关文章:

c++ - 避免在树结构中使用强制转换

c++ - 用于 Visual Studio 2005 和 Visual Studio 2008 的诺基亚 QT 4

c++ - 刷新 cin 输入的正确方法

c++ - std::vector 与 C++ 中的原始数组有多相似?

c++ - OpenCV 无法从树莓派创建的 gstreamer 打开网络流

c++ - 帮助 c++ 模板模板

c++ - 使用 C++ boost 正则表达式

c++ - 在 Windows 中执行 system() 时未找到入口点

c++ - C++ 中 3 个类的多态性

c++ - 如何提示 Visual C++ 编译器优化器不太可能执行 if 语句的特定分支?