c++ - objected映射到unordered_map中,是系统初始化的吗?

标签 c++ unordered-map

在使用unordered_map映射到结构体时,想知道映射到的对象是否被系统初始化了。我正在使用带有 g++ 4.7.3 的 Ubuntu 12.04 64 位。谢谢。

#include <iostream>
#include <unordered_map>

struct strDum {
    uint a[3];
    int id;
};

int main (int argc, char *argv[])
{
    int i;
    char cmd[100];
    std::unordered_map<std::string,strDum> mymap;
    mymap["john"].a[0] = 10;
    mymap["john"].a[1] = 20;
    mymap["john"].a[2] = 30;
    printf("%d\n", mymap[argv[1]].a[1]);
    return 0;
}

最佳答案

如果在使用 std::unordered_map::operator[] 时执行插入, 映射值为 value-initialized .

Returns a reference to the value that is mapped to a key equivalent to key, performing an insertion if such key does not already exist.

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.

关于c++ - objected映射到unordered_map中,是系统初始化的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33194030/

相关文章:

c++ - 状态机状态之间的通信

c++ - 如何在迭代无序 map 时从无序 map 中删除多个项目?

c++ - unordered_map 哈希函数返回

c++ - unordered_map 储备的行为

c# - 从控制句柄获取进程

c++ - Armadillo 和 superLU 错误 : "use of SuperLU must be enabled" and undefined references

C++ - 将 Complex 分配给 double

c++ - 没有为类引用定义哈希?

c++ - 这是否因为两次查找而不是一次查找而变慢?

c++ - 无法与另一个项目共享头文件