vector 2d matrix code example
Example 1: 2d vector
#include <bits/stdc++.h>
using namespace std;
int main()
{
int rows = 2;
int cols = 2;
int val = 1;
vector< vector<int> > v(rows, vector<int> (cols, val));
v[0][0] = 5;
v[1][1] = 4;
cout << v[0][0] << endl;
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));
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
cout<<v[i][j]<<" ";
cout<<endl;
}
Example 3: initialising 2d vector
vector<vector<int> > vect{ { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };