Boost logo

Boost Users :

Subject: Re: [Boost-users] [Bind] Finding the right syntax.
From: gongyiling (gongyiling2010_at_[hidden])
Date: 2011-10-17 22:26:14


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_at_[hidden]> 写入消息
> news:CAG3ZVmoX2g=G9sGBYef71cHR5bW->7CH6LBewLVE99SxFwtUWrw_at_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_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net