(C++) Answer of exercise \#9: No for-loops \#29
January 9, 2018 · View on GitHub
(C++) Answer of exercise #9: No for-loops #29
This is the answer of Exercise #9: No for-loops.
Question #29: GetAllTrue on std::map<int,bool>
Replace the BOOST_FOREACH. You will need:
#include <cassert> #include <map> #include <boost/foreach.hpp> ///Returns true if all bools are true bool GetAllTrue(const std::map<int,bool>& v) { assert(!v.empty()); typedef std::pair<int,bool> Pair; BOOST_FOREACH(const Pair& p,v) { if (p.second == false) return false; } return true; }
Answer using Boost.Bind
#include <algorithm> #include <cassert> #include <map> #include <boost/lambda/bind.hpp> #include <boost/lambda/lambda.hpp> ///Returns true if all bools are true bool GetAllTrue(const std::map<int,bool>& v) { return std::find_if( v.begin(), v.end(), boost::lambda::bind(&std::pair<int,bool>::second, boost::lambda::_1) == false) == v.end(); }