Skip to main content

2D Vectors

#include <iostream>
#include <vector>

using namespace std;

//Function to Print 2D Vector:
void printVector(vector<vector<int>>v) {
for (int i = 0; i < v.size(); i++) {
for (int j = 0; j < v[i].size(); j++) {
cout << v[i][j] << " ";
}
cout << "\n";
}
}


int main()
{
freopen ("inputf.in", "r", stdin);
freopen ("outputf.in", "w", stdout);
/*
int n;
cin >> n;

//Declaring and Initiallising 2D Vector v;
vector<vector<int>> v1 {{10, 20, 30, 40, 50}, {1, 2, 3}, {12, 23, 34, 56}};

printVector(v1);
*/

/****************************************************************/
/****************************************************************/

/*Creating 2D Vector with "USER DEFINED NUMBER OF COLUMNS" and "ELEMENTS":*/
int row;
cin >> row;

vector<vector<int>> v(row);

//Take the column's value from the user:
for (int i = 0; i < v.size(); i++) {
int col;
cin >> col; //Numeber of columns by the user

v[i] = vector<int> (col); //Assigning Column Size by the uiser to the vector at v[i]

//Assigning Different user input values to the vector:
for (int j = 0; j < v[i].size(); j++) {
cin >> v[i][j];
}

}

printVector(v);


cout<<"\n";


/*0000000000000000000000000000000000000000000000000000*/
/*0000000000000000000000000000000000000000000000000000*/


/*
//Only for 2D matrix: taking user input using "emplace_back()"
vector<vector<int>> vec1(ROW);

for (int i=0; i<ROW;i++) {
for (int j=0;j<COL;j++) {
int temp;
cin>>temp;
vec1[i].emplace_back(temp);
}
}
*/

/****************************************************************/
/****************************************************************/

//Creating Vector with User defined Row and Column Size.
int r, c;
cin >> r >> c;

vector<vector<int>> vec(r, vector<int>(c, 101));

printVector(vec);




return 0;


/*Let the input be:
5
5 10 20 30 40 50
3 100 200 300
4 1 2 3 4
2 1000 1000
1 100000

5 6
*/
}