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: Searching

The key is in the backpack, so this program should print You open the door. But it prints The door is locked. Click the line with the mistake.

1#include <iostream>
2#include <algorithm>
3#include <string>
4#include <vector>
5
6int main() {
7    std::vector<std::string> backpack = {"key", "rope", "torch"};
8    auto it = std::find(backpack.begin(), backpack.end(), "key");
9    if (it != backpack.begin()) {
10        std::cout << "You open the door" << std::endl;
11    } else {
12        std::cout << "The door is locked" << std::endl;
13    }
14    return 0;
15}
Back

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