Sorting 2D Vector in C++ | Set 2 (In descending order by row and column)

We have discussed some of the cases of sorting 2D vector in below set 1.

Sorting 2D Vector in C++ | Set 1 (By row and column)

More cases are discussed in this article

Case 3 : To sort a particular row of 2D vector in descending order 对二维的vector的行进行降序排列。
This type of sorting arranges a selected row of 2D vector in descending order . This is achieved by using “sort()” and passing iterators of 1D vector as its arguments. 对二维的vector中选定的特定行进行降序排列,由sort()函数实现。

// C++ code to demonstrate sorting of a 
// row of 2D vector in descending order 
#include<iostream> 
#include<vector> // for 2D vector 
#include<algorithm> // for sort() 
using namespace std; 

int main() 
{ 
	// Initializing 2D vector "vect" with 
	// values 
	vector< vector<int> > vect{{3, 5, 1}, 
							{4, 8, 6}, 
							{7, 2, 9}}; 
	// Number of rows; 
	int m = vect.size(); 

	// Number of columns (Assuming all rows 
	// are of same size). We can have different 
	// sizes though (like Java). 
	int n = vect[0].size(); 

	// Displaying the 2D vector before sorting 
	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; 
	} 

	// Use of "sort()" for sorting first row 
	sort(vect[0].rbegin(), vect[0].rend()); 

	// Displaying the 2D vector after sorting 
	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; 
} 

Output:

The Matrix before sorting 1st row is:
3 5 1 
4 8 6 
7 2 9 
The Matrix after sorting 1st row is:
5 3 1 
4 8 6 
7 2 9 

Case 4 : To sort the entire 2D vector on basis of a particular column in descending order. 对整个二维的vector基于特定列进行降序排列。
In this type of sorting 2D vector is entirely sorted on basis of a chosen column in descending order. For example if the chosen column is second, the row with greatest value in second column becomes first row, second greatest value in second column becomes second row, and so on. 这种排序二维的vector,是基于选定特定列进行降序排序。例如,如果选定第二列,在第二列中值最大的行放在第一行,列中第二大的值放在第二行。

{3, 5, 1},
{4, 8, 6},
{7, 2, 9};

After sorting this matrix by second column, we get

{4, 8, 6} // Row with greatest value in second column
{3, 5, 1} // Row with second greatest value in second column
{7, 2, 9}

This is achieved by passing a third argument in “sort()” as a call to user defined explicit function.

// C++ code to demonstrate sorting of a 
// 2D vector on basis of a column in 
// descending order 
#include<iostream> 
#include<vector> // for 2D vector 
#include<algorithm> // for sort() 
using namespace std; 

// Driver function to sort the 2D vector 
// on basis of a particular column in 
// descending order 
bool sortcol( const vector<int>& v1, const vector<int>& v2 ) 
{ 
	return v1[1] > v2[1]; 
} 

int main() 
{ 
	// Initializing 2D vector "vect" with 
	// values 
	vector< vector<int> > vect{{3, 5, 1}, 
								{4, 8, 6}, 
								{7, 2, 9}}; 

	// Number of rows; 
	int m = vect.size(); 

	// Number of columns (Assuming all rows 
	// are of same size). We can have different 
	// sizes though (like Java). 
	int n = vect[0].size(); 
	
	// Displaying the 2D vector before sorting 
	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; 
	}							 

	// Use of "sort()" for sorting on basis 
	// of 2nd column in descending order 
	sort(vect.begin(), vect.end(),sortcol); 

	// Displaying the 2D vector after sorting 
	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; 
} 

Output:

The Matrix before sorting is:
3 5 1 
4 8 6 
7 2 9 
The Matrix after sorting is:
4 8 6 
3 5 1 
7 2 9 

This article is contributed by Manjeet Singh .If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected]. 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.

Recommended Posts:

猜你喜欢

转载自blog.csdn.net/qq_27009517/article/details/86495778
今日推荐