Reputation: 5069
When using unordered_map to map to a structure, wonder if the object mapped to is initialized by the system. I am using Ubuntu 12.04 64bit with g++ 4.7.3. Thanks.
#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;
}
Upvotes: 1
Views: 95
Reputation: 172894
If an insertion is performed when using std::unordered_map::operator[], the mapped value is 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.
Upvotes: 1
Reputation: 15824
You are initializing your std::unordered_map
mymap
in following lines
mymap["john"].a[0] = 10;
mymap["john"].a[1] = 20;
mymap["john"].a[2] = 30;
Upvotes: 0