I want to initialize a vector like we do in case of an array.
Example
int vv[2] = {12, 43};
But when I do it like this,
vector<int> v(2) = {34, 23};
OR
vector<int> v(2);
v = {0, 9};
it gives an error:
expected primary-expression before ‘{’ token
AND
error: expected ‘,’ or ‘;’ before ‘=’ token
respectively.
With the new C++ standard (may need special flags to be enabled on your compiler) you can simply do:
Or even:
On compilers that don’t support this feature (initializer lists) yet you can emulate this with an array:
Or, for the case of assignment to an existing vector:
Like James Kanze suggested, it’s more robust to have functions that give you the beginning and end of an array:
And then you can do this without having to repeat the size all over: