3 ms·
In C++, the way you would do this involves boost::men_fn (http://www.boost.org/doc/libs/1_39_0/libs/bind/mem_fn.html http://www.boost.org/doc/libs/1_39_0/libs/b
by scott_s 17y ago
In C++, the way you would do this involves boost::men_fn (http://www.boost.org/doc/libs/1_39_0/libs/bind/mem_fn.html http://www.boost.org/doc/libs/1_39_0/libs/bind/mem_fn.html):
list<Person> beerDrinkers = filter(persons, boost::mem_fn(&Person::legalAge));
Where filter looks something like:
template <class Sequence, class Pred>
Sequence filter(const Sequence& in, Pred p)
{
Sequence out;
for (typename Sequence::const_iterator i = in.begin(); i != in.end(); ++i) {
if (p(*i)) {
out.insert(out.end(), *i);
}
}
return out;
}
We can even use this style if legalAge is something that takes a parameter, like ageCheck(age) using boost::bind (http://www.boost.org/doc/libs/1_39_0/libs/bind/bind.html http://www.boost.org/doc/libs/1_39_0/libs/bind/bind.html):
list<Person> beerDrinkers = filter(persons, boost::bind(&Person::ageCheck, 21, _1));