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

Searching with a Lambda: std::find_if

To find a monster by its name, std::find isn't enough. It compares whole elements, and a monster isn't the same as the string "Troll": std::find(monsters.begin(), monsters.end(), "Troll") ends with the error invalid operands to binary expression ('Monster' and 'const char[6]'). For such searches, there's std::find_if. Like std::count_if, it takes a lambda. It returns an iterator to the first element for which the lambda returns true, or end() when there's no such element:

std::string wanted = "Troll";
auto it = std::find_if(monsters.begin(), monsters.end(), [wanted](const Monster& monster) {
    return monster.name == wanted;
});

if (it != monsters.end()) {
    std::cout << (*it).name << " has " << (*it).health << " health" << std::endl;
}

With the monsters in the editor, this prints Troll has 80 health. *it is the monster, and (*it).name is its name. The parentheses are needed, because the dot goes first: *it.name would look for a member name in the iterator itself. You need this so often that C++ has a shorter way to write it, the arrow ->. it->name means exactly the same as (*it).name:

if (it != monsters.end()) {
    std::cout << it->name << " has " << it->health << " health" << std::endl;
}

The arrow works the same way with pointers: after Monster* p = &monsters[0];, p->name is the name of the first monster. When you write a dot instead of the arrow, the compiler tells you what to do: error: no member named 'name' in 'std::__wrap_iter<Monster *>'; did you mean to use '->' instead of '.'?

Remember that std::find_if returns the first element that fits and doesn't look any further. If you look for a monster with more than 40 health, you get the Orc (50), even though the Troll comes later with 80.

Instructions

  1. 1

    Find the monster whose name is in wanted, with std::find_if and a lambda that captures wanted. When it's there, print its name, health and attack as Troll: health 80, attack 7. When it isn't, print Not found: and the name from wanted. Keep the line with wanted as it is: the exercise also runs your program with other names.

  2. 2

    Below that, find the first monster in the vector with an attack of at least 8, and print its name as First strong one: Orc. Keep the monsters as they are.

Back

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