欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-03-01 23:26:09
...

What is a 2D Vector?
A 2D vector is vector of vectors. It is an matrix implemented with the help of vectors. 二维的vector,是vector中的元素是vector,用vector实现的矩阵。

// C++ code to demonstrate 2D vector 
#include<iostream> 
#include<vector> // for 2D vector 
using namespace std; 

int main() 
{ 
	// Initializing 2D vector "vect" with 
	// values 
	vector< vector<int> > 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; 
} 

Output:

1 2 3
4 5 6
7 8 9

Case 1 : To sort a particular row of 2D vector. 对二维的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. 这种排序对二维vector中的特定的行进行升序排序。用函数sort()实现,传一维vector的迭代器作为参数。

// C++ code to demonstrate sorting of a 
// row of 2D vector 
#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].begin(), vect[0].end()); 

	// 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:
1 3 5 
4 8 6 
7 2 9 

Case 2 : To sort the entire 2D vector on basis of a particular column. 基于整个二维的vector,对特定列进行排序。
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 smallest value in second column becomes first row, second smallest 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

{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 user defined explicit function. 通过给sort()传递第三个参数实现,是一个用户自定义的函数。

// C++ code to demonstrate sorting of a 
// 2D vector on basis of a column 
#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 
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 
	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:
7 2 9 
3 5 1 
4 8 6 

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

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:

相关标签: vector