std::for_each question

Discussion in 'C++' started by Dylan, Jul 4, 2004.

  1. Dylan

    Dylan Guest

    At the moment I often find myself doing something like the following

    std::list<MyClass*> mcl;

    /.../fill mcl with some elements

    //now call aMethod() for each element;
    for (std::list<MyClass*>::iterator it = mcl.begin();
    it != mcl.end();
    ++it)
    {
    it->aMethod();
    }

    Is there a *convenient* way of using something like std::for_each to
    achieve the same result as the loop shown in the previous code
    snippet?

    thanks
     
    Dylan, Jul 4, 2004
    #1
    1. Advertising

  2. Dylan

    jota Guest

    "Dylan" <> skrev i meddelandet
    news:...
    > At the moment I often find myself doing something like the following
    >
    > std::list<MyClass*> mcl;


    std::for_each(mcl.begin(),mcl.end(),aMethod);
    //jota
     
    jota, Jul 4, 2004
    #2
    1. Advertising

  3. Dylan

    Buster Guest

    Dylan wrote:
    > At the moment I often find myself doing something like the following
    >
    > std::list<MyClass*> mcl;
    >
    > /.../fill mcl with some elements
    >
    > //now call aMethod() for each element;
    > for (std::list<MyClass*>::iterator it = mcl.begin();
    > it != mcl.end();
    > ++it)
    > {
    > it->aMethod();
    > }
    >
    > Is there a *convenient* way of using something like std::for_each to
    > achieve the same result as the loop shown in the previous code
    > snippet?


    std::for_each (mcl.begin (), mcl.end (),
    std::mem_fun (& MyClass::aMethod));

    --
    Regards,
    Buster.
     
    Buster, Jul 4, 2004
    #3
  4. Dylan

    Pete Becker Guest

    Dylan wrote:
    >
    > Is there a *convenient* way of using something like std::for_each to
    > achieve the same result as the loop shown in the previous code
    > snippet?


    Read about std::mem_fun.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
     
    Pete Becker, Jul 4, 2004
    #4
  5. Dylan

    Pete Becker Guest

    jota wrote:
    >
    > "Dylan" <> skrev i meddelandet
    > news:...
    > > At the moment I often find myself doing something like the following
    > >
    > > std::list<MyClass*> mcl;

    >
    > std::for_each(mcl.begin(),mcl.end(),aMethod);


    Not quite. This would attempt to call aMethod(*iter), which is fine if
    aMethod is an ordinary function, but what you need with a member
    function is (*iter)->aMethod. Use

    std::for_each(mc1.begin(), mc1.end(), mem_fun(&MyClass::aMethod));

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
     
    Pete Becker, Jul 4, 2004
    #5
  6. On Sun, 04 Jul 2004 20:41:05 +0100, Dylan wrote:

    > At the moment I often find myself doing something like the following
    >
    > std::list<MyClass*> mcl;
    >
    > /.../fill mcl with some elements
    >
    > //now call aMethod() for each element;
    > for (std::list<MyClass*>::iterator it = mcl.begin();
    > it != mcl.end();
    > ++it)
    > {
    > it->aMethod();
    > }
    >
    > Is there a *convenient* way of using something like std::for_each to
    > achieve the same result as the loop shown in the previous code
    > snippet?


    I think you could use something like:

    std::for_each(mcl.begin(), mcl.end(), std::mem_fun(&MyClass::aMethod))

    If you want it to be even more powerful, try Boost. For instance, look at
    this sample:
    http://www.boost.org/libs/bind/bind.html#with_algorithms

    Regards,

    Josep
     
    =?iso-8859-1?q?Josep_Mon=E9s_i_Teixidor?=, Jul 5, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Philip Potter

    std::for_each

    Philip Potter, Aug 29, 2006, in forum: C++
    Replies:
    6
    Views:
    683
    Shooting
    Aug 29, 2006
  2. Replies:
    2
    Views:
    319
    David Harmon
    Mar 14, 2007
  3. yurec

    std::for_each + break

    yurec, Nov 19, 2007, in forum: C++
    Replies:
    38
    Views:
    1,742
    Kai-Uwe Bux
    Nov 26, 2007
  4. Javier
    Replies:
    0
    Views:
    397
    Javier
    Apr 18, 2008
  5. Brice Gagnage
    Replies:
    6
    Views:
    1,567
    Brice Gagnage
    Apr 5, 2012
Loading...

Share This Page