This is the answer of Exercise #9: No for-loops.
Replace the for-loop. You will need:
#include <vector> struct Widget { void DoIt() const { /* do it */ } }; void DoIt(const std::vector<Widget*>& v) { const int sz = static_cast<int>(v.size()); for (int i=0; i!=sz; ++i) { v[i]->DoIt(); } }
Answer STL-only
#include <algorithm> #include <numeric> #include <vector> struct Widget { void DoIt() const { /* do it */ } }; void DoIt(const std::vector<Widget*>& v) { std::for_each(v.begin(),v.end(),std::mem_fun(&Widget::DoIt)); }
Answer using Boost
#include <algorithm> #include <boost/mem_fn.hpp> #include <vector> struct Widget { void DoIt() const { /* do it */ } }; void DoIt(const std::vector<Widget*>& v) { std::for_each(v.begin(),v.end(),boost::mem_fn(&Widget::DoIt)); }