cpx cpx - 1 year ago 144
C++ Question

Initialize a vector array of strings

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
just to initialize and fill it with strings. Or should i just fill it in constructor if it can't be initialized like we do regular arrays.

Answer Source

Sort of:

class some_class {
    static std::vector<std::string> v; // declaration

const char *vinit[] = {"one", "two", "three"};

std::vector<std::string> some_class::v(vinit, end(vinit)); // definition

end is just so I don't have to write vinit+3 and keep it up to date if the length changes later. Define it as:

template<typename T, size_t N>
T * end(T (&ra)[N]) {
    return ra + N;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download