Problems with for_each and bind1st

Discussion in 'C++' started by Alberto, Feb 7, 2005.

  1. Alberto

    Alberto Guest

    Hello,

    while writing a program I ran across the problem of using for_each.
    Although I can traverse lists with a for loop, I'd prefer to use STL's
    for_each. Here's my faulty code:

    #include <iostream>
    #include <list>
    #include <algorithm>

    using namespace std;

    void do_calc(int &k,int ii){
    k+=ii;
    }

    class A{
    list <int> li;
    int k;

    public:


    A():k(0){
    li.push_back(8);
    li.push_back(3);

    for_each(li.begin(),li.end(),bind1st(do_calc,k));
    }
    };


    At first, do_calc() was a member function, but

    for_each(li.begin(),li.end(),mem_fun(&A::do_calc));

    didn't work either. Then I made do_calc() a static member function,
    but again I had no luck.

    What would be the best approach?

    Many thanks in advance,
     
    Alberto, Feb 7, 2005
    #1
    1. Advertising

  2. On 2005-02-07 10:24:39 -0500, (Alberto) said:

    > Hello,
    >
    > while writing a program I ran across the problem of using for_each.
    > Although I can traverse lists with a for loop, I'd prefer to use STL's
    > for_each. Here's my faulty code:
    >
    > #include <iostream>
    > #include <list>
    > #include <algorithm>
    >
    > using namespace std;
    >
    > void do_calc(int &k,int ii){
    > k+=ii;
    > }
    >
    > class A{
    > list <int> li;
    > int k;
    >
    > public:
    >
    >
    > A():k(0){
    > li.push_back(8);
    > li.push_back(3);
    >
    > for_each(li.begin(),li.end(),bind1st(do_calc,k));
    > }
    > };
    >
    >
    > At first, do_calc() was a member function, but
    > for_each(li.begin(),li.end(),mem_fun(&A::do_calc));
    >
    > didn't work either. Then I made do_calc() a static member function,
    > but again I had no luck.
    >
    > What would be the best approach?
    >
    > Many thanks in advance,


    A couple of issues:
    1) bind1st() takes an *Adaptable* function. You can turn a regular
    function into an adaptable one by passing it through ptr_fun().
    2) bind1st isn't appropriate here, as it passes a *copy* of the bound
    parameter, and references cannot be copied.
    3) Why don't you just use accumulate()?

    Try this:
    //----- begin code ---------
    #include <list>
    #include <numeric>

    using namespace std;

    int do_calc(int k,int ii){
    return k+ii;
    }

    class A{
    list <int> li;
    int k;

    public:


    A():k(0){
    li.push_back(8);
    li.push_back(3);

    k = accumulate(li.begin(),li.end(), 0, do_calc);
    }
    };
    //----- end code ---------

    Or, if your do_calc function really just adds the numbers, you can omit
    it altogether:
    //----- begin code ---------
    #include <list>
    #include <numeric>

    using namespace std;

    class A{
    list <int> li;
    int k;

    public:


    A():k(0){
    li.push_back(8);
    li.push_back(3);

    k = accumulate(li.begin(),li.end(), 0);
    }
    };
    //----- end code ---------


    --
    Clark S. Cox, III
     
    Clark S. Cox III, Feb 7, 2005
    #2
    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. wenmang
    Replies:
    1
    Views:
    518
    Victor Bazarov
    Aug 8, 2003
  2. Fred Ma
    Replies:
    2
    Views:
    2,099
    Fred Ma
    Feb 5, 2004
  3. Denis Remezov

    binary_function and bind1st

    Denis Remezov, May 19, 2004, in forum: C++
    Replies:
    6
    Views:
    773
    P.J. Plauger
    May 21, 2004
  4. Marc
    Replies:
    6
    Views:
    700
    Denis Remezov
    Jul 6, 2004
  5. John Black
    Replies:
    3
    Views:
    497
    John Harrison
    Aug 8, 2004
Loading...

Share This Page