Search code examples
c++vectorfindstd

Searching for multiple elements using std::find()


Is it possible to use std::find() to search for multiple elements with 1 call? Example: std::find(vector.begin(), vector.end(), 0&&1&&D)

Thanks


Solution

  • You can't use std::find. Assuming 0&&1&&D is meant to be a list of three values and you're trying to find an element in the vector with any of those values, you can use:

    • std::find_if with a predicate (a lambda may be easiest e.g. [](const T& x) { return x == 0 || x == 1 || x == d; }, where T is whatever type your vector holds), or

    • std::find_first_of (the linked page has a good example).