468,765 Members | 1,248 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

Problems with for_each and bind1st

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,
Jul 23 '05 #1
2 1916
On 2005-02-07 10:24:39 -0500, az****@yahoo.es (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
cl*******@gmail.com

Jul 23 '05 #2
Clark S. Cox III wrote:
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().

So only functors are allowed, right?

2) bind1st isn't appropriate here, as it passes a *copy* of the bound
parameter, and references cannot be copied.

Is it possible to do so? Is there any variation of bind1st that takes
references, pointers, etc?

3) Why don't you just use accumulate()?

Well, because I didn't remember that algorithm ;) Actually, I wrote this
code for demonstration purposes only. In my real code, I'm doing other
things with the data stored in the list.

[Snipped examples using accumulated]

By the way, these examples are good tips to me. I appreciate them, thanks.
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by wenmang | last post: by
11 posts views Thread by franklini | last post: by
5 posts views Thread by glen stark | last post: by
1 post views Thread by ibe | last post: by
6 posts views Thread by Philip Potter | last post: by
3 posts views Thread by Chris Roth | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.