在C ++中对2D向量进行排序
就2D向量而言,它是1D向量的向量。但是我们对一维向量进行排序的过程
sort(vector.begin(),vector.end());
没有任何用户定义的比较器功能,我们无法对2D向量执行相同的操作,因为它只会根据每列的第一个元素进行排序。
但是我们可以根据用例对2D向量进行排序:
1)根据特定的行排序
下面的示例对2D向量中的特定行进行排序。
例如,二维矩阵为:
[[3, 5, 4], [6, 4, 2], [1, 7, 3]]
因此,如果我们以升序对第一行进行排序,则输出将是:
[[3, 4, 5], [6, 4, 2], [1, 7, 3]]
#include <bits/stdc++.h>
using namespace std;
void print(vector<vector<int> > two_D_vector)
{
for (auto it : two_D_vector) {
//现在是一维向量
for (auto ij : it) {
cout << ij << " ";
}
cout << endl;
}
}
int main(){
//初始化的2D向量
//仅用户定义的元素
vector<vector<int> > two_D_vector{
{ 3, 5, 4 },
{ 6, 4, 2 },
{ 1, 7, 3 }
};
//打印二维矢量
cout << "printing the 2D vector before sorting\n";
print(two_D_vector);
//根据特定的行对2D数组排序
//在这里,我们对2D向量的第一行进行排序
//因此,基本上,我们对一维数组(第一行)进行排序
sort(two_D_vector[0].begin(), two_D_vector[0].end());
//打印2D向量
cout << "printing the 2D vector after sorting\n";
print(two_D_vector);
return 0;
}输出:
printing the 2D vector before sorting 3 5 4 6 4 2 1 7 3 printing the 2D vector after sorting 3 4 5 6 4 2 1 7 3
因此,如果我们以降序对第一行进行排序,则输出将是:
[[3, 5, 4], [6, 4, 2], [7, 3, 1]]
#include <bits/stdc++.h>
using namespace std;
void print(vector<vector<int> > two_D_vector)
{
for (auto it : two_D_vector) {
//现在是一维向量
for (auto ij : it) {
cout << ij << " ";
}
cout << endl;
}
}
int main(){
//初始化的2D向量
//仅用户定义的元素
vector<vector<int> > two_D_vector{
{ 3, 5, 4 },
{ 6, 4, 2 },
{ 1, 7, 3 }
};
//打印二维矢量
cout << "printing the 2D vector before sorting\n";
print(two_D_vector);
//根据特定的行对2D数组排序
//在这里,我们对2D向量的最后一行进行排序
//降序
//对一维数组进行排序
//降序(最后一行)
sort(two_D_vector[2].begin(), two_D_vector[2].end(), greater<int>());
//打印2D向量
cout << "printing the 2D vector after sorting\n";
print(two_D_vector);
return 0;
}输出:
printing the 2D vector before sorting 3 5 4 6 4 2 1 7 3 printing the 2D vector after sorting 3 5 4 6 4 2 7 3 1
2)根据特定的列排序
下面的示例对2D向量中的特定列进行排序。
例如,二维矩阵为:
[[3, 5, 4], [6, 4, 2], [1, 7, 3]]
因此,如果我们以升序对第一列进行排序,则输出将是:
[[1, 4, 5], [3, 4, 2], [6, 7, 3]]
在这里,我们需要定义用户定义的比较器函数来完成上述操作。就像我们将采用2D向量的每个元素(是1D向量,每一行都是特定的)并仅基于第一个元素(或任何特定元素)进行比较。这就是为什么我们需要一个用户定义的比较器。
#include <bits/stdc++.h>
using namespace std;
void print(vector<vector<int> > two_D_vector)
{
for (auto it : two_D_vector) {
//现在是一维向量
for (auto ij : it) {
cout << ij << " ";
}
cout << endl;
}
}
bool mycomp(vector<int>& A, vector<int>& B)
{
//如果first的第一个元素
//row<first element of second row
if (A[0] < B[0])
return true; //没有交换
//否则交换行
return false;
}
int main(){
//初始化的2D向量
//仅用户定义的元素
vector<vector<int> > two_D_vector{
{ 3, 5, 4 },
{ 6, 4, 2 },
{ 1, 7, 3 }
};
//打印二维矢量
cout << "printing the 2D vector before sorting\n";
print(two_D_vector);
//根据特定的行对2D数组排序
//在这里,我们对2D向量的最后一行进行排序
//降序
//对一维数组进行排序
//降序(最后一行)
sort(two_D_vector.begin(), two_D_vector.end(), mycomp);
//打印2D向量
cout << "printing the 2D vector after sorting\n";
print(two_D_vector);
return 0;
}输出:
printing the 2D vector before sorting 3 5 4 6 4 2 1 7 3 printing the 2D vector after sorting 1 7 3 3 5 4 6 4 2
要以降序排序,我们只需要更改比较器功能即可。
#include <bits/stdc++.h>
using namespace std;
void print(vector<vector<int> > two_D_vector)
{
for (auto it : two_D_vector) {
//现在是一维向量
for (auto ij : it) {
cout << ij << " ";
}
cout << endl;
}
}
//根据列以降序排序
bool mycomp(vector<int>& A, vector<int>& B)
{
//如果first的第一个元素
//row<first element of second row
if (A[0] < B[0])
return false; //交换行
//其他明智的不交换
return true;
}
int main(){
//初始化的2D向量
//仅用户定义的元素
vector<vector<int> > two_D_vector{
{ 3, 5, 4 },
{ 6, 4, 2 },
{ 1, 7, 3 }
};
//打印二维矢量
cout << "printing the 2D vector before sorting\n";
print(two_D_vector);
//根据特定的行对2D数组排序
//在这里,我们对2D向量的最后一行进行排序
//降序
//对一维数组进行排序
//降序(最后一行)
sort(two_D_vector.begin(), two_D_vector.end(), mycomp);
//打印2D向量
cout << "printing the 2D vector after sorting\n";
print(two_D_vector);
return 0;
}输出:
printing the 2D vector before sorting 3 5 4 6 4 2 1 7 3 printing the 2D vector after sorting 6 4 2 3 5 4 1 7 3
可能会有各种用例对2D向量进行排序,我们需要编写比较器函数。
练习题
(a)根据行大小升序排序
Say the 2D vector is
{
{2, 3, 4, 5},
{3, 4, 1},
{1}}
After sorting the 2D vector based on
row size in ascending order:
{
{1},
{3, 4, 1},
{2, 3, 4, 5}
}在这里,我们需要使用用户定义的函数,该函数将根据行的大小交换行。
#include <bits/stdc++.h>
using namespace std;
void print(vector<vector<int> > two_D_vector)
{
for (auto it : two_D_vector) {
//现在是一维向量
for (auto ij : it) {
cout << ij << " ";
}
cout << endl;
}
}
//根据列以降序排序
bool mycomp(vector<int>& A, vector<int>& B)
{
//if first row size>second row size
if (A.size() > B.size())
return false; //交换行
//其他明智的不交换
return true;
}
int main(){
//初始化的2D向量
//仅使用定义的元素
vector<vector<int> > two_D_vector{
{ 2, 3, 4, 5 },
{ 3, 4, 1 },
{ 1 }
};
//打印二维矢量
cout << "printing the 2D vector before sorting\n";
print(two_D_vector);
//根据特定的行对2D数组排序
//在这里,我们对2D向量的最后一行进行排序
//降序
//对一维数组进行排序
//降序(最后一行)
sort(two_D_vector.begin(), two_D_vector.end(), mycomp);
//打印2D向量
cout << "printing the 2D vector after sorting\n";
print(two_D_vector);
return 0;
}输出:
printing the 2D vector before sorting 2 3 4 5 3 4 1 1 printing the 2D vector after sorting 1 3 4 1 2 3 4 5
(b)仍然可以基于列大小进行排序吗?
如果您无法通过这种方式进行排序,那么请评论一下您为什么不能这样做?