Right now, I have this code:
bool isAnyTrue() {
for(std::list< boost::shared_ptr<Foo> >::iterator i = mylist.begin(); i != mylist.end(); ++i) {
if( (*i)->isTrue() )
return true;
}
return false;
}
I have used Boost here and then but I couldn’t really remember any simple way to write it somewhat like I would maybe write it in Python, e.g.:
def isAnyTrue():
return any(o.isTrue() for o in mylist)
Is there any construct in STL/Boost to write it more or less like this?
Or maybe an equivalent to this Python Code:
def isAnyTrue():
return any(map(mylist, lambda o: o.isTrue()))
Mostly I am wondering if there is any existing any (and all) equivalent in Boost / STL yet. Or why there is not (because it seems quite useful and I use it quite often in Python).
C++ does not (yet) have a
foreachconstruct. You have to write that yourself/That said, you can use the
std::find_ifalgorithm here:Also, you should probably be using
std::vectororstd::dequerather thanstd::list.EDIT: sth has just informed me that this won’t actually compile because your list contains
shared_ptrinstead of the actual objects… because of that, you’re going to need to write your own functor, or rely on boost:Note, I haven’t tested this second solution.