1

Does c++ support either in the STL or there exists an external library supporting Arrays.asList()?

Typical usage

private ArrayList<String> lexeme = new ArrayList<String>(Arrays.asList(" ", ",", "(", ")", ";", "=", ".", "*", "-"));

I am using Visual Studio 11 (2012) and they have not included the c++11 feature Initializer lists leaving me in a quandry as to initialize a vector of nine unique strings without

std::vector<std::string>::push_back("a");
std::vector<std::string>::push_back("b");
std::vector<std::string>::push_back("c");
                 . . .
Mushy
  • 2,535
  • 10
  • 33
  • 54
  • I believe this is close to your question and problem: http://stackoverflow.com/questions/7050485/implementing-a-stdarray-like-container-with-a-c11-initializer-list?rq=1 – quetzalcoatl Apr 08 '13 at 18:11

2 Answers2

3

A common thing to do before C++11 was to first create an array, then initialize the vector with it, for example:

char const * arr[] = { " ", ",", "(", ")", ";", "=", ".", "*", "-" };
std::vector<std::string> str_vec(arr, arr + sizeof(arr) / sizeof(*arr));

Of course, VS11 does support some of C++11, so you can do this instead, which is slightly more readable:

char const * arr[] = { " ", ",", "(", ")", ";", "=", ".", "*", "-" };
std::vector<std::string> str_vec(std::begin(arr), std::end(arr));
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
1

You could use:

const char* arr[] = {"a", "bc", "def"};
std::vector<std::string> vec(std::begin(arr), std::end(arr));

If your compiler doesn't support std::begin() and std::end(), they are easy to do without:

std::vector<std::string> vec(arr, arr + sizeof(arr) / sizeof(*arr));
NPE
  • 486,780
  • 108
  • 951
  • 1,012