Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize a vector array of strings

Tags:

c++

stl

Would it be possible to initialize a vector array of strings?

for example:

static std::vector<std::string> v; //declared as a class member 

I used static just to initialize and fill it with strings. Or should i just fill it in the constructor if it can't be initialized like we do with regular arrays.

like image 260
cpx Avatar asked Nov 24 '10 16:11

cpx


People also ask

How do you initialize a vector vector of strings in C++?

vector (size_type n, const value_type& val, const allocator_type& alloc = allocator_type()); It accepts the size of vector and an element as an argument. Then it initializes the vector with n elements of value val. Lets see an example that how to initialize a vector of std::string to 5 string objects with value “Hi”.

How do you Declare a vector string in C++?

How to Create a Vector of Strings in C++ The program begins with the inclusion of the iostream library, which is needed for keyboard input, and output to the terminal (screen). This is followed by the inclusion of the string library, which is needed for automatic composing of strings.


1 Answers

It is 2017, but this thread is top in my search engine, today the following methods are preferred (initializer lists)

std::vector<std::string> v = { "xyzzy", "plugh", "abracadabra" }; std::vector<std::string> v({ "xyzzy", "plugh", "abracadabra" }); std::vector<std::string> v{ "xyzzy", "plugh", "abracadabra" };  

From https://en.wikipedia.org/wiki/C%2B%2B11#Initializer_lists

like image 83
Alleo Avatar answered Sep 21 '22 13:09

Alleo