Easily initialise an std::list of std::strings?

Dataflashsabot picture Dataflashsabot · Sep 16, 2010 · Viewed 9k times · Source

In C++0x, what I want would be:

std::list<std::string> colours = {"red", "blue", "green", "grey", "pink", "violet"};

What's the easiest way in standard, non-0x C++?

Answer

Johannes Schaub - litb picture Johannes Schaub - litb · Sep 16, 2010
char const *x[] = {"red", "blue", "green", "grey", "pink", "violet"};
std::list<std::string> colours(x, x + sizeof(x) / sizeof(*x));

Or you can use the boost libraries and functions like list_of("a")("b")...