A
Alberto
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,
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,