A 2D vector is a vector of the vector. Like 2D arrays, we can declare and assign values to a 2D vector!
Assuming you are familiar with a normal vector in C++, with the help of an example we demonstrate how a 2D vector differs from a normal vector below:
C++
#include <vector>
using namespace std;
int main()
{
vector<vector< int >> vect;
return 0;
}
|
In a 2D vector, every element is a vector.
Time Complexity: O(1)
Auxiliary Space: O(1)
C++
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<vector< int >> vect
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
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;
}
|
Time Complexity: O(N*N)
Auxiliary Space: O(N*N)
Another approach to access the vector elements:
C++
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<vector< int >> vect
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (vector< int > vect1D : vect)
{
for ( int x : vect1D)
{
cout << x << " " ;
}
cout << endl;
}
return 0;
}
|
Time Complexity: O(N*N)
Auxiliary Space: O(N*N)
Like Java’s jagged arrays, each element of a 2D vector can contain a different number of values.
C++
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<vector< int >> vect
{
{1, 2},
{4, 5, 6},
{7, 8, 9, 10}
};
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;
}
|
Exercise Problem: Define the 2D vector with different sizes of columns.
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
Output : 1 2 3
1 2
1
2D vectors are often treated as a matrix with “rows” and “columns” inside it. Under the hood they are actually elements of the 2D vector.
We first declare an integer variable named “row” and then an array named “column” which is going to hold the value of the size of each row.
After that we proceed to initialize the memory of every row by the size of column.
C++
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int row = 5;
int column[] = {5, 3, 4, 2, 1};
vector<vector< int >> vec(row);
for ( int i = 0; i < row; i++)
{
int col = column[i];
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;
}
return 0;
}
|
Output1 2 3 4 5
1 2 3
1 2 3 4
1 2
1
Another Approach
Suppose we want to initialize a 2D vector of “n” rows and “m” columns, with a value 0.
C++
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n = 3;
int m = 4;
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;
}
|
Output0 0 0 0
0 0 0 0
0 0 0 0
Time Complexity: O(N*M)
Auxiliary Space: O(N*M)
Yet Another Approach:
Suppose we want to create a 2D vector of “n” rows and “m” columns and input values.
C++
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n = 4;
int m = 5;
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;
}
|
Output1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
Time Complexity: O(N*M)
Auxiliary Space: O(N*M)
We hope you that you leave this article with a better understanding of 2D vectors and are now confident enough to apply them on your own.
This article is contributed by Amit Verma. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.