Reputation: 31
Hi I am a newbie in C++ and I just cannot initialize a vector using {}, even if the code is copied with a book. For example, when I do these
vector <string> articles {"a", "an", "the"};
And
vector <string> articles = {"a", "an", "the"};
I got these error message respectively:
Error: expected a ";"
And
Error: initialization with "{...}" is not allowed for object of type "std::vector<std::string, std::allocator<std::string>>"
Would anyone help me out? I believe it should be a simple mistake that I couldn't find out.
Upvotes: 3
Views: 3768
Reputation: 23624
uniform initialization
is introduced since C++11, You should use a recent compiler that supports this new feature.
If your compiler does not support this feature, you may try the following:
string arrOfString[3] = {"a", "an", "the"};
vector<string> articles(arrOfString, arrOfString +3);
EDIT:
with MSVC11, you can do (by courtesy of @chris):
string arrOfString[3] = {"a", "an", "the"};
vector<string> articles(std::begin(arrOfString), std::end(arrOfString));
Upvotes: 2