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.
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 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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With