Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching for multiple elements using std::find()

Tags:

c++

find

std

vector

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

like image 756
Noobgineer Avatar asked Oct 24 '25 17:10

Noobgineer


1 Answers

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).

like image 111
Tony Delroy Avatar answered Oct 26 '25 07:10

Tony Delroy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!