|
Boost Users : |
Subject: Re: [Boost-users] [Bind] Is this how to write it?
From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2010-03-23 08:22:25
Just in case you interested how to accomplish it with boost::lambda
#include "boost/lambda/lambda.hpp"
#include <algorithm>
#include <vector>
#include <iterator>
#include <iostream>
using namespace std;
using namespace boost;
namespace bll=boost::lambda;
int main( )
{
vector< int > v;
int i=0;
// generate vector values
generate_n(back_inserter(v), 100, ++bll::var(i));
//create the output iterator
ostream_iterator<int> oiter(cout, ",");
copy(v.begin(), v.end(), oiter);
cout << endl;
// double each value in the vector
for_each(v.begin(), v.end(), bll::_1*=2);
copy(v.begin(), v.end(), oiter);
cout << endl;
return 0;
}
Regards,
Ovanes
On Tue, Mar 23, 2010 at 10:49 AM, Robert Jones <robertgbjones_at_[hidden]>wrote:
> #include <algorithm>
> #include <vector>
> #include "boost/bind.hpp"
>
> struct A
> {
> int double_it( int i ) { return i * 2; }
> } a;
>
> std::vector< int > v;
>
> int main( )
> {
> for ( std::vector< int >::iterator i = v.begin( ); i != v.end( ); ++ i
> )
> {
> a.double_it( * i );
> }
>
> // Write it as a for_each/bind
> std::for_each( v.begin(), v.end(), boost::bind( & A::double_it,
> boost::ref( a ), _1 ) );
> }
>
>
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