(C++) std::mem\_fun
January 11, 2018 · View on GitHub
(C++) std::mem_fun
An adapter to be able to use for_each on a member function of T stored in a container as T* (compare std::mem_fun_ref, to use for_each on a member function of T stored in a container as T ).
Use a container of boost::shared_ptr<T> instead.
Replacing a for loop by algorithms using std::mem_fun
#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(); } }
#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)); }