473,222 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,222 software developers and data experts.

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 2110
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: wenmang | last post by:
Hi, all: I am reading Herb Sutter's article: http://www.cuj.com/documents/s=8840/cujexp0309sutter/ I have trouble to understand for following lines: class Subject { // ... public: virtual...
6
by: Marc | last post by:
T x; T foo(T, T); bind1st(ptr_fun(foo), x) creates a function object that takes an argument of type T const&. This does not work if T is already a reference type like int const&. So my first...
11
by: franklini | last post by:
hello people, just wanted to say thanks again for the help in the past. i have a new problem which am wondering if any body can help me with. i have written this abtract class shape and its...
8
by: pookiebearbottom | last post by:
Ok, this is what I am trying to do. I have a member function that as a parameter accepts a pointer to a different member function. In this 2nd function, I want to iterate over an STL container...
5
by: glen stark | last post by:
Hi Everyone So, I want to use a std::for_each in the following situation: class B{ //whatever } class A{ private:
1
by: ibe | last post by:
Hi, i can't get the clue: I hav a loop in my code like this: ---snip--- class Foo { accept(Visitor& v) { v.visit(*this); }
6
by: Belebele | last post by:
Suppose I want to use some object's member function as the action passed to a for_each call: class A { ... public: void foo(int ); }; A a;
6
by: Philip Potter | last post by:
Hello there, I'm reading about the std::for_each() function in TC++PL, 3rd Ed. It seems like a good idea, but in practice I can never see a way to bend it to my wishes without writing huge...
3
by: Chris Roth | last post by:
I have a vector (v) containing objects of class C. class C { private: double d; public: void foo( B& b ); };
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.