c ++向量库
As per as a 2D vector is concerned it's a vector of a 1D vector. But what we do in sorting a 1D vector like,
就2D向量而言,它是1D向量的向量 。 但是我们在对一维向量进行排序时所做的工作
sort(vector.begin(),vector.end());
We can't do the same for a 2D vector without any user-defined comparator function, as it will merely sort based on the first element of each column.
没有任何用户定义的比较器函数,我们无法对2D向量执行相同的操作,因为它只会根据每列的第一个元素进行排序。
But we can sort 2D vector based on use cases:
但是我们可以根据用例对2D向量进行排序:
1)根据特定的行排序 (1) Sort based on a particular row)
The below example sorts a particular row in the 2D vector.
下面的示例对2D向量中的特定行进行排序。
Say for example, the 2D matrix is:
例如,二维矩阵为:
[[3, 5, 4],
[6, 4, 2],
[1, 7, 3]]
So if we sort the first row in ascending order the output will be:
因此,如果我们以升序对第一行进行排序,则输出将是:
[[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) {
//it is now an 1D vector
for (auto ij : it) {
cout << ij << " ";
}
cout << endl;
}
}
int main()
{
//2D vector initialized with
//user-defined elements only
vector<vector<int> > two_D_vector{
{ 3, 5, 4 },
{ 6, 4, 2 },
{ 1, 7, 3 }
};
//printing the 2D vector
cout << "printing the 2D vector before sorting\n";
print(two_D_vector);
//sorting the 2D array based on a particular row
//here we sort the first row of the 2D vector
//so, basically we sort the 1D array(the first row)
sort(two_D_vector[0].begin(), two_D_vector[0].end());
//print the 2D vector
cout << "printing the 2D vector after sorting\n";
print(two_D_vector);
return 0;
}
Output:
输出:
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
So if we sort the first row in descending order the output will be:
因此,如果我们以降序对第一行进行排序,则输出将是:
[[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) {
//it is now an 1D vector
for (auto ij : it) {
cout << ij << " ";
}
cout << endl;
}
}
int main()
{
//2D vector initialized with
//user-defined elements only
vector<vector<int> > two_D_vector{
{ 3, 5, 4 },
{ 6, 4, 2 },
{ 1, 7, 3 }
};
//printing the 2D vector
cout << "printing the 2D vector before sorting\n";
print(two_D_vector);
//sorting the 2D array based on a particular row
//here we sort the last row of the 2D vector
//in descending order
//so, basically we sort the 1D array in
//descending order(the last row)
sort(two_D_vector[2].begin(), two_D_vector[2].end(), greater<int>());
//print the 2D vector
cout << "printing the 2D vector after sorting\n";
print(two_D_vector);
return 0;
}
Output:
输出:
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)根据特定的列排序 (2) Sort based on a particular column)
The below example sorts a particular column in the 2D vector.
下面的示例对2D向量中的特定列进行排序。
Say for example, the 2D matrix is:
例如,二维矩阵为:
[[3, 5, 4],
[6, 4, 2],
[1, 7, 3]]
So if we sort the first column in ascending order the output will be:
因此,如果我们以升序对第一列进行排序,则输出将是:
[[1, 4, 5],
[3, 4, 2],
[6, 7, 3]]
Here we need to define our user-defined comparator function to do the above thing. Like we will take each element of the 2D vector (which is a 1D vector, to be specific each row) and compare based on the first element (or any particular element) only. That's why we need a user-defined comparator.
在这里,我们需要定义用户定义的比较器函数来完成上述操作。 就像我们将采用2D向量的每个元素(这是1D向量,每一行都是特定的)并仅基于第一个元素(或任何特定元素)进行比较。 这就是为什么我们需要一个用户定义的比较器 。
#include <bits/stdc++.h>
using namespace std;
void print(vector<vector<int> > two_D_vector)
{
for (auto it : two_D_vector) {
//it is now an 1D vector
for (auto ij : it) {
cout << ij << " ";
}
cout << endl;
}
}
bool mycomp(vector<int>& A, vector<int>& B)
{
//if first element of first
//row<first element of second row
if (A[0] < B[0])
return true; //no swap
//other wise swap the rows
return false;
}
int main()
{
//2D vector initialized with
//user-defined elements only
vector<vector<int> > two_D_vector{
{ 3, 5, 4 },
{ 6, 4, 2 },
{ 1, 7, 3 }
};
//printing the 2D vector
cout << "printing the 2D vector before sorting\n";
print(two_D_vector);
//sorting the 2D array based on a particular row
//here we sort the last row of the 2D vector
//in descending order
//so, basically we sort the 1D array in
//descending order(the last row)
sort(two_D_vector.begin(), two_D_vector.end(), mycomp);
//print the 2D vector
cout << "printing the 2D vector after sorting\n";
print(two_D_vector);
return 0;
}
Output:
输出:
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
To sort in descending order, we need to just change the comparator function.
要按降序排序,我们只需要更改比较器功能即可。
#include <bits/stdc++.h>
using namespace std;
void print(vector<vector<int> > two_D_vector)
{
for (auto it : two_D_vector) {
//it is now an 1D vector
for (auto ij : it) {
cout << ij << " ";
}
cout << endl;
}
}
//to sort based on column in descending order
bool mycomp(vector<int>& A, vector<int>& B)
{
//if first element of first
//row<first element of second row
if (A[0] < B[0])
return false; //swap the rows
//other wise no swap
return true;
}
int main()
{
//2D vector initialized with
//user-defined elements only
vector<vector<int> > two_D_vector{
{ 3, 5, 4 },
{ 6, 4, 2 },
{ 1, 7, 3 }
};
//printing the 2D vector
cout << "printing the 2D vector before sorting\n";
print(two_D_vector);
//sorting the 2D array based on a particular row
//here we sort the last row of the 2D vector
//in descending order
//so, basically we sort the 1D array in
//descending order(the last row)
sort(two_D_vector.begin(), two_D_vector.end(), mycomp);
//print the 2D vector
cout << "printing the 2D vector after sorting\n";
print(two_D_vector);
return 0;
}
Output:
输出:
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
There can be various use cases to sort a 2D vector and we need to write our comparator functions.
可以使用各种用例对2D向量进行排序,我们需要编写比较器函数。
Exercises
练习题
(a) Sort based on row sizes in ascending order
(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}
}
Here we need to use our user define a function which will swap the rows as per their sizes.
在这里,我们需要使用用户定义的函数,该函数将根据行的大小交换行。
#include <bits/stdc++.h>
using namespace std;
void print(vector<vector<int> > two_D_vector)
{
for (auto it : two_D_vector) {
//it is now an 1D vector
for (auto ij : it) {
cout << ij << " ";
}
cout << endl;
}
}
//to sort based on column in descending order
bool mycomp(vector<int>& A, vector<int>& B)
{
//if first row size>second row size
if (A.size() > B.size())
return false; //swap the rows
//other wise no swap
return true;
}
int main()
{
//2D vector initialized with
//use- defined elements only
vector<vector<int> > two_D_vector{
{ 2, 3, 4, 5 },
{ 3, 4, 1 },
{ 1 }
};
//printing the 2D vector
cout << "printing the 2D vector before sorting\n";
print(two_D_vector);
//sorting the 2D array based on a particular row
//here we sort the last row of the 2D vector
//in descending order
//so, basically we sort the 1D array in
//descending order(the last row)
sort(two_D_vector.begin(), two_D_vector.end(), mycomp);
//print the 2D vector
cout << "printing the 2D vector after sorting\n";
print(two_D_vector);
return 0;
}
Output:
输出:
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) Can you sort based on column size anyway?
(b)仍然可以根据列大小进行排序吗?
If you can't sort in that way, then do comment why you cant?
如果您无法通过这种方式进行排序,那么请评论一下您为什么不能这样做?
翻译自: https://www.includehelp.com/stl/sort-a-2d-vector-in-cpp.aspx
c ++向量库