std::find_if
is a generalization of std::find
when you need a function to check the elements you want, rather than a simple equality test. If you just want to do a simple equality test, then there is no need for a generalized form, and the lambda will just add complexity and verbosity. Just use std::find(begin, end, findValue)
instead:
std::vector<std::list<int>::const_iterator> matches; auto i = list.begin(), end = list.end(); while (i != end) { i = std::find(i, end, findValue); if (i != end) matches.push_back(i++); }
But instead of calling find
in a loop, I just write the loop manually:
std::vector<std::list<int>::const_iterator> matches; for (auto i = list.begin(), toofar = l.end(); i != toofar; ++i) if (*i == findValue) matches.push_back(i);
source share