c++ - 创建排行榜,需要帮助将总分数组与 "player"数组配对

标签 c++ arrays sorting

正如标题所说,我正在尝试为我制作的网站创建排行榜。我的 C++ 程序的目标是对每个玩家的分数进行排序,然后从最高到最低显示分数,显示与他们的分数相关的名称。我需要帮助的问题是在我对代码中的点进行排序后,在对它们进行排序后,玩家的名字不再与正确的人匹配。 我无法弄清楚如何在排序后再次将玩家数组与分数数组配对。所以如果有人能看到我能做什么或任何提示会很棒。

此外,分数来自外部来源,因此每次运行此程序时我都手动输入分数。

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>


const int MAX_NAMES = 41;

using namespace std;

int main()
{
int points[MAX_NAMES];
int j = 1;

// Array of names of each person on the leaderboard. 
string names[]
{
    "Austin ",
    "Jarred ",
    "Cameron ",
    "Mike ",
    "Blake ",
    "Mitch ",
    "Juan ",
    "Justus ",
    "Avery ",
    "Nick ",
    "Garrett ",
    "Dillion ",
    "Ryan ",
    "Andrew ",
    "Brendan ",
    "Justin ",
    "Jared ",
    "Steve ",
    "Dylan ",
    "Kylor ",
    "Ian ",
    "Josh ",
    "Jake ",
    "Kevin ",
    "Nick ",
    "Marco ",
    "Patrick ",
    "Danny ",
    "Jay ",
    "Bryson ",
    "Mitchell ",
    "Noah ",
    "Tyler ",
    "Andrew ",
    "Evan ",
    "Casey ",
    "Mikey ",
    "Hunter ",
    "Luke ",
    "Colton ",
    "Harbir ",
};

// 1. Manually input score for each person and saves it into array. 
cout << right << setw(50) << "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-" << endl;
cout << right << setw(55) << "INPUT TOTAL BETA POINT SCORE" << endl;
cout << right << setw(50) << "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-" << endl;

for (int i = 0; i < MAX_NAMES; i++)
{
    cout << right << setw(40) << names[i] << " : ";
    cin >> points[i];
}


// 2. organizes from highest to lowest
for (int k = 40; k >= 0; k--)
{
    for (int x = 0; x < MAX_NAMES; x++)
    {
        if (points[x] < points[x + 1])
        {

            int temp = points[x + 1];

            points[x + 1] = points[x];
            points[x] = temp;
        }

    }
}


cout << right << setw(50) << "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-" << endl;
cout << right << setw(35) << "SORTED" << endl;
cout << right << setw(50) << "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-" << endl;

for (int i = 1; i < MAX_NAMES; i++)
{
    cout << i << ") " << points[i] << endl;
}


// 3. Output totals into a html formatted file.
//ofstream outfile;
//outfile.open("total.txt")


system("pause");
return 0;
}

最佳答案

这个问题的一个解决方案是创建一个 pair<int, string> 将分数和玩家姓名存储在同一个数据元素中,然后将它们存储在适合您的任何数组类型中(我会推荐 std::vector ,但我将在我的示例中使用您选择的简单数组)。如 previous answers 中所述, pair<T1, T2>重载了 operator<定义首先比较第一个元素,然后比较第二个元素。 std::sort 将利用这一点:

#include<utility>   // for pair
#include<algorithm> // for sort
#include<string>
#include<iostream>

int main() {
    int MAX_NAMES = 3; // example
    std::pair<int, std::string> scoreboard[MAX_NAMES] = {
        {22, "Anna"}, {11, "Bo"}, {33, "Clare"} // using c++11 inline initialization
    };

    std::sort(scoreboard, scoreboard + MAX_NAMES); // sorts in place using operator<

    for (int i = 0; i < MAX_NAMES; ++i) {
        std::cout << i << ") " << scoreboard[i].second << " has " << scoreboard[i].first << " points" << std::endl;
    }
    return 0;
}

结果:

0) Bo has 11 points
1) Anna has 22 points
2) Clare has 33 points

您可以通过简单地向后遍历 scoreboard 来反转此列表.你可以看到一个工作示例 here .

关于c++ - 创建排行榜,需要帮助将总分数组与 "player"数组配对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48275446/

相关文章:

c++ - Qt C++ C2143 : syntax error : missing ';' before '*'

ruby - 创建范围从 11AA 到 99ZZ 的数组

Java - 字符串的自定义排序ArrayList

bash - 排序和删除一行中的重复单词

c++ - 为什么复杂的模板不起作用?

c++ - 允许图像角点坐标且适用于非网络语言的 map API

python - 在 python 中使用 numpy 获取避免 nan 的平均值

PHP 根据键排序数组,顺序为字母顺序,然后是数字,然后是特殊字符

algorithm - Bubblesort 优于其他排序算法?

c++ - 结合静态断言和断言?