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

Quiz: Sort Order

What does this program print?

1#include <iostream>
2#include <algorithm>
3#include <string>
4#include <vector>
5
6struct Monster {
7    std::string name;
8    int health;
9    int attack;
10};
11
12int main() {
13    std::vector<Monster> monsters = {
14        {"Goblin", 30, 5},
15        {"Troll", 80, 7},
16        {"Bat", 8, 2},
17        {"Slime", 15, 1},
18    };
19    std::sort(monsters.begin(), monsters.end(), [](const Monster& a, const Monster& b) {
20        return a.attack < b.attack;
21    });
22
23    for (const Monster& monster : monsters) {
24        std::cout << monster.name << std::endl;
25    }
26    return 0;
27}
Back

5/13

  1. 1. Sorting: std::sort
  2. 2. Reversing: std::reverse
  3. 3. A Comparison Function
  4. 4. Lambdas
  5. 5. Quiz: Sort Order
  6. 6. Counting: std::count_if
  7. 7. Capturing Variables
  8. 8. Searching: std::find
  9. 9. Searching with a Lambda: std::find_if
  10. 10. Quiz: Searching
  11. 11. The Largest Element: std::max_element
  12. 12. Yes or No: std::any_of and std::all_of
  13. 13. Summary
Next

Try it yourself

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