473,654 Members | 3,253 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.beg in(),li.end(),b ind1st(do_calc, k));
}
};
At first, do_calc() was a member function, but

for_each(li.beg in(),li.end(),m em_fun(&A::do_c alc));

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 2132
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.beg in(),li.end(),b ind1st(do_calc, k));
}
};
At first, do_calc() was a member function, but
for_each(li.beg in(),li.end(),m em_fun(&A::do_c alc));

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.b egin(),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.b egin(),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
5285
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 void Attach( function<void (Subject*)> o ) { obs_.push_back(o); } --Line1
6
2211
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 problem is with the &. My second problem is with the const. Why should bind1st change the constness of the second argument of the function ?
11
2798
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 derived class circle, rectangle and triangle. i have also written this method for each shape object called 'draw_all_seq_inside2' which takes two values. i want to call this method using for_each statement in another method but it doesnt seem to...
8
1432
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 and call the passed in member function. I can do this in my own loop, but I am looking to call for_each. I am a little confused on the syntax to use. Any help would be appreciated. look at the following code and at the comment in
5
2685
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
2229
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
4028
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
9035
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 function objects. The same goes for most things in <algorithm>, though I don't know if I'm just not used to it or if it really is ugly. Specific question: I have a vector of objects and I'd like to send them all to std::cout. Is
3
2367
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
8379
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8294
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8494
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7309
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4297
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2719
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1924
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.