You can briefly initialize vector<string> from a statically created char* array:
char* strarray[] = {"hey", "sup", "dogg"}; vector<string> strvector(strarray, strarray + 3);
This copies all lines, by the way, so you use memory twice. You can use Will Dean's suggestion to replace the magic number 3 here with arraysize (str_array) - although I remember there is some special case where this particular version of arraysize can do something bad (sorry, I canβt remember the details right away) But it very often works correctly.
Also, if you really play on one line, you can define a variable macro so that one line works, such as DEFINE_STR_VEC(strvector, "hi", "there", "everyone"); .
Tyler Aug 29 '08 at 21:21 2008-08-29 21:21
source share