We have discussed some of the cases of sorting 2D vector in below set 1.

Sorting 2D Vector in C++ | Set 1 (By row and column)

More cases are discussed in this article

Case 3 : To sort a particular row of 2D vector in descending order 对二维的vector的行进行降序排列。
This type of sorting arranges a selected row of 2D vector in descending order . This is achieved by using “sort()” and passing iterators of 1D vector as its arguments. 对二维的vector中选定的特定行进行降序排列,由sort()函数实现。

// C++ code to demonstrate sorting of a
// row of 2D vector in descending order
#include<iostream>
#include<vector> // for 2D vector
#include<algorithm> // for sort()
using namespace std; 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:\n"; 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].rbegin(), vect[0].rend()); // Displaying the 2D vector after sorting cout << "The Matrix after sorting 1st row is:\n"; for (int i=0; i<m; i++) { for (int j=0; j<n ;j++) cout << vect[i][j] << " "; cout << endl; } return 0;
}

Output:

The Matrix before sorting 1st row is:
3 5 1
4 8 6
7 2 9
The Matrix after sorting 1st row is:
5 3 1
4 8 6
7 2 9 

Case 4 : To sort the entire 2D vector on basis of a particular column in descending order. 对整个二维的vector基于特定列进行降序排列。
In this type of sorting 2D vector is entirely sorted on basis of a chosen column in descending order. For example if the chosen column is second, the row with greatest value in second column becomes first row, second greatest value in second column becomes second row, and so on. 这种排序二维的vector,是基于选定特定列进行降序排序。例如,如果选定第二列,在第二列中值最大的行放在第一行,列中第二大的值放在第二行。

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

After sorting this matrix by second column, we get

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

This is achieved by passing a third argument in “sort()” as a call to user defined explicit function.

// C++ code to demonstrate sorting of a
// 2D vector on basis of a column 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 particular column in
// descending order
bool sortcol( const vector<int>& v1, const vector<int>& v2 )
{ return v1[1] > v2[1];
} 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:\n"; 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 in descending order sort(vect.begin(), vect.end(),sortcol); // Displaying the 2D vector after sorting cout << "The Matrix after sorting is:\n"; for (int i=0; i<m; i++) { for (int j=0; j<n ;j++) cout << vect[i][j] << " "; cout << endl; } return 0;
}

Output:

The Matrix before sorting is:
3 5 1
4 8 6
7 2 9
The Matrix after sorting is:
4 8 6
3 5 1
7 2 9

This article is contributed by Manjeet Singh .If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Recommended Posts:

  • Sorting of Vector of Tuple in C++ (Descending Order)
  • Sorting Vector of Pairs in C++ | Set 2 (Sort in descending order by first and second)
  • Sorting rows of matrix in descending order followed by columns in ascending order
  • Sorting rows of matrix in ascending order followed by columns in descending order
  • Sorting 2D Vector in C++ | Set 1 (By row and column)
  • Sorting of Vector of Tuple in C++ (Ascending Order)
  • Sort first k values in ascending order and remaining n-k values in descending order
  • Descending order in Map and Multimap of C++ STL
  • Stable sort for descending order
  • Print numbers in descending order along with their frequencies
  • Program to sort string in descending order
  • Sorting a vector in C++
  • Sort prime numbers of an array in descending order
  • Sorting 2D Vector in C++ | Set 3 (By number of columns)
  • Sorting Vector of Pairs in C++ | Set 1 (Sort by first and second)

Sorting 2D Vector in C++ | Set 2 (In descending order by row and column)相关推荐

  1. C4D和Redshift:2D矢量到三维渲染 Cinema 4D and Redshift: 2D vector to 3D render

    C4D和Redshift:2D矢量到三维渲染 时长:1h 4m |视频:. MP4 1280×720,30 fps(r) |音频:AAC,48000 Hz,2ch |大小解压后:633 MB 含课程文 ...

  2. C++ 遍历二维vector

    简单版本 #include <iostream> #include <vector> // for 2D vector using namespace std; int mai ...

  3. DataCamp中Introduction to Python之NumPy

    这一节开始练习numpy Your First NumPy Array In this chapter, we're going to dive into the world of baseball. ...

  4. 对于fmri的设计矩阵构造的一个很直观的解释-by 西南大学xulei教授

    本程序意在解释这样几个问题:完整版代码在本文的最后. 1.实验的设计如何转换成设计矩阵?2.设计矩阵的每列表示一个刺激条件,如何确定它们?3.如何根据设计矩阵和每个体素的信号求得该体素对刺激的敏感性? ...

  5. MATLAB中求矩阵非零元的坐标

    MATLAB中求矩阵非零元的坐标: 方法1: index=find(a); [i,j]=ind2sub(size(a),index); disp([i,j]) 方法2: [i,j]=find(a> ...

  6. sql limit 子句_SQL Order by子句概述

    sql limit 子句 In this article, we'll walk-through the concept of the SQL Order by clause and understa ...

  7. mysql 如何对表排序_学习MySQL:对表中的数据进行排序和过滤

    mysql 如何对表排序 In this article, we will learn how we can sort and filter data using the WHERE clause a ...

  8. matlab2c使用c++实现matlab函数系列教程-find函数

    全栈工程师开发手册 (作者:栾鹏) matlab2c动态链接库下载 matlab库函数大全 matlab2c基础教程 matlab2c开发全解教程 matlab2c调用方法: 1.下载动态链接库 2. ...

  9. sql limit 子句_SQL按子句排序

    sql limit 子句 SQL排序依据 (SQL Order By) SQL Order By clause is used for sorting the result set.SQL Order ...

  10. 9. Data Manipulation with dplyr in R

    文章目录 1. Transforming Data with dplyr 1.1 The countries dataset (video) 1.2 Understanding you data 1. ...

最新文章

  1. Android SERVICE后台服务进程的自启动和保持
  2. linux高性能网络编程读书笔记之socket数据读写
  3. AlphaFold2被超越!中国团队刷新全球蛋白质结构预测纪录,大牛彭健创业项目一鸣惊人...
  4. linux dentry 存储,dentry与dentry_cache
  5. 【AI超级美发师】深度学习算法打造染发特效(附代码)
  6. 设备选购需要注意的几个方面
  7. Android 图形架构
  8. virsh 关机_kvm虚拟机不能使用virsh shutdownw命令关闭虚拟机的解决方法
  9. 使用log4j2免费分配日志记录
  10. 「R」ggplot2拼图包patchwork推荐与使用
  11. py第五天,列表的练习,每日菜谱
  12. springboot集成swagger2,构建优雅的Restful API
  13. linux中安装,编译时调用,运行时调用,更新共享库
  14. HDU--1054--Strategic Game【最小点覆盖】
  15. mongoDB 基础指令
  16. 吴裕雄--天生自然C语言开发:作用域规则
  17. ➤mobi➢azw3➢epub⇨电子书阅读器 转换器
  18. 2022PMP考试敏捷知识点(2)
  19. 计算机毕业设计Java大学生旅游拼团网站(源码+系统+mysql数据库+lw文档)
  20. (转载)New poker 2总算放出新固件了!

热门文章

  1. 茶 与 茶道 之 人生如茶
  2. 分门别类刷leetcode——贪心算法(C++实现)
  3. How a non-windowed component can receive messages from Windows -- AllocateHWnd
  4. Mysql 基准测试
  5. SAP发票校验中支付条件中的基准日期缺省值的配置及控制逻辑测试
  6. @huangcheng: Fedora 9 GDM开启XDMCP
  7. linux怎么安装网卡驱动固件,linux下安装网卡驱动的方法步骤
  8. Idea字体美化终极解决方案
  9. 使用ps命令结束相应进程
  10. 读《哈佛大学》纪录片 后感