Public Group

# How To Make A Scoreboard?

This topic is 4526 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi there. I want to be able to make a scoreboard, where different players or teams are sorted by the number of points they have scored. (Think of this as competition points in a sports competition, for example, rather than the scores within a particular match.) How would I go about this? Would I need to use a map of some sort? I really need a few suggestions to point me in the right direction, or even a template of some sort.

##### Share on other sites
Since speed is not a big factor here, it seems you could use a map (indexed by score), or perhaps another container along with std::sort(). You could create a struct that held the display info for each team, including the score, and overload the < operator to sort by score (and alphabetically, for example, for ties). Then just apply std::sort() to the container and you're good to go.

That's just one idea; there are probably many ways you could manage a sorted scoreboard.

##### Share on other sites
Quote:
 Original post by JesbassHi there.I want to be able to make a scoreboard, where different players or teams are sorted by the number of points they have scored. (Think of this as competition points in a sports competition, for example, rather than the scores within a particular match.)How would I go about this? Would I need to use a map of some sort? I really need a few suggestions to point me in the right direction, or even a template of some sort.

No, you don't need to use a map. For starters, a map is an associative container and you want a sequential container.

For a small number of entries (under a hundred or so) you could even store the lists in a giant array of plain text, parse them every step, and sort them with a bubble sort. Nothing fancy is required, and performance on a modern PC would be a non-issue.

If you want to use a C++ class, the priority_queue container class might be a good option for you. Or just call the sort function from <algorithm> on a regular vector.

Fill the container with sortable objects by implementing the less operator on your team or player class, and your job is done.

##### Share on other sites
Using the standard library sort is probably easiest. Easier than writing and debugging your own sort, even if it is bubble sort ;)

For example (not tested):

#include <string>#include <vector>#include <algorithm>class Team {  int score;  std::string name;  public:  Team(const std::string& name) : score(0), name(name) {}  bool operator<(const Team& other) const {    return score < other.score;  }};struct TeamPointerComparer {  bool operator()(const Team* a, const Team* b) {    return (*a) < (*b);  }};Team myTeam, yourTeam; // for example// This references the Teams that already exist in your program. It does *not*// "own" the pointed-to objects, so you should *not* 'delete' things out of the// vector.std::vector<Team*> highScoreList;highScoreList.push_back(&myTeam);highScoreList.push_back(&yourTeam);std::sort(highScoreList.begin(), highScoreList.end(), TeamPointerComparer());// Of course, if you are *already* holding your teams in some sort of container// (which is likely to be a good idea), then you can define the operator<// for Team, and just sort the container directly using the 2-arg form// of std::sort:// std::vector<Team> allTeams;// std::sort(allTeams.begin(), allTeams.end()); // uses Team::operator<

• 10
• 17
• 9
• 13
• 41