如何对2D数组进行排序。 [英] How to sort a 2D array.

查看:98
本文介绍了如何对2D数组进行排序。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。

如何逐行排序2D数组,而不是一次排序整个数组。

是否可以使用STL Sort来执行此操作。谢谢。

解决方案

你可以使用两个(嵌套的)for循环,如果a [i] [j]< a [i] [j-1]交换(a [i] [j],a [i] [j-1]。

但专家会告诉你阵列基本上都是一维的因此,以上述方式排序不会对整个数组进行排序。


如果您有权访问stl,则可以访问向量,不应使用数组。

基本上你可以使用std :: sort来完成这项工作,但你如何确定1行比另一行更大?


@Banfa


你好Banfa。

你能给出一个如何在2D数组中使用std :: sort的例子你已经知道了,让我们说row1> row2。谢谢

Hi.
How to sort a 2D array row by row, not the whole array at once.
Is it possible to use the STL Sort to do this. Thanks.

解决方案

You could use two (nested) for loops and if a[i][j]< a[i][j-1]swap(a[i][j],a[i][j-1].
But the experts will tell you that arrays are all essentially one dimensional so sorting in the above fashion will not sort the full array.


If you have access to the stl you have access to vectors and should not be using arrays.

Essentially you can use std::sort to do the job but how do you determine that 1 row is greater than another?


@Banfa
Hi Banfa.
Can you give an example how to use std::sort in 2D arrays and you know already, let''s say row1 > row2. Thanks


这篇关于如何对2D数组进行排序。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆