Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) Answer of exercise #9: No for-loops #10

 

This is the answer of Exercise #9: No for-loops.

 

 

 

 

 

Question #10: Widget::DoIt on boost::shared_ptr<Widget>

 

Replace the for-loop. You will need:

 

#include <vector>
#include <boost/shared_ptr.hpp>
 
struct Widget
{
  void DoIt() const { /* do it */ }
};
 
void DoIt(const std::vector<boost::shared_ptr<Widget> >& v)
{
  const std::size_t sz = v.size();
  for (std::size_t i=0; i!=sz; ++i)
  {
    v[i]->DoIt();
  }
}

 

 

 

 

 

Answer

 

#include <algorithm>
#include <numeric>
#include <vector>
#include <boost/shared_ptr.hpp>

struct Widget
{
  void DoIt() const { /* do it */ }
};

void DoIt(const std::vector<boost::shared_ptr<Widget> >& v)
{
  std::for_each(v.begin(),v.end(),boost::mem_fn(&Widget::DoIt));
}

 

 

 

 

 

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

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict