// Create a vector containing n
//vectors of size m, all u=initialized with 0
vector<vector<int> > vec( n , vector<int> (m, 0));
For user defined size:
vector<vector<int>> vec( n );
where n -> number of rows
vector<vector<int>> vec( n , vector<int> (m, 0));
where n -> number of rows
where m -> number of columns (all initialized with 0)
// CPP program
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n = 3;
int m = 4;
/*
We create a 2D vector containing "n"
elements each having the value "vector<int> (m, 0)".
"vector<int> (m, 0)" means a vector having "m"
elements each of value "0".
Here these elements are vectors.
*/
vector<vector<int>> vec( n , vector<int> (m, 0));
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
cout << vec[i][j] << " ";
}
cout<< endl;
}
return 0;
}
// CPP program
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n = 3;
int m = 4;
/*
We create a 2D vector containing "n"
elements each having the value "vector<int> (m, 0)".
"vector<int> (m, 0)" means a vector having "m"
elements each of value "0".
Here these elements are vectors.
*/
vector<vector<int>> vec( n , vector<int> (m, 0));
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
cout << vec[i][j] << " ";
}
cout<< endl;
}
return 0;
}