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: std::find

In the Loops lesson, you searched with a loop: a variable position started at -1, and when the loop found the element, it stored its index and ended with break. The algorithm std::find searches for you. It looks for the first element that is equal to a value:

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>

int main() {
    std::vector<std::string> backpack = {"rope", "torch", "potion", "map"};
    auto it = std::find(backpack.begin(), backpack.end(), "potion");

    if (it == backpack.end()) {
        std::cout << "No potion" << std::endl;
    } else {
        std::cout << "Found: " << *it << std::endl;
        std::cout << "Index: " << it - backpack.begin() << std::endl;
    }
    return 0;
}

This prints:

Found: potion
Index: 2

std::find doesn't return the element or its index, but an iterator: the position of the element in the vector. You met iterators at the start of the lesson, as begin() and end(). Here's what you can do with the one that std::find returns:

  • Check whether something was found. When the value isn't in the vector, std::find returns end(), the position after the last element. That's how algorithms say “not found”, like the -1 in your loop.
  • Read the element with *it. An iterator works much like a pointer: *it is the element at its position, just as *p is the variable that a pointer points to. And you can change the element through it: *it = "bottle"; changes the element in the vector.
  • Get the index with it - backpack.begin(): how many positions the iterator is from the beginning.

Use *it only after you have checked that it isn't end(). There's no element at end(), and reading it is the same kind of mistake as reading through nullptr.

The type of this iterator is long, std::vector<std::string>::iterator. You could write std::vector<std::string>::iterator it = …, but with auto, which you know from the lesson on maps, the compiler works out the type itself. In error messages, you will see iterators under the library's own names. For example, std::cout << it without the asterisk doesn't compile, because an iterator can't be printed: error: invalid operands to binary expression ('ostream' (aka 'basic_ostream<char>') and 'std::__wrap_iter<std::string *>').

Instructions

  1. 1

    In place of the comment, look for "potion" in backpack with std::find. When it's there, print its index, worked out from the iterator, as Potion at index 2. When it isn't, print No potion. Keep the line with the backpack as it is: the exercise also runs your program with other items.

  2. 2

    The hero drinks the potion, and only the bottle is left. In the branch where the potion was found, below the line that prints its index, change the element to "bottle" through the iterator. The program then prints Backpack: rope torch bottle map key.

Back

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