2d vector c++ at code example
Example 1: 2d vector c++ declaration
vector< vector<int>> a(rows, vector<int> (cols));
Example 2: 2d vector
#include <bits/stdc++.h>
using namespace std;
main() {
int r=2,c=3,val=1;
vector<vector<int>> v(r,vector<int>(c,val));
/*
2d vector “v[r][c]”;
all elements = val;
(default value is 0)
*/
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
cout<<v[i][j]<<" ";
cout<<endl;
/*
1 1 1
1 1 1
*/
}