# 2D vector in C++ with user defined size

A 2D vector is a vector of vector. Like 2D arrays, we can declare and assign values to 2D matrix

 `// C++ code to demonstrate 2D vector ` `#include ` `#include // for 2D vector ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``// Initializing 2D vector "vect" with ` `    ``// values ` `    ``vector > vect{ { 1, 2, 3 }, ` `                               ``{ 4, 5, 6 }, ` `                               ``{ 7, 8, 9 } }; ` ` `  `    ``// Displaying the 2D vector ` `    ``for` `(``int` `i = 0; i < vect.size(); i++) { ` `        ``for` `(``int` `j = 0; j < vect[i].size(); j++) ` `            ``cout << vect[i][j] << ``" "``; ` `        ``cout << endl; ` `    ``} ` ` `  `    ``return` `0; ` `} `

Outout :

```1 2 3
4 5 6
7 8 9
```

Like Java’s jagged arrays, each row of 2D vector can contain different number of columns.

 `// C++ code to demonstrate 2D vector where ` `// each row is of different size. ` `#include ` `#include // for 2D vector ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``// Initializing 2D vector "vect" with ` `    ``// different number of values in each ` `    ``// row. ` `    ``vector > vect{ { 1, 2 }, ` `                               ``{ 4, 5, 6 }, ` `                               ``{ 7, 8, 9, 10 } }; ` ` `  `    ``// Displaying the 2D vector ` `    ``for` `(``int` `i = 0; i < vect.size(); i++) { ` `        ``for` `(``int` `j = 0; j < vect[i].size(); j++) ` `            ``cout << vect[i][j] << ``" "``; ` `        ``cout << endl; ` `    ``} ` ` `  `    ``return` `0; ` `} `

Output :

```1 2
4 5 6
7 8 9 10
```

Exercise Problem : Define the 2D vector with different sizes of column input by user.
Examples:

```Input : Number of rows : 5
Number of columns in rows :
2 3 4 5 1
Output : 1 2
1 2 3
1 2 3 4
1 2 3 4 5
1

Input : Number of rows : 3
Number of columns in rows :
3 2 1
Input : 3 3 2 1
Output : 1 2 3
1 2
1
```

First we take the input of row and and take the input of column of every row.
Now the initialize the memory of every row by the size of column.

 `// CPP program to create a 2D vector where  ` `// every row has  ` `#include ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``// size of row ` `    ``int` `row = 5; ` `    ``int` `colom[] = { 5, 3, 4, 2, 1 }; ` ` `  `    ``// Create a vector of vector with size ` `    ``// equal to row. ` `    ``vector > vec(row);  ` ` `  `    ``for` `(``int` `i = 0; i < row; i++) { ` ` `  `        ``// size of column ` `        ``int` `col;  ` `        ``col = colom[i]; ` ` `  `        ``// declare  the i-th row to size of column ` `        ``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; ` `    ``} ` `} `

Output:

```1 2 3 4 5
1 2 3
1 2 3 4
1 2
1
```

Other Approach
Suppose we want to initialise a 2D vector of N rows and M column, with a value 0.

 `#include ` `using` `namespace` `std; ` ` `  `int` `main(){ ` `    ``int` `n = 3; ` `    ``int` `m = 4; ` ` `  `    ``// Create a vector containing n ` `    ``//vectors of size m.  ` `    ``vector > 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; ` `} `

Output:

```0 0 0 0
0 0 0 0
0 0 0 0
```

Other Approach
Suppose we want to create a 2D vector of N rows and M column, and input values.

 `#include ` `using` `namespace` `std; ` ` `  `int` `main(){ ` `    ``int` `n = 4; ` `    ``int` `m = 5; ` ` `  `    ``// Create a vector containing n ` `    ``//vectors of size m.  ` `    ``vector > vec( n , vector<``int``> (m));  ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``for` `(``int` `j = 0; j < m; j++){ ` `            ``vec[i][j] = j + i + 1; ` `        ``} ` `    ``} ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``for` `(``int` `j = 0; j < m; j++){ ` `            ``cout<< vec[i][j]<< ``" "``; ` `        ``} ` `        ``cout<< ``"\n"``; ` `    ``} ` ` `  `return` `0; ` `} `

Output:

```1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
```

This article is contributed by Amit Verma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up