创建一个排行榜,需要帮助将一组总点数与“玩家”阵列配对

时间:2022-11-29 02:09:35

So as the title says I am trying to create a leader board for a website I made. The goal of my c++ program was to sort the points for each player and then display the points from highest to lowest showing the name that correlates with their score. The problem that I need help with is after i sorted the points in the code, after they are sorted the player's name is no longer matched to the correct person. I cant figure out how to pair the player array with the score array again after they have been sorted. So if anyone could see what i could do or any tips would be amazing.

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

Also, the scores come from an outside source so i manually input scores each time this program is run.

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

#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;
}

3 个解决方案

#1


0  

You need to sort the lists at the same time to maintain the relative order. Eg,

您需要同时对列表进行排序以维护相对顺序。例如,

        int temp = points[x + 1];  

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

        string temp_name = names[x + 1];

        names[x + 1] = names[x];
        names[x] = temp_name;

#2


2  

One solution to this problem is to create a pair<int, string> to store both the score and the player name in the same data element, then store those in whatever array type suits you (I would recommend std::vector, but I will use your choice of a simple array in my example). As noted in previous answers, pair<T1, T2> has an overloaded operator< defined that compares the first element first, then the second element. std::sort will take advantage of that:

这个问题的一个解决方案是创建一个对 来将得分和玩家名称存储在同一个数据元素中,然后存储那些适合你的数组类型(我会推荐std :: vector,但是我将在我的示例中使用您选择的简单数组)。如前面的答案所述,对 有一个重载的运算符 ,它首先比较第一个元素,然后比较第二个元素。> ,t2> ,string>

#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;
}

Result:

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

You can reverse this list by simply iterating backward through scoreboard. You can see a working example here.

您可以通过向后遍历记分板来反转此列表。你可以在这里看到一个有效的例子。

#3


1  

Thinking in the future of your project, you should rely on encapsulation. Create a Player class with the properties name and points, in the future you might want to add more functionality to your players! And you don't want to break your code, so encapsulation is the way to go.

考虑到项目的未来,您应该依赖封装。使用属性名称和点创建一个Player类,将来您可能希望为玩家添加更多功能!而且你不想破坏你的代码,所以封装是要走的路。

You should try to use the algorithm library, it will make your code more expressive, and thus easier to maintain.

您应该尝试使用算法库,它将使您的代码更具表现力,从而更易于维护。

Try to use standard containers to manage your memory. The usual recommendation is to use vector for dynamic memory.

尝试使用标准容器来管理你的记忆。通常的建议是使用矢量来动态记忆。

Here you have an example of a simple implementation of these tips, try your own!

这里有一个简单实现这些技巧的例子,试试你自己!

#include<algorithm>
#include<vector>
#include<string>
#include<iostream>

class Player {
    std::string name;
    int points;
    public:
    Player(std::string name, int points) : name(name), points(points) {}
    bool operator<(const Player &rhs) const {return points < rhs.points;}
    std::string get_name() const {return name;}
    int get_points() const {return points;}
};

int main() {
    std::vector<Player> players;
    players.push_back(Player("Pedro",50));
    players.push_back(Player("Andres",20));
    players.push_back(Player("Santiago",57));

    std::sort(players.begin(),players.end()); 

    for (const auto& player : players) {
        std::cout << player.get_name() << ": " << player.get_points() << "\n";
    }
}

Here I use the sort algorithm to sort the players in the vector. Sort uses the operator <, that's why you see the operator overload in the Player class. This tells how (player1 < player2) should behave (in our case, it's just comparing their points. But you could do anything, such as adding a bonus property to the Player class)

在这里,我使用排序算法对向量中的玩家进行排序。 Sort使用operator <,这就是你在Player类中看到运算符重载的原因。这告诉了(player1 )应该如何表现(在我们的例子中,它只是比较他们的分数。但你可以做任何事情,比如在player类中添加奖励属性)

Another recommendation is to separate the Player's class into a different file to keep things more structured.

另一个建议是将Player的类分成不同的文件以使事情更加结构化。

#1


0  

You need to sort the lists at the same time to maintain the relative order. Eg,

您需要同时对列表进行排序以维护相对顺序。例如,

        int temp = points[x + 1];  

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

        string temp_name = names[x + 1];

        names[x + 1] = names[x];
        names[x] = temp_name;

#2


2  

One solution to this problem is to create a pair<int, string> to store both the score and the player name in the same data element, then store those in whatever array type suits you (I would recommend std::vector, but I will use your choice of a simple array in my example). As noted in previous answers, pair<T1, T2> has an overloaded operator< defined that compares the first element first, then the second element. std::sort will take advantage of that:

这个问题的一个解决方案是创建一个对 来将得分和玩家名称存储在同一个数据元素中,然后存储那些适合你的数组类型(我会推荐std :: vector,但是我将在我的示例中使用您选择的简单数组)。如前面的答案所述,对 有一个重载的运算符 ,它首先比较第一个元素,然后比较第二个元素。> ,t2> ,string>

#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;
}

Result:

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

You can reverse this list by simply iterating backward through scoreboard. You can see a working example here.

您可以通过向后遍历记分板来反转此列表。你可以在这里看到一个有效的例子。

#3


1  

Thinking in the future of your project, you should rely on encapsulation. Create a Player class with the properties name and points, in the future you might want to add more functionality to your players! And you don't want to break your code, so encapsulation is the way to go.

考虑到项目的未来,您应该依赖封装。使用属性名称和点创建一个Player类,将来您可能希望为玩家添加更多功能!而且你不想破坏你的代码,所以封装是要走的路。

You should try to use the algorithm library, it will make your code more expressive, and thus easier to maintain.

您应该尝试使用算法库,它将使您的代码更具表现力,从而更易于维护。

Try to use standard containers to manage your memory. The usual recommendation is to use vector for dynamic memory.

尝试使用标准容器来管理你的记忆。通常的建议是使用矢量来动态记忆。

Here you have an example of a simple implementation of these tips, try your own!

这里有一个简单实现这些技巧的例子,试试你自己!

#include<algorithm>
#include<vector>
#include<string>
#include<iostream>

class Player {
    std::string name;
    int points;
    public:
    Player(std::string name, int points) : name(name), points(points) {}
    bool operator<(const Player &rhs) const {return points < rhs.points;}
    std::string get_name() const {return name;}
    int get_points() const {return points;}
};

int main() {
    std::vector<Player> players;
    players.push_back(Player("Pedro",50));
    players.push_back(Player("Andres",20));
    players.push_back(Player("Santiago",57));

    std::sort(players.begin(),players.end()); 

    for (const auto& player : players) {
        std::cout << player.get_name() << ": " << player.get_points() << "\n";
    }
}

Here I use the sort algorithm to sort the players in the vector. Sort uses the operator <, that's why you see the operator overload in the Player class. This tells how (player1 < player2) should behave (in our case, it's just comparing their points. But you could do anything, such as adding a bonus property to the Player class)

在这里,我使用排序算法对向量中的玩家进行排序。 Sort使用operator <,这就是你在Player类中看到运算符重载的原因。这告诉了(player1 )应该如何表现(在我们的例子中,它只是比较他们的分数。但你可以做任何事情,比如在player类中添加奖励属性)

Another recommendation is to separate the Player's class into a different file to keep things more structured.

另一个建议是将Player的类分成不同的文件以使事情更加结构化。