I have a 2D char
vector:
vector< vector > matrix;
I will read in a matrix as an input and store it in that v
Given the vector is empty, you can simply resize the outer vector with preallocated inner vectors without the need of a loop:
matrix.resize(COL, vector(ROW));
Alternatively, when initializing or if you want to reset a non-empty vector, you can use the constructor overload taking a size and initial value to initialize all the inner vectors:
matrix = vector >(COL, vector(ROW));
Depending on whether your matrix is column- or row-major, you need to swap the arguments ROW
and COL
. The first one (the first parameter on the outer vector) is your first dimension to access the matrix, i.e. I assumed you access it with matrix[col][row]
.