Learn C++ · lesson 57 of 61 · about 11 minutes
Project: Leaderboard
Rank, filter and summarise a list of players using algorithms and lambdas.
What you'll learn
- Sort structs by a field, with a tie-breaker
- Filter and count with lambdas
- Summarise with accumulate
The castle tournament
The adventure ends at the castle's jousting tournament, and the hero keeps the leaderboard. Each jouster is typed as a name and points, like Bo 510, until done. The struct Jouster and the reading are already written for you, and leave a vector called jousters. Your part is the leaderboard: rank them, most points first with ties in name order, number the top three, then give the points in all and how many jousters beat the average.
The plan
Each player has a name and a score, read from lines like Ada 340. The leaderboard will:
1. Store the players in a std::vector<Player>
2. Sort them by score, highest first, with ties in name order
3. Print the top three with their places
4. Report the average score and how many players beat it
Sorting with a tie-breaker
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
struct Player {
std::string name;
int score;
};
int main() {
std::vector<Player> ps = {{"Cy", 300}, {"Ada", 510}, {"Bo", 300}};
std::sort(ps.begin(), ps.end(), [](const Player& a, const Player& b) {
if (a.score != b.score) return a.score > b.score;
return a.name < b.name;
});
for (std::size_t i = 0; i < ps.size(); i++) std::cout << i + 1 << ". " << ps[i].name << " " << ps[i].score << "\n";
}Output
1. Ada 510
2. Bo 300
3. Cy 300Summarising
std::accumulate (from <numeric>) can add up a field using a lambda that takes the running total and the next player. Capture the average in a count_if lambda to count who beat it.
Average and above average
#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
struct Player {
std::string name;
int score;
};
int main() {
std::vector<Player> ps = {{"Ada", 510}, {"Bo", 300}, {"Cy", 300}};
int total = std::accumulate(ps.begin(), ps.end(), 0, [](int sum, const Player& p) { return sum + p.score; });
double avg = double(total) / ps.size();
auto above = std::count_if(ps.begin(), ps.end(), [avg](const Player& p) { return p.score > avg; });
std::cout << "Average " << avg << ", " << above << " above\n";
}Output
Average 370, 1 aboveBeating the average without a fraction
The average is the total divided by the number of entrants, and a jouster beats it when points > total / entrants. Multiply both sides by the number of entrants and it becomes points * entrants > total, which stays in whole numbers, so there's no rounding to worry about. The count_if lambda captures both, [totalPoints, entrants].
Tip
A tie-breaker in a sort lambda: compare the main thing first, and only if it's equal, compare the next thing.
Now try it yourself
This lesson is free in Codiqo, with 8 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.