2D vectors in c++ code example
Example 1: 2d vector c++ declaration
vector< vector<int>> a(rows, vector<int> (cols));
Example 2: c++ 2D vectors
int main()
{
int row = 5;
int colom[] = { 5, 3, 4, 2, 1 };
vector<vector<int> > vec(row);
for (int i = 0; i < row; i++) {
int col;
col = colom[i];
vec[i] = vector<int>(col);
for (int j = 0; j < col; j++)
vec[i][j] = j + 1;
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < vec[i].size(); j++)
cout << vec[i][j] << " ";
cout << endl;
}
}
Example 3: initialising 2d vector
vector<vector<int> > vect{ { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };