Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Ranking

The teacher would also like to see the whole class in order, from the highest score to the lowest. That's a job for std::sort, with a lambda that says when one student goes before another: when they have more points, a.score > b.score.

But which vector should it sort? Not the one in main(): the results would then be printed in a different order too. And printResults() can't sort its students, because through a const reference, it can only read them. The compiler then reports errors from inside the library, the first of them this one:

/usr/include/c++/v1/__algorithm/sift_down.h:62:22: error: no viable overloaded '='

Further down, a note with main.cpp names the line with your call of std::sort.

What you need is a copy. You know from the lesson on functions that a parameter without & gets a copy of the argument. Usually, that's something to avoid. Here, it's exactly what you want: a function printRanking(std::vector<Student> students) gets its own copy of the class, sorts it and prints it, and the vector in main() stays as it was.

The names go on one line, with a comma and a space between them, but not after the last one. So print the comma before each name, except before the first one, which has the index 0:

std::vector<std::string> colors = {"red", "green", "blue"};
for (int i = 0; i < colors.size(); i++) {
    if (i > 0) {
        std::cout << ", ";
    }
    std::cout << colors[i];
}
std::cout << std::endl;
red, green, blue

If you see Ranking: Jack, Lucy, Emma, Adam, Finn, the lambda puts the lowest score first: it needs >, not <. And when two students have the same score, std::sort may put either of them first.

Instructions

  1. 1

    Between bestStudent() and printResults(), write a function void printRanking(std::vector<Student> students), without &, that sorts students with std::sort and a lambda from the highest score to the lowest.

  2. 2

    In printRanking(), after sorting, print Ranking: and the names in their new order on one line, with a comma and a space between them. Call printRanking(students); at the end of printResults(). The results above it stay in the order of the vector, and a new line appears under the best student:

    Best: Finn (48 points)
    Ranking: Finn, Adam, Emma, Lucy, Jack
Back

5/8

  1. 1. Test Results
  2. 2. Grades
  3. 3. Class Average
  4. 4. Top Score
  5. 5. Ranking
  6. 6. Bonus Points
  7. 7. Finding a Student
  8. 8. Congratulations!
Next

Try it yourself

Do this exercise right here in your browser. No account needed.