cpx cpx - 3 months ago 9
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
static
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

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;
}