Skip to content

Latest commit

 

History

History
76 lines (42 loc) · 2.33 KB

CppExerciseNoForLoopsAnswer10.md

File metadata and controls

76 lines (42 loc) · 2.33 KB

 

 

 

 

 

 

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