c++ - 如何在 C++ 中按列对多维数组进行排序?

标签 c++ arrays algorithm sorting multidimensional-array

你知道如何在 C++ 中对多维数组进行排序吗?

我的输入是这样的:

Bogaerts_X  144 12  138
Cespedes_Y  51  5   48
Gomes_J     78  6   70
Holt_B      106 4   98
Napoli_M    119 17  133
Nava_D      113 4   81
Ortiz_D     142 35  95
Pedroia_D   135 7   75
Pierzynski_A72  4   40
Ross_D      50  7   58

我想按照第4列降序排列,我的代码包括sort()函数是这样的:

#include <iostream>
#include <fstream>  //Required for fin.open, fin.close, fout.open, fout.close
#include <cstdlib>  //Required for srand(), rand().
#include <ctime>    //Required for time(NULL) to seed the random num gen

using namespace std;


// Declaration of the main function
int main()
{
    ofstream fout;
    ifstream fin;
    string array[100][100];
    int limit(0);

    fin.open("312.txt" );

    cout << " -------------------------------" << endl << endl;

    for (int i=0; i<12; ++i)    //every row
    {
        for (int j=0; j<4; ++j )//every col
        {
            fin >> array[i][j];
        }
    }

    for (int i=0; i<12; ++i)    //every row
    {
        for (int j=0; j<4; ++j )//every col
        {
            cout << "\t" << array[i][j];
        }
        cout << endl;
    }


    //sort players according to the 4th column




    //Asks the user for a limit of home runs to search by
    cout << "Give me the limit of home runs to search by"<<endl;
    cin >> limit;


    //sorted alphabetically and displays
    for (int i=0; i<limit; ++i) //every row
    {
        for (int j=0; j<4; ++j )//every col
        {
            cout << "\t" << array[i][j];
        }
        cout << endl;
    }


    fin.close();

    cout << endl << endl << endl << endl << endl << endl << endl;

    // Exit program.
    return 0;
}

//This sample function sorts the array with n elements
//into ascending order using selection sort

void sort(const double a[], int n)
{
    double temp; int m; int x[0];
    for (int k=0; k<=n-2; ++k) {
        //find position of smallest element beginning at k
        m = k;
        for (int j=k+1; j < n-1; ++j)
            if (a[j] < a[m])
                m = j;
        //exchange smallest value with value at k
        temp = x[m];
        x[m] = x[k];
        x[k] = temp;
    } //end for (k)
} //end sort()

如何使用此排序功能按第 4 列排序?我真的很困惑......

最佳答案

当项目按某种结构分组时,对项目进行排序会容易得多。

struct Data
{
    std::string name_;
    int x_, y_, z_;
};

int main()
{
    std::vector<Data> data;

    Data d1 = { "Bogaerts_X", 144, 12, 138 };
    Data d2 = { "Cespedes_Y", 51, 5, 48 };

    data.push_back(d1);
    data.push_back(d2);

    std::sort(std::begin(data), std::end(data), [](const Data& a, const Data& b)
    {
        // sort based on the last member variable or 4th column in your case
        return a.z_ < b.z_;
    });

    return 0;
}

关于c++ - 如何在 C++ 中按列对多维数组进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33892630/

相关文章:

ios - 快速切换与数组匹配的模式

java - 在二维数组中移动元素

algorithm - 如何有效地从 Fenwick 树中找到连续范围的已用/空闲槽

c++ - 如何提高大整数的乘法效率?

c# - 用于从一组数字中确定所有可能的和的非递归算法

c++ - 如何使用 Gradle DSL(领域特定语言)上的文档?

c++ - .begin() 和 .end() 的意义何在?

c++ - GDB 问题 : Pretty-Printing a 2D Array?

Javascript:Is-SubArray 返回 false

c++ - 使用 list 会导致窗口部分保持空白