Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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++?

like image 493
Dataflashsabot Avatar asked Sep 16 '10 12:09

Dataflashsabot


1 Answers

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")...

like image 162
Johannes Schaub - litb Avatar answered Oct 27 '22 02:10

Johannes Schaub - litb