
Hi All Here's a quickie for you.... Given struct A { void a(); }; type std::vector<boost::shared_ptr<A> > VecType; VecType v; for (VecType::iterator i=v.begin(); i != v.end(); ++i ) (*i)->a(); How can I write that as a for_each loop? for_each( v.begin(), v.end(), boost::bind( ????? ) ); Thanks, - Rob. ps I can't use lambda because boost.bind is already used, and the placeholder name collisions just aren't worth the pain.

2009/7/17 Robert Jones <robertgbjones@gmail.com>
struct A { void a(); }; type std::vector<boost::shared_ptr<A> > VecType; VecType v;
for (VecType::iterator i=v.begin(); i != v.end(); ++i ) (*i)->a();
How can I write that as a for_each loop?
std::for_each(v.begin(), v.end(), boost::mem_fn(&A::a)); Roman Perepelitsa.

On Fri, Jul 17, 2009 at 1:47 PM, Roman Perepelitsa < roman.perepelitsa@gmail.com> wrote:
std::for_each(v.begin(), v.end(), boost::mem_fn(&A::a));
Thanks Roman - I've missed a trick there! Is that also equivalent to for_each( v.begin(), v.end(), boost::bind(&A::a, _1)); ? Thanks. -- ACCU - Professionalism in programming - http://www.accu.org

2009/7/17 Robert Jones <robertgbjones@gmail.com>
On Fri, Jul 17, 2009 at 1:47 PM, Roman Perepelitsa < roman.perepelitsa@gmail.com> wrote:
std::for_each(v.begin(), v.end(), boost::mem_fn(&A::a));
Thanks Roman - I've missed a trick there!
Is that also equivalent to
for_each( v.begin(), v.end(), boost::bind(&A::a, _1));
Yes. Note that with boost::lambda::bind you would have to automatically extract a pointer from shared_ptr, but boost::bind does it for you. Roman Perepelitsa.
participants (2)
-
Robert Jones
-
Roman Perepelitsa