使用内置函数(或任何其他方法)在 C++ 中对二维数组进行排序? [英] Sort a 2D array in C++ using built in functions(or any other method)?

查看:38
本文介绍了使用内置函数(或任何其他方法)在 C++ 中对二维数组进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的二维数组.( array[5][2] )

I have a 2D array like below. ( array[5][2] )

20  11    
10  20
39  14
29  15
22  23

排序后应该如下所示.

10  20
20  11
22  23
29  15
39  14

这意味着数组应该只比较第一列的值进行排序.

that means the array should be sorted comparing the first column values only.

在 Java 中有一个内置的函数功能可以做到这一点.如下图.

In Java there is a built in function capability to do that. like below.

Arrays.sort(a, new Comparator<Long[]>() {

            @Override
            public int compare(Long[] o1, Long[] o2) {

                Long t1 = o1[1];
                Long p1 = o1[0];
                Long t2 = o2[1];
                Long p2 = o2[0];

                if (t1 == t2) {
                    return (p1 > p2 ? 1 : (p1 == p2 ? 0 : -1));
                } else {
                    return (t1 < t2 ? -1 : 1);
                }

            }
        });

那么是否有任何 C++ 内置函数功能来执行此类操作,或者我如何在 C++ 中执行此操作(最快的实现)?

So is there any C++ built in function capability to do these kind of stuff or how can i do this in C++ (the fastest implementation) ?

提前致谢:)

推荐答案

我提供这个只是因为它是 std::qsort 做得好的少数事情之一em> std::sort 根本就没有,即对多列固定数组进行排序:比较器是一串三元语句,但如果你盯着它看够久的话应该足够清楚:

I'm offering this up only because it was one of the few things std::qsort does well that std::sort simply does not, namely sort multi-column fixed arrays: The comparator is a string of ternary statements, but should be clear enough if you stare at it long enough:

#include <iostream>
#include <random>
#include <algorithm>

int main()
{
    int ar[10][2];

    // populate with random data
    std::random_device rd;
    std::default_random_engine rng(rd());
    std::uniform_int_distribution<> dist(1,20);
    std::for_each(std::begin(ar), std::end(ar),
        [&](int (&ar)[2]){ ar[0] = dist(rng); ar[1] = dist(rng); });

    std::cout << "Before Sort..." << '\n';
    std::for_each(std::begin(ar), std::end(ar),
        [](const int(&ar)[2]) { std::cout << ar[0] << ',' << ar[1] << '\n';});

    std::qsort(ar, 10, sizeof(*ar),
        [](const void *arg1, const void *arg2)->int
        {
            int const *lhs = static_cast<int const*>(arg1);
            int const *rhs = static_cast<int const*>(arg2);
            return (lhs[0] < rhs[0]) ? -1
                :  ((rhs[0] < lhs[0]) ? 1
                :  (lhs[1] < rhs[1] ? -1
                :  ((rhs[1] < lhs[1] ? 1 : 0))));
        });

    std::cout << "After Sort..." << '\n';
    std::for_each(std::begin(ar), std::end(ar),
        [](const int(&ar)[2]) { std::cout << ar[0] << ',' << ar[1] << '\n';});

    return 0;
}

样品运行(很明显,您的会有所不同)

Sample Run (yours will vary, obviously)

Before Sort...
2,11
18,4
20,20
14,6
8,10
17,8
14,14
3,10
20,14
19,19
After Sort...
2,11
3,10
8,10
14,6
14,14
17,8
18,4
19,19
20,14
20,20

注意:这在比较器中专门使用了严格值比较而不是减法捷径,以避免潜在的下溢问题.如果这在您受限的数据空间中不是问题,您可以轻松地使比较器变得更加简单.

Notes: this specifically uses strict-value comparison rather than subtraction short-cuts in the comparator so as to avoid potential underflow issues. If that is not a problem in your restricted data-space, you could easily make that comparator significantly simpler.

这篇关于使用内置函数(或任何其他方法)在 C++ 中对二维数组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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