S.Mitchell S.Mitchell - 8 months ago 59
C++ Question

initializing 2d vector using only 1 line c++

I need to be able to initialize a 2D vector of int's in the same line in which I create it.

To be more specific, I have to create a 3x2 size 2D vector and set all of it's values to 0 using only 1 line of code.

Is there a way this can be done without using a for loop and several lines of code?

Answer Source

Try this:

std::vector<std::vector<int>> twoDimVector(3, std::vector<int>(2, 0));