按降序对包含玩家姓名和分数的文本文件进行排序

Sorting a textfile that contains player name and score in descending order

我正在尝试打开一个文本文件,然后按降序重新排列它,以显示得分最高的人。在文本文件中有玩家姓名和他们的分数。

我已经设法用 C++ 打印出文本文件,但我找不到对其进行排序的方法,因为变量在文本文件中。

#include <string>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <functional>

using namespace std;

struct player {
    string name;
    int score;
    int position;
};

int main()
{
    string line;
    ifstream inFile;
    inFile.open("C:/Users/kkpet/Desktop/highscore.txt");
    if (inFile.is_open()) {
        while (getline(inFile, line)) {
            player x;
            ifstream inFile;
            inFile.open("C:/Users/kkpet/Desktop/highscore.txt");
            cout << line << '\n';
        }
        inFile.close();
    }
    else
        cout << "Unable to open text";
}

假设您的文本文件如下所示:

Name1 1
Name2 1
Name4 5
Name3 6

你可以这样做:

#include <string>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <functional>
#include <vector>


int main()
{
    std::string line;
    std::ifstream inFile;
    inFile.open("/C:/Users/kkpet/Desktop/highscore.txt");
    if (inFile.is_open()) {
        std::vector<std::pair<int, std::string> > score_vector;
        std::string name;
        int score;
        while (inFile >> name >> score) {
            score_vector.push_back(std::make_pair(score, name));
            std::cout << line << '\n';
        }
        inFile.close();
        std::sort(score_vector.begin(), score_vector.end());
        std::reverse(score_vector.begin(), score_vector.end());
        for(auto it = score_vector.begin(); it != score_vector.end(); ++it){
            std::cout << "Name: " << it->second << " Score: " << it->first << std::endl;
        }
    }
    else
        std::cout << "Unable to open text";
}

你首先使用inFile << name << score逐行读取文件,直接给你玩家的名字和分数。然后,您从中创建一对,并将分数作为第一个,这样可以更轻松地进行排序,您也可以使用自己的比较函数按一对中的第二个元素进行排序,但为简单起见,我这样说。然后您可以使用 std::sort 方法轻松地对向量进行排序。之后需要还原。

具有自定义比较功能的完整代码:

#include <string.h>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <functional>
#include <vector>

// This is a compare funciton for the sort algorithm of std::sort. See [1]
bool compareScorePair(std::pair<std::string, int>&a, std::pair<std::string, int>&b){
    if(a.second > b.second){return true;}
    if(a.second == b.second){return a.first.compare(b.first) > 0;}
    return false;
}

int main()
{
    std::ifstream inFile;
    inFile.open("C:/Users/kkpet/Desktop/highscore.txt");
    if (inFile.is_open()) {
        std::vector<std::pair<std::string, int> > score_vector;
        std::string name;
        int score;
        while (inFile >> name >> score) { // Can be used to directly assign istream data to variables. See [2]
            score_vector.push_back(std::make_pair(name, score)); // Storing data as pair, to keep relationships between score and name.
        }
        inFile.close();
        std::sort(score_vector.begin(), score_vector.end(), compareScorePair); // Sort the vector with the custom compare function, See top of code.
        int place = 1;
        //auto is used purely for convenience. auto represents std::vector<std::pair<std::string, int> >::iterator here.
        for(auto it = score_vector.begin(); it != score_vector.end(); ++it){ 
            std::cout << "Place: " << place << " Name: " << it->first << " Score: " << it->second << std::endl;
            ++place;
        }
        // The whole for loop could look like this:
        /*
        for(uint i = 0; i < score_vector.size(); ++i){
            std::string name_out = score_vector[i].first;
            int score_out = score_vector[i].second;
            std::cout << "Place: " << i << " Name: " << name_out << " Score: " << score_out << std::endl;
        }
        */
    }
    else
        std::cout << "Unable to open text";
}

输出:

Place: 1 Name: Name3 Score: 6
Place: 2 Name: Name4 Score: 5
Place: 3 Name: Name2 Score: 1
Place: 4 Name: Name1 Score: 1

链接:
[1]: https://en.cppreference.com/w/cpp/named_req/Compare
[2]: http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/
配对信息:
https://en.cppreference.com/w/cpp/utility/pair
有关迭代器的信息(参见 auto 关键字):
https://www.geeksforgeeks.org/iterators-c-stl/

这个问题不需要用C++!只需在 shell 提示符中输入即可。

sort -rk 2 highscore.txt > sorted_scores.txt

解释:
'sort' 对文件进行排序,通常按第一个字母排序。
-k 2 选项表示按第二列排序。
-r 选项表示反转(因此最高分在最上面)。