Declare MAP already passing values in C ++?

1

Good luck, I see that with the array I can do this:

int array[]= {5,6,4,2};

My question is, in the language C ++ I can declare the map already passing values?

Example:

map<string,int> mymap = { "a",1};
    
asked by anonymous 08.08.2018 / 22:53

1 answer

3

In fact, your initialization example for map was {} to be right!

Just enough:

map<string,int> mymap = {{"a",1}};
//                      ^       ^

The question is what the first pair of {} represents the map integer and then takes other {} for each initialized element.

So if you wanted to boot with more elements could do:

map<string,int> mymap = {{"a",1}, {"b",2}, {"c",3}};

It's worth mentioning that the support is from C ++ 11

Documentation

    
08.08.2018 / 23:49