The 2D vector is a vector of vector likewise 2D array for declaring and assigning values on it. Its syntax looks like a 2D array but is written in vector format. So in this post, we are discussing on 2D vector with a user-defined size.
If you’re familiar with normal vector declaration then how 2D vector is different from normal vector below is demonstrated:
/* Vectors belong to a C++ library called STL so we need to import it first! */ #include <vector> using namespace std; int main() { /* In the case of a normal vector we initialize it as: 1. vector<datatype> variable_name Now in the case of a 2D vector all we do is create a vector of datatype vector. We simply replace "datatype" with "vector<int>": 1. vector<vector<int>> variable_name That's literally it! We just created a 2D vector! On line 23 below we declare an actual 2D vector named "vect". */ vector<vector<int>> vect; //2D vector declaration. return 0; }
Now, In a 2D vector, every element is a vector.
/* C++ code to demonstrate a 2D vector with elements(vectors) inside it. */ #include <iostream> #include <vector> using namespace std; int main() { /* Below we initialize a 2D vector named "vect" on line 12 and then we declare the values on line 14, 15 and 16 respectively. */ vector<vector<int>> vect { {10, 20, 30}, {40, 50, 60}, {70, 80, 90} }; /* Now we print the values that we just declared on lines 14, 15 and 16 using a simple nested for loop. */ 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:
10 20 30
40 50 60
70 80 90
Here is another approach for accessing 2D vector elements are:
/* C++ code to demonstrate a 2D vector with elements(vectors) inside it. */ #include <iostream> #include <vector> using namespace std; int main() { /* Below we initialize a 2D vector named "vect" on line 12 and then we declare the values on line 14, 15 and 16 respectively. */ vector<vector<int>> vect { {10, 20, 30}, {40, 50, 60}, {70, 80, 90} }; /* Now we print the values that we just declared on lines 14, 15 and 16 using a simple nested for loop with the help of iterator. */ /* vector<vector<int>> vect We can divide this declaration to two parts, which will help us to understand the below concepts. 1. vect is a 2D vector consisting multiple elements of type vector<int>. 2. vector<int> is a 1D vector consisting of multiple int data. So we can use iterator provided by STL instead of i,j variable used in for loop. It can reduce the error which can happen wrt to i, j operations(i++, j++) In the below code we are using iterator to access the vector elements. 1. We are getting vect1D vectors of type vector<int> from the 2D vector vect. 2. We are getting int elements to x from the vector<int> vect 1D vector. */ for (vector<int> vect1D : vect) { for (int x : vect1D) { cout << x << " "; } cout << endl; } return 0; }
Output:
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
Each element of a 2D vector can contain a different number of values.
/* C++ program to demonstrate a 2D vector where each of its elements is of different size. */ #include <iostream> #include <vector> using namespace std; int main() { /* We initialize a 2D vector named "vect" on line 16 with different number of values in each element. */ vector<vector<int>> vect { /* Element one with 2 values in it. */ {10, 20}, /* Element two with 3 values in it. */ {40, 50, 60}, /* Element three with 4 values in it. */ {70, 80, 90, 100} }; /* Now we print the vector that we just defined using a simple nested for loop. */ 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:
10 20
40 50 60
70 80 90 100
Question: Declare 2D vector with different sizes of columns
Example:
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
Output : 1 2 3
1 2
1
So, from the above example, you’ll say that 2D vectors are often treated as “row” and “columns” like matrices. So actually these are elements of the 2D vector.
We declare the number of rows and number of columns in rows means how many entries the 2D vector? So values are entered by the size of each row.
If row defines the number of values you add on each row by columns.
/* C++ program to create a 2D vector where every row has a certain number of values as defined by the user.(On line 13) */ #include <iostream> #include <vector> using namespace std; int main() { /* Here we tell how many rows the 2D vector is going to have. */ int row = 5; /* We define the number of values each row is supposed to have. */ int column[] = {2, 5, 4, 3, 1}; /* We now create a vector of vector with size equal to row. */ vector<vector<int>> vec(row); /* On line 21 we created a 2D vector and assigned it a capacity of "row"(in this case 5) units. */ /* Now we will proceed to create the structure of our 2D vector by assigning the value of rows and columns through a nested for loop. */ for(int i = 0; i < row; i++) { /* Declaring the size of the column. */ int col = column[i]; /* On the 43rd line we declare the i-th row to the size of the column. We create a normal vector of capacity "col" which in every iteration of the for loop will define the values inside of each row. */ vec[i] = vector<int>(col); for(int j = 0; j < col; j++) { vec[i][j] = j + 1; } } /* We now finally use a simple nested for loop to print the 2D vector that we just created above. */ for(int i = 0; i < row; i++) { for (int j = 0; j < vec[i].size(); j++) { cout << vec[i][j] << " "; } cout << endl; } return 0; }
Output:
1, 2
1, 2, 3, 4, 5
1, 2, 3, 4
1, 2, 3
1
Another Approach:
If we want to initialize a 2D vector of “n” rows and “m” columns, with a value 0.
// CPP program #include <iostream> #include <vector> using namespace std; int main() { int n = 3; int m = 4; /* We create a 2D vector containing "n" elements each having the value "vector<int> (m, 0)". "vector<int> (m, 0)" means a vector having "m" elements each of value "0". Here these elements are vectors. */ 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<< endl; } return 0; }
Output:
0 0 0 0
0 0 0 0
0 0 0 0
Another Approach:
If we want to initialize a 2D vector of “n” rows and “m” columns, with input values.
// CPP program #include <iostream> #include <vector> using namespace std; int main() { int n = 4; int m = 5; /* Create a vector containing "n" vectors each of size "m". */ vector<vector<int>> 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 << endl; } return 0; }
Output:
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
We hope that you will learn and have a better understanding of 2D vectors and how it uses and print also. If you found mistakes then comment or reply to my email.
If you like LingarajTechhub and would like to contribute, you can also submit an article using Write For Us and wants to earn then follow this “reward” section.