Check out example codes for "c++ 2D vectors". It will help you in understanding the concepts better.

Code Example 1

``````myVector[
Vector[0, 4, 2, 5],
Vector[1, 4, 2]
];

/*When you call for myVector[1].size() it would return 3 and [0] would return 4.

For the amount of rows (int vectors) in the 2d vector, you can just use myVector.size()

You can run this to see it in actions*/``````

Code Example 2

``````#include<bits/stdc++.h>
using namespace std;

int main(){
int n = 3;
int m = 4;

// Create a vector containing n
//vectors of size m.
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<< "\n";
}

return 0;
}``````

Code Example 3

``````std::vector<vector<int>> d;
//std::vector<int> d;
cout<<"Enter the N number of ship and port:"<<endl;
cin>>in;
cout<<"\Enter preference etc..:\n";
for(i=0; i<in; i++){
cout<<"ship"<<i+1<<":"<<' ';
for(j=0; j<in; j++){
cin>>temp;
d.push_back(temp);// I don't know how to push_back here!!
}
}``````

Code Example 4

``````std::vector<vector<int>> d;
//std::vector<int> d;
cout<<"Enter the N number of ship and port:"<<endl;
cin>>in;
cout<<"\Enter preference etc..:\n";
for(i=0; i<in; i++){
cout<<"ship"<<i+1<<":"<<' ';
for(j=0; j<in; j++){
cin>>temp;
d[i].push_back(temp);
}
}``````

Code Example 5

``````// Create a vector containing n
//vectors of size m, all u=initialized with 0
vector<vector<int> > vec( n , vector<int> (m, 0));``````

Code Example 6

``````int main()
{
int row = 5; // size of row
int colom[] = { 5, 3, 4, 2, 1 };

vector<vector<int> > vec(row);  // Create a vector of vector with size equal to row.
for (int i = 0; i < row; i++) {
int col;
col = colom[i];
vec[i] = vector<int>(col); //Assigning the coloumn size of vector
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;
}
}``````

Learn ReactJs, React Native from akashmittal.com