我们已经讨论了在下面的集合1和集合2中对2D向量进行排序的一些情况。 C++(1)中的二维向量排序(行和列) C++ 2集中二维向量的排序(按行和列的降序) 本文将讨论更多的案例 正如在本集发表的一篇文章中提到的,2D向量也可以有不同列数的行。此属性与2D数组不同,2D数组中所有行的列数相同。
null
CPP
// C++ code to demonstrate 2D Vector // with different no. of columns #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}}; // Displaying the 2D vector for ( int i=0; i<vect.size(); i++) { //loop till the size of particular //row for ( int j=0; j<vect[i].size() ;j++) cout << vect[i][j] << " "; cout << endl; } return 0; } |
输出:
1 23 4 56
案例5:根据行中的列数按升序排序2D向量。 在这种类型的排序中,2D向量是根据列的数量按升序排序的。这是通过在“sort()”中传递第三个参数作为对用户定义的显式函数的调用来实现的。
CPP
// C++ code to demonstrate sorting of // 2D vector on basis of no. of columns // in ascending order #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 no. of columns in // ascending order bool sizecom( const vector< int >& v1, const vector< int >& v2) { return v1.size() < v2.size(); } int main() { // Initializing 2D vector "vect" with // values vector< vector< int > > vect{{1, 2}, {3, 4, 5}, {6}}; // Displaying the 2D vector before sorting cout << "The Matrix before sorting is:"; for ( int i=0; i<vect.size(); i++) { //loop till the size of particular //row for ( int j=0; j<vect[i].size() ;j++) cout << vect[i][j] << " "; cout << endl; } //Use of "sort()" for sorting on //basis of no. of columns in //ascending order. sort(vect.begin(), vect.end(), sizecom); // Displaying the 2D vector after sorting cout << "The Matrix after sorting is:"; for ( int i=0; i<vect.size(); i++) { //loop till the size of particular //row for ( int j=0; j<vect[i].size() ;j++) cout << vect[i][j] << " "; cout << endl; } return 0; } |
输出:
The Matrix before sorting is:1 2 3 4 5 6 The Matrix after sorting is:6 1 2 3 4 5
案例6:根据行中的列数降序排列2D向量。 在这种类型的排序中,2D向量是根据列的数量降序排序的。这是通过在“sort()”中传递第三个参数作为对用户定义的显式函数的调用来实现的。
CPP
// C++ code to demonstrate sorting of // 2D vector on basis of no. of columns // in descending order #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 no. of columns in // descending order bool sizecom( const vector< int >& v1, const vector< int >& v2) { return v1.size() > v2.size(); } int main() { // Initializing 2D vector "vect" with // values vector< vector< int > > vect{{1, 2}, {3, 4, 5}, {6}}; // Displaying the 2D vector before sorting cout << "The Matrix before sorting is:"; for ( int i=0; i<vect.size(); i++) { //loop till the size of particular //row for ( int j=0; j<vect[i].size() ;j++) cout << vect[i][j] << " "; cout << endl; } //Use of "sort()" for sorting on //basis of no. of columns in //descending order. sort(vect.begin(), vect.end(), sizecom); // Displaying the 2D vector after sorting cout << "The Matrix after sorting is:"; for ( int i=0; i<vect.size(); i++) { //loop till the size of particular //row for ( int j=0; j<vect[i].size() ;j++) cout << vect[i][j] << " "; cout << endl; } return 0; } |
输出:
The Matrix before sorting is:1 2 3 4 5 6 The Matrix after sorting is:3 4 5 1 2 6
本文由 曼吉星 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END