Hi All In the code below my objective is pass a nullary function object to apply, and for that nullary function object to be 'my_function()' called with each member of the vector. How can I recode the call to apply as a unary bind expression such that I can write the for loop as a for_each loop? Thx, - Rob. #include <vector> #include "boost/function.hpp" #include "boost/bind.hpp" void apply( boost::function<void()> f ) { f( ); } int main( ) { static boost::function<void(int)> my_function = 0; std::vector<int> v; for ( std::vector<int>::iterator i = v.begin( ); i != v.end( ); ++ i ) { apply( bind( my_function, * i ) ); } }
here is one of the example, maybe not the best. #include <vector> #include "boost/function.hpp" #include "boost/bind.hpp" void apply( boost::function<void(int)> f, int i) { f(i); } int main( ) { static boost::function<void(int)> my_function = 0; std::vector<int> v; std::for_each(v.begin(), v.end(), boost::bind(apply, my_function, _1)); }
"Robert Jones" <robertgbjones@gmail.com> 写入消息 news:CAG3ZVmoX2g=G9sGBYef71cHR5bW->7CH6LBewLVE99SxFwtUWrw@mail.gmail.com... Hi All
In the code below my objective is pass a nullary function object to apply, and for that nullary function object to be 'my_function()' called with each member of the vector. How can I recode the call to apply as a unary bind expression such that I can write the for loop as a for_each loop?
Thx,
- Rob.
#include <vector> #include "boost/function.hpp" #include "boost/bind.hpp"
void apply( boost::function<void()> f ) { f( ); }
int main( ) { static boost::function<void(int)> my_function = 0;
std::vector<int> v;
for ( std::vector<int>::iterator i = v.begin( ); i != v.end( ); ++ i ) { apply( bind( my_function, * i ) ); } }
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
gongyiling
-
Robert Jones