c ++向量库_在C ++中对2D向量进行排序
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 vectorfor (auto ij : it) {cout << ij << " ";}cout << endl;} }int main() {//2D vector initialized with //user-defined elements onlyvector<vector<int> > two_D_vector{{ 3, 5, 4 },{ 6, 4, 2 },{ 1, 7, 3 }};//printing the 2D vectorcout << "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 vectorcout << "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 3So 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 vectorfor (auto ij : it) {cout << ij << " ";}cout << endl;} }int main() {//2D vector initialized with //user-defined elements onlyvector<vector<int> > two_D_vector{{ 3, 5, 4 },{ 6, 4, 2 },{ 1, 7, 3 }};//printing the 2D vectorcout << "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 vectorcout << "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 12)根據特定的列排序 (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 vectorfor (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 rowif (A[0] < B[0])return true; //no swap//other wise swap the rowsreturn false; }int main() {//2D vector initialized with //user-defined elements onlyvector<vector<int> > two_D_vector{{ 3, 5, 4 },{ 6, 4, 2 },{ 1, 7, 3 }};//printing the 2D vectorcout << "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 vectorcout << "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 2To 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 vectorfor (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 rowif (A[0] < B[0])return false; //swap the rows//other wise no swapreturn true; }int main() {//2D vector initialized with //user-defined elements onlyvector<vector<int> > two_D_vector{{ 3, 5, 4 },{ 6, 4, 2 },{ 1, 7, 3 }};//printing the 2D vectorcout << "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 vectorcout << "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 3There 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 vectorfor (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 sizeif (A.size() > B.size())return false; //swap the rows//other wise no swapreturn true; }int main() {//2D vector initialized with //use- defined elements onlyvector<vector<int> > two_D_vector{{ 2, 3, 4, 5 },{ 3, 4, 1 },{ 1 }};//printing the 2D vectorcout << "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 vectorcout << "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 ++向量庫
總結
以上是生活随笔為你收集整理的c ++向量库_在C ++中对2D向量进行排序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java方法重载和重载方法_我们可以在J
- 下一篇: java代码中何处以main开始_自测题