在使用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/