2d array of vectors c++ code example
Example 1: how to make a 2d vector in c++
vector<vector<int> > vec( n , vector<int> (m, 0));
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: size of a matrix using vector c++
myVector[0].size();
Example 4: initialising 2d vector
vector<vector<int> > vect{ { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
Example 5: c++ code to write 2d array
#include <iostream>
using namespace std;
int main(){
int n,m;
int a[n][m];
cin >> n >>m;
for ( int i=0; i<n; i++){
for (int j=0; j<m; j++){
cin >> a[i][j];
}
}
for ( int x=0; x<n; x++){
for (int y=0; y<m; y++){
cout << "a[" << x << "][" << y << "]: ";
cout << a[x][y] << endl;
}
}
return 0;
}