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

Counting: std::count_if

In the Loops lesson, you counted with a counter, a loop and an if:

std::vector<int> scores = {640, 1250, 380, 980, 720};
int over700 = 0;
for (int score : scores) {
    if (score > 700) {
        over700++;
    }
}

The algorithm std::count_if does all of this in one line:

int over700 = std::count_if(scores.begin(), scores.end(), [](int score) { return score > 700; });

Both versions give 3: the scores 1250, 980 and 720. Look at what's left of the loop. The condition of the if, score > 700, is now the body of a lambda. std::count_if calls the lambda for each element of the range and counts the elements for which it returns true. The counter, the loop and the ++ are its job.

This lambda has one parameter, because it answers a question about one element: “Is this score over 700?” The lambda for std::sort has two, because it compares two elements. With the wrong number of parameters, the program doesn't compile.

With structs, the lambda gets the whole struct and can look at any of its members. This counts the monsters with an attack of at least 5:

int strong = std::count_if(monsters.begin(), monsters.end(), [](const Monster& monster) {
    return monster.attack >= 5;
});

For the five monsters of the previous exercises, strong is 3: the Goblin, the Orc and the Troll.

When you only want to count the elements that are equal to a value, you don't need a lambda. std::count takes the value itself:

std::vector<std::string> loot = {"gold", "potion", "gold", "bone", "gold"};
int gold = std::count(loot.begin(), loot.end(), "gold");

Here, gold is 3.

Instructions

  1. 1

    The vector loot holds what the hero found after a fight. Count with std::count how many times "potion" is in it, and print the result as Potions: 2. Keep the line with the loot as it is: the exercise also runs your program with other loot.

  2. 2

    After the fight, some of the monsters have 0 health left. Count with std::count_if and a lambda the monsters that are still alive, the ones with more than 0 health, and print the result as Still alive: 2. Keep the monsters as they are.

Back

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