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

Summary

In this lesson, you learned that:

  • An algorithm is a recipe that solves a task step by step, and the library <algorithm> has many of them ready-made. They work on a range: v.begin(), v.end() is the whole vector, where end() is the position after the last element.
  • std::sort(v.begin(), v.end()) sorts the vector itself, from the smallest value. Strings go in the order of their character codes. std::reverse turns the order around, and works on a string too.
  • A comparison function, such as bool stronger(const Monster& a, const Monster& b), returns true when a should come before b. You pass its name to std::sort without parentheses. It compares with < or >, never with <= or >=, and elements that it considers equal can end up in any order.
  • A lambda is a function without a name, written right where it's needed: [](const Monster& a, const Monster& b) { return a.attack > b.attack; }. The compiler works out its return type.
  • std::count(v.begin(), v.end(), value) counts the elements equal to a value, and std::count_if the elements for which a lambda with one parameter returns true.
  • A lambda sees only its parameters and its own variables. To use a variable from outside, capture it in the square brackets: [limit](int score) { return score > limit; }. Several variables are separated by commas.
  • std::find(v.begin(), v.end(), value) and std::find_if with a lambda return an iterator to the first element that fits, or end() when there's none. Check it != v.end() before you use *it.
  • An iterator works like a pointer: *it is the element, it->name is short for (*it).name, and it - v.begin() is the index. With auto it, you don't have to write its long type.
  • std::max_element and std::min_element return an iterator to the largest and the smallest element. Their lambda says whether a is smaller than b, so it compares with <. std::max(a, b) and std::min(a, b) pick one of two values.
  • std::any_of, std::all_of and std::none_of answer a yes-or-no question about the whole vector.

That covers all the topics of the course. In the next lesson, the Final Project, you will write one bigger program step by step, for a teacher's test results, and use everything you know from the course in it, structs, maps and the algorithms of this lesson included.

Instructions

  1. 1

    Let's practice. The hero fights the monsters from the weakest to the strongest. Sort monsters by health, lowest first, with a lambda, and print each monster on its own line as its name with its health in parentheses. The list starts with Bat (8) and ends with Dragon (200). Keep the monsters as they are: the exercise also runs your program with other values.

  2. 2

    With one hit, the hero defeats a monster whose health is at most heroAttack. Below the list, find with std::find_if the first monster in the sorted vector that the hero can't defeat with one hit, and print First real fight: Orc. When the hero can defeat every monster with one hit, print Every fight is easy instead. Keep the line with heroAttack as it is.

Back

13/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.