What does the list of extended initializers std :: map look like?

If it exists, what would an extended list of std::map initializers look like?

I tried some combinations ... well, all that I could think of with GCC 4.4, but did not find anything that compiled.

+80
c ++ dictionary c ++ 11 initializer-list
Jul 14 '10 at 20:19
source share
1 answer

It exists and works well:

 std::map <int, std::string> x { std::make_pair (42, "foo"), std::make_pair (3, "bar") }; 

Remember that the map value type is pair <const key_type, mapped_type> , so you basically need a list of pairs with the same or convertible types.

Unified initialization with std :: pair makes code even easier

 std::map <int, std::string> x { { 42, "foo" }, { 3, "bar" } }; 
+133
Jul 14 '10 at 20:55
source share



All Articles