Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Vectors initialization

#include <vector>
...
//inside main function
vector<int> vi3 = {42,42,42,42,42,42,42,42,42,42};

I'm learning C++, I thought it was possible to initialize a vector like this... Am I doing something wrong? I know about the other ways of initializing a vector. In the book I'm reading it says it can also be done like this:

vector<int> vi3{42,42,42,42,42,42,42,42,42,42};

It's the first thing on the book that gave me an error. What am I doing wrong?

like image 562
SadSeven Avatar asked Dec 27 '22 00:12

SadSeven


1 Answers

It's not clear which compiler you are using, but versions of Microsoft's Visual Studio before the 2013 preview do not support the uniform initialisation syntax {}

If you are using gcc you need to tell it to use C++11:

-std=c++0x
like image 160
doctorlove Avatar answered Jan 09 '23 01:01

doctorlove