I'm having problems setting up the functors to use for_each. Here's an
example:
class A {
public:
void getName() { return name; }
private:
string name;
};
class B {
public:
vector<intgetIds { return vi; }
A& getA(int idx) { return vA.at(idx); }
void bfunc();
private:
vector<AvA;
vector<intvi;
}
class C {
public:
void cfunc();
private:
B b;
}
void B::bfunc()
{
// populate vi
for(int i=0; i<vi.size(); ++i) {
cout << vA[i].printName() << endl;
}
}
void C::cfunc()
{
// populate vi
vector<intvc = b.getIds();
for(int i=0; i<vc.size(); ++i) {
A& a = getA(vc[i]);
cout << a.getName() << endl;
}
}
Here vA is just a vector of As and vi is a vector of integers that
index into vA. Let's suppose that vA and vi are populated
appropriately. All I'm trying to do is to execute a particular
function on a subset of the As.
I have three questions:
1) How do I replace the for loop in bfunc with a for_each?
2) How do I replace the for loop in cfunc with a for_each?
3) How would I do this using the Boost.Lambda functions?
Thanks.