按子数组C中的第一项对数组进行排序 [英] sort array by first item in subarray c++

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

问题描述

我有一个数组数组,例如: [[4,204],[10,39],[1,500]] )。
我想按子数组的第一个元素对它们进行排序,使其变为: [[1,500],[4,204],[10,39]]

I have an array of arrays, for example: [[4, 204], [10, 39], [1, 500]]). I want to sort them by the first element of the subarray so that it becomes: [[1, 500], [4, 204], [10, 39]].

对于 C ++ ,我找不到此问题的任何答案。

I couldn't find any answers to this question for C++.

我将如何在 C ++ 中做到这一点?

How would I do this in C++?

代码:

int n;
int time;  
int timeTable[100][2];

if (inputFile.is_open()) {

    for (int i = 0; i < 100; i++) {
        inputFile >> n;
        timeTable[i][0] = n;
        inputFile >> time;
        timeTable[i][1] = time;
    }

}


推荐答案

对二维数组进行排序的一种方法是不对数组本身进行排序

One way to sort the two dimensional array is to not sort the array itself.

相反,对指向该数组的索引进行排序在数组中,然后使用排序后的索引访问元素。比在排序函数中操作2d数组要容易得多。

Instead, sort an array of indices that point inside of the array, and then use the sorted index to access the elements. It is much easier to do that than to manipulate a 2d array in a sorting function.

通常,如果您有索引数组的内存(我们将需要索引数组的额外存储),对具有较大有效负载或只是笨拙的 1 或无法在排序算法中轻松操作的对象进行排序的方法,可以使用此技术。

In general, if you have the memory for the array of indices (we will need additional storage for the index array), sorting objects that have a large payload or are just clumsy1 or impossible to manipulate easily in a sorting algorithm can use this technique.

这里是一个示例:

#include <algorithm>
#include <iostream>

int main()
{
    int index[3] = {0,1,2};
    int timeTable[3][2] = {{4, 204}, {10, 39}, {1, 500}};
    std::sort(index, index + 3, [&](int n1, int n2){ return timeTable[n1][0] < timeTable[n2][0]; });
    for (int i = 0; i < 3; ++i)
       std::cout << "The index is " << index[i] << ".  The data at this index is  [" << 
                 timeTable[index[i]][0] << " " << timeTable[index[i]][1] << "]\n";
}

实时示例

因此,基本上,我们创建了一个初始索引数组,其编号从0到 n-1 其中 n 是要排序的项目数。然后我们在索引上调用 std :: sort ,在排序标准中,我们使用传递给排序谓词的索引来比较实际数组中的项目。结果将是索引数组在排序期间交换其元素,而不是交换原始数组的元素。

So basically we create an initial index array numbered from 0 to n-1 where n are the number of items to sort. Then we call std::sort on the index, and in the sorting criteria, we compare the items in the actual array using the indices passed to the sorting predicate. The result will be the index array having its elements swapped around during the sort instead of the original array's elements being swapped.

请注意,排序谓词非常简单-我们通过使用在 timeTable 数组中传递的索引,准确说明我们希望索引在排序中表示的内容。如果其中一项发生故障,没有棘手的,容易出错的代码或无法很好地扩展的代码(例如,并行数组方案-想象一下20个数组,并且仅因为一项出现故障而不得不交换20个项目)

Note that the sorting predicate is very simple -- we state exactly what we want the indices to represent in the sort by using the indices being passed on the timeTable array. There is no tricky, error-prone, or code that does not scale well if one of the items is out of order (for example, the parallel array scenario -- imagine 20 arrays and having to swap 20 items just because one item is out of order).

对索引进行排序后,当需要使用 timeTable 数组时,我们使用指向项目的索引数组(请注意我们如何通过使用 timeTable [index [i]] [0] 而不是 timeTable [i]指定索引] [0] )。

After sorting the indices, when it comes time to use the timeTable array, we use the index array to point to the items (note how we specify the index by using timeTable[index[i]][0] instead of timeTable[i][0]).


1 包括在笨拙类别中的那些出现在StackOverflow上的并行
数组排序问题,其中要求发布者根据其中一个数组中的数据并行对多个数组进行排序。

1 Included in the "clumsy" category are those "parallel array sort" questions that show up on StackOverflow, where the poster is asked to sort multiple arrays "in parallel" based on data in one of those arrays.

这篇关于按子数组C中的第一项对数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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