How to do it...

The following is a list of algorithms that can be used for finding elements in a range:

  • Use std::find() to find a value in a range; this algorithm returns an iterator to the first element equal to the value:
        std::vector<int> v{ 1, 1, 2, 3, 5, 8, 13 };        auto it = std::find(v.cbegin(), v.cend(), 3);        if (it != v.cend()) std::cout << *it << std::endl;
  • Use std::find_if() to find a value in a range that meets a criterion from a unary predicate; this algorithm returns an iterator to the first element for which the predicate returns true:
        std::vector<int> v{ 1, 1, 2, 3, 5, 8, 13 };        auto it = std::find_if(v.cbegin(), v.cend(),                                [](int const n) {return n > 10; });        if (it != v.cend()) std::cout << *it << std::endl;
  • Use std::find_if_not() ...

Get Modern C++: Efficient and Scalable Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.