Skip to content

Latest commit

 

History

History
75 lines (41 loc) · 2.81 KB

CppExerciseNoForLoopsAnswer29.md

File metadata and controls

75 lines (41 loc) · 2.81 KB

 

 

 

 

 

 

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; }

 

 

 

 

 

Boost 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(); }