I used to create a two-dimensional array that always uses
int n=5, m=6;
vector<vector<int> > Matrix (N);
for (int i =0; i< matrix.size (); i++) {
Matrix[i].resize (M);
}
More than three lines are written to specify the strong number of the Matrix.
I found out today that we can do the work in a simpler way.
The following line can replace the above four lines
vector<vector<int> > Matrix (N, vector<int> (M));
#include <iostream>using namespacestd; #include<vector>intMainvoid) {vector<vector <int>> Array (3,vector<int> (3));//vector<vector <int>>array (3);//for (int i = 0; I <3; i++)//array[i].resize (3);//sets the size of the array 3x3//Now you can use this vector just like you would with an array . for(inti =0; I <3; i++) for(intj =0; J <3; J + +) Array[i][j]= (i*j); //Output for(inti =0; I <3; i++) { for(intj =0; J <3; J + +) cout<<ARRAY[I][J] <<" "; cout<<Endl; } cout<<"-------------"<<Endl; array[0].swap (array[1]);//Array Interaction for(inti =0; I <3; i++) { for(intj =0; J <3; J + +) cout<< Array[i][j] <<" "; cout<<Endl; }}
Vector Create 2-D array