A 2D vector is a vector of vectors. In C++, 2D vectors are used for creating matrices, tables, or any other structures, dynamically. Basically, It is a matrix implemented with the help of vectors. They are created using the <vector> header file.
Following is a program to demonstrate 2D vectors in C++:
CPP
#include <iostream>
#include <vector> // for 2D 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*M) where N and M are the dimensions of the vector.
Space Complexity: O(N*M) where N and M are the dimensions of the vector.
Ways to Sort a 2D Vector
Case 1: To sort a particular row of 2D vector
This type of sorting arranges a selected row of 2D vector in ascending order. This is achieved by using sort() and passing iterators of 1D vector as its arguments. In sort(), it generally takes two parameters, the first one being the point of the array/vector from where the sorting needs to begin and the second parameter being the length up to which we want the array/vector to get sorted. This function is included in <algorithm> header file.
CPP
#include <algorithm> // for sort()
#include <iostream>
#include <vector> // for 2D vector
using namespace std;
int main()
{
vector<vector< int > > vect{ { 3, 5, 1 },
{ 4, 8, 6 },
{ 7, 2, 9 } };
int m = vect.size();
int n = vect[0].size();
cout << "The Matrix before sorting 1st row is:\n" ;
for ( int i = 0; i < m; i++) {
for ( int j = 0; j < n; j++)
cout << vect[i][j] << " " ;
cout << endl;
}
sort(vect[0].begin(), vect[0].end());
cout << "The Matrix after sorting 1st row is:\n" ;
for ( int i = 0; i < m; i++) {
for ( int j = 0; j < n; j++)
cout << vect[i][j] << " " ;
cout << endl;
}
return 0;
}
|
OutputThe Matrix before sorting 1st row is:
3 5 1
4 8 6
7 2 9
The Matrix after sorting 1st row is:
1 3 5
4 8 6
7 2 9
Case 2: To sort the entire 2D vector on basis of a particular column
In this type of sorting 2D vector is entirely sorted on basis of a chosen column. For example, if the chosen column is second, the row with the smallest value in the second column becomes the first row, the second smallest value in the second column becomes the second row, and so on.
{3, 5, 1},
{4, 8, 6},
{7, 2, 9};
After sorting this matrix by the second column, we get
{7, 2, 9} // Row with smallest value in second column
{3, 5, 1} // Row with smallest value in second column
{4, 8, 6}
This is achieved by passing a third argument in sort() as a call to the user-defined explicit function.
CPP
#include <algorithm> // for sort()
#include <iostream>
#include <vector> // for 2D vector
using namespace std;
bool sortcol( const vector< int >& v1, const vector< int >& v2)
{
return v1[1] < v2[1];
}
int main()
{
vector<vector< int > > vect{ { 3, 5, 1 },
{ 4, 8, 6 },
{ 7, 2, 9 } };
int m = vect.size();
int n = vect[0].size();
cout << "The Matrix before sorting is:\n" ;
for ( int i = 0; i < m; i++) {
for ( int j = 0; j < n; j++)
cout << vect[i][j] << " " ;
cout << endl;
}
sort(vect.begin(), vect.end(), sortcol);
cout << "The Matrix after sorting is:\n" ;
for ( int i = 0; i < m; i++) {
for ( int j = 0; j < n; j++)
cout << vect[i][j] << " " ;
cout << endl;
}
return 0;
}
|
OutputThe Matrix before sorting is:
3 5 1
4 8 6
7 2 9
The Matrix after sorting is:
7 2 9
3 5 1
4 8 6
Must Read:
This article is contributed by Manjeet Singh. 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.