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

Yes or No: std::any_of and std::all_of

Sometimes you don't need a number, only a yes or a no about the whole vector. Is any monster still alive? Are all of them defeated? With a loop, you would need a bool variable, an if and maybe break. The algorithms std::any_of, std::all_of and std::none_of answer such questions directly. Each of them takes a lambda that asks about one element, as std::count_if does, and returns true or false:

  • std::any_of: is the lambda true for at least one element?
  • std::all_of: is it true for every element?
  • std::none_of: is it true for no element at all?

The result is a bool, which you can store in a variable and use as a condition:

bool allDefeated = std::all_of(monsters.begin(), monsters.end(), [](const Monster& monster) {
    return monster.health == 0;
});
if (allDefeated) {
    std::cout << "Victory!" << std::endl;
}

These algorithms stop as soon as they know the answer, like a loop with break. std::any_of stops at the first element for which the lambda returns true, and std::all_of at the first one for which it returns false.

A question can often be asked in two ways. “Are all the monsters defeated?” means the same as “Is none of them alive?”, so std::none_of with monster.health > 0 gives the same answer as the example. Choose the one that reads most like your question.

One surprise: for an empty vector, std::all_of returns true, because it has no element for which the lambda would be false. std::any_of returns false, because it has no element at all.

Instructions

  1. 1

    When all the monsters have 0 health, the fight is won. Check it with std::all_of, and print Victory! when it's true, or The fight goes on when it isn't. Keep the monsters as they are: the exercise also runs your program with other values.

  2. 2

    The hero is in danger when a monster that is still alive could take all of the hero's health with one blow: its health is more than 0, and its attack is at least heroHealth. Check it with std::any_of and a lambda that captures heroHealth. Print Danger! when it's true, or Safe for now when it isn't. Keep the line with heroHealth as it is.

Back

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