89

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.

3
  • Initialize it with what, exactly? There are of course a myriad of ways to initialize it. Commented Nov 24, 2010 at 16:15
  • static doesn't "fill it with strings". The std::vector is a dynamic data structure and is created empty. Commented Nov 24, 2010 at 16:15
  • static in this context means multiple instances of your class all share the same v, is that what you really want? Commented Nov 24, 2010 at 16:15

9 Answers 9

98

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

Sign up to request clarification or add additional context in comments.

Comments

68

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

8 Comments

can i just use int numElements = sizeof(vinit)/4;
@cpx: if sizeof(char*) is 4, yes. Not so much on a 64bit machine. I just got bored of typing a bunch of sizeof stuff every time I iterate an array. My end function has the same effect for arrays as std::end in C++0x does.
In case it isn't obvious, when I say "my" end function, I mean the function template in my answer, in contrast with C++0x's std::end. I don't claim to have invented it!
@SteveJessop can you explain how end works? I cant figure it out. It takes N also as an argument but you dont give to it....
@RoozbehG: N is deduced as 3 from the type of the argument vinit, which is const char* [3]. For that matter, T is also deduced as const char * from that same type. So writing end(vinit) is like writing end<const char*, 3>(vinit) thanks to template parameter deduction.
|
36

If you are using cpp11 (enable with the -std=c++0x flag if needed), then you can simply initialize the vector like this:

// static std::vector<std::string> v;
v = {"haha", "hehe"};

Comments

15
 const char* args[] = {"01", "02", "03", "04"};
 std::vector<std::string> v(args, args + 4);

And in C++0x, you can take advantage of std::initializer_list<>:

http://en.wikipedia.org/wiki/C%2B%2B0x#Initializer_lists

Comments

11

MSVC 2010 solution, since it doesn't support std::initializer_list<> for vectors but it does support std::end

const char *args[] = {"hello", "world!"};
std::vector<std::string> v(args, std::end(args));

Comments

5

same as @Moo-Juice:

const char* args[] = {"01", "02", "03", "04"};
std::vector<std::string> v(args, args + sizeof(args)/sizeof(args[0])); //get array size

2 Comments

doesn't this assume all args[] in the init list are the same size?
@jwillis0720: all args[n] are actually of the same size, sizeof(char*) to be precise. The reason is that args stores a pointer (char*) to the "thing", not the "thing" itself.
2

Take a look at boost::assign.

Comments

1

In C++0x you will be able to initialize containers just like arrays

http://www2.research.att.com/~bs/C++0xFAQ.html#init-list

Comments

0

Main thing is static will not help in initializing and filling the vector with strings.

But can be initialized like mentioned below when it is declared. (There are some more examples in other answers)

//JUST AN EMPTY VECTOR, BUT THAT CAN HOLD std::string type.
std::vector<std::string> vecStr;

//AN INITIALIZED VECTOR OF TWO EMPTY STRINGS.
std::vector<std::string> vecString(2);

//AN INITIALIZED VECTOR OF TWO STRINGS WITH "testing"
std::vector<std::string> anotherVecString(2, "testing");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.