【问题标题】:C++ Sort Array by 2nd ValueC++ 按第二个值对数组进行排序
【发布时间】:2021-11-01 09:38:33
【问题描述】:

我刚开始使用 C++,我想知道是否有一种方法可以按每个数组中的第二个值对二维数组进行排序。我在网上没有找到任何方法,所以我在这里问。

例如,您可以这样开始:

int exampleArray[5][2] = {
    {4, 20},
    {1, 4},
    {7, 15},
    {8, 8},
    {8, 1}
};

排序后,数组将是

int exampleArray[5][2] = {
    {8, 1},
    {1, 4},
    {8, 8},
    {7, 15},
    {4, 20}
};

在 Python 中,您可以使用 exampleArray.sort(key = lambda element : element[1]) 对其进行排序,但我不知道如何使用 C++ 进行排序

【问题讨论】:

  • 反问:你试过什么?您可能只是有点错误,我们可以立即解决。
  • @user4581301 我什至不知道如何处理这个问题。
  • 很公平。 C++ 中的数组是 C++ 中的二等公民,并且比您习惯从 Python 中获得的数组更难处理,因为 Python 中的数组在精神上更接近 C++ 的std::vector(但仍然不是一个很好的匹配项)。 Here's an answer that should help you get started。您必须调整传递给 std::qsort 的 lambda 表达式中使用的规则,因为它当前基于第 1 列排序,第 2 列中断。
  • @CoderTang 我在网上没有找到任何方法 -- 想想outside the box

标签: c++ arrays sorting matrix syntax


【解决方案1】:

使用向量要简单得多,但假设你不能使用std::vector,这里有另一种“排序”方式,基于answer given here

#include <iostream>
#include <algorithm>

int main()
{
    int exampleArray[5][2] = {
        {4, 20},
        {1, 4},
        {7, 15},
        {8, 8},
        {8, 1}
    };

    // create the indexing (there are 5 2D entries)
    int index[] = {0,1,2,3,4};

    // sort the index based on the second value in the 2D array    
    std::sort(index, index + 5, [&](int n1, int n2) 
             { return exampleArray[n1][1] < exampleArray[n2][1]; });

    // output results
    for (int i = 0; i < 5; ++i)
       std::cout << exampleArray[index[i]][0] << " " << exampleArray[index[i]][1] << "\n";
}

输出:

8 1
1 4
8 8
7 15
4 20

基本上,您希望根据二维数组中的第二个值对索引数组进行排序。这就是 lambda 函数所说的——索引数组正在“排序”,而不是原始的二维数组。

排序完成后,索引数组用于以排序方式访问二维数组中的项目。

从那里,如果您希望将更改存储在实际的二维数组中,您可以使用排序索引重建原始数组:

    // rebuild the original array using the sorted index
    int tempArray[5][2] = {};
    for (int i = 0; i < 5; ++i)
    {
        tempArray[i][0] = exampleArray[index[i]][0];
        tempArray[i][1] = exampleArray[index[i]][1];
    }
    
    // copy the new sorted array to the original array
    memcpy(exampleArray, tempArray, sizeof(exampleArray));

【讨论】:

    【解决方案2】:

    您可以使用结构来保存数据:

    struct Data
    {
        int x,y;
    };
    vector<Data> a;
    

    使用 std::sort ,您可以使用 lambda 函数来实现您的目标

    std::sort(a.begin(),a.end(),[](Data e1,Data e2){return e1.y&lt;e2.y;});

    【讨论】:

      【解决方案3】:

      简短的回答是:不要那样做。 C++ 从 C 继承了它的内置数组,它根本不适合你正在尝试做的事情。

      相当相似且易于实现的方法是使用std::vector 而不是数组。

      std::vector<std::vector<int>> someVector {
          {4, 20},
          {1, 4},
          {7, 15},
          {8, 8},
          {8, 1}
      };
      

      根据每行中的第二项对其进行排序非常简单:

          std::sort(someVector.begin(), someVector.end(), 
              [](auto const &a, auto const &b) { return a[1] < b[1]; });
      

      然后我们可以打印出结果来验证它是否按预期工作:

      for (auto const &row : someVector)
          std::cout << row[0] << "\t" << row[1] << "\n";
      

      如您所料,这会产生:

      8   1
      1   4
      8   8
      7   15
      4   20
      

      【讨论】:

        猜你喜欢
        • 2011-10-25
        • 1970-01-01
        • 1970-01-01
        • 2014-04-06
        • 2022-01-28
        • 1970-01-01
        • 1970-01-01
        • 2021-05-03
        相关资源
        最近更新 更多