Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

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

 

 

 

 

 

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

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict