In some cases only the below line works.Why so?
vector< vector<int>> a(M,N);
This works in every case.
vector< vector<int>> a(M, vector<int> (N));
What's the difference?
See Question&Answers more detail:osIn some cases only the below line works.Why so?
vector< vector<int>> a(M,N);
This works in every case.
vector< vector<int>> a(M, vector<int> (N));
What's the difference?
See Question&Answers more detail:osstd::vector
has a fill constructor which creates a vector of n elements and fills with the value specified. a
has the type std::vector<std::vector<int>>
which means that it is a vector of a vector. Hence your default value to fill the vector is a vector itself, not an int
. Therefore the second options is the correct one.
std::vector<std::vector<int>> array_2d(rows, std::vector<int>(cols, 0));
This creates a rows * cols 2D array where each element is 0. The default value is std::vector<int>(cols, 0)
which means each row has a vector which has cols
number of element, each being 0.