C++(1)中的二维向量排序(行和列)

二维向量是向量的向量。在C++中,2D向量被用来动态地创建矩阵、表或任何其他结构。基本上,它是一个借助向量实现的矩阵。它们是使用 头文件。

null

下面是一个用C++演示二维向量的程序:

CPP

// C++ code to demonstrate 2D vector
#include <iostream>
#include <vector> // for 2D vector
using namespace std;
// Driver Code
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;
}


输出

1 2 3 4 5 6 7 8 9 

二维向量的排序方法

案例1:对2D向量的特定行进行排序 这种类型的排序按升序排列选定的二维向量行。这是通过使用 排序() 并将一维向量的迭代器作为其参数传递。在sort()中,它通常需要两个参数,第一个参数是数组/向量开始排序的点,第二个参数是我们希望数组/向量排序的长度。此功能包含在 头文件。

CPP

// C++ code to demonstrate sorting of a
// row of 2D vector
#include <algorithm> // for sort()
#include <iostream>
#include <vector> // for 2D vector
using namespace std;
// Driver Code
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:" ;
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:" ;
for ( int i = 0; i < m; i++) {
for ( int j = 0; j < n; j++)
cout << vect[i][j] << " " ;
cout << endl;
}
return 0;
}


输出

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 

案例2:根据特定列对整个2D向量进行排序

在这种类型的排序中,2D向量完全基于所选列进行排序。例如,如果所选列为第二列,则第二列中值最小的行将成为第一行,第二列中值最小的第二行将成为第二行,依此类推。

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

按第二列对矩阵进行排序后,我们得到

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

这是通过在中传递第三个参数来实现的 排序() 作为对用户定义的显式函数的调用。

CPP

// C++ code to demonstrate sorting of a
// 2D vector on basis of a column
#include <algorithm> // for sort()
#include <iostream>
#include <vector> // for 2D vector
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];
}
// Driver Code
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:" ;
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:" ;
for ( int i = 0; i < m; i++) {
for ( int j = 0; j < n; j++)
cout << vect[i][j] << " " ;
cout << endl;
}
return 0;
}


输出

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 

必须阅读:

本文由 曼吉星 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享