views:

66

answers:

1

If it even exists, what would a std::map extended initializer list look like?

I've tried some combinations of... well, everything I could think of with GCC 4.4, but found nothing that compiled.

+5  A: 

It exists and works well:

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

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

EDIT:

Also see the first comment below, it provides a nice shortcut that makes initializer list much handier.

doublep
With unified initialization with std::pair, the code becomes std::map <int, std::string> x { { 45, "foo" }, { 3, "bar" } };
snk_kid
Awesome, this makes it very nice stylewise. I might just "drop" support for MSVC 2010 to be able to use this with GCC :).
rubenvb