About for_each usage

F

fgh.vbn.rty

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<int> getIds { return vi; }
A& getA(int idx) { return vA.at(idx); }
void bfunc();
private:
vector<A> vA;
vector<int> vi;
}

class C {
public:
void cfunc();
private:
B b;
}

void B::bfunc()
{
// populate vi
for(int i=0; i<vi.size(); ++i) {
cout << vA.printName() << endl;
}
}

void C::cfunc()
{
// populate vi
vector<int> vc = b.getIds();
for(int i=0; i<vc.size(); ++i) {
A& a = getA(vc);
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.
 
I

Ivan Novick

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<int> getIds { return vi; }
     A& getA(int idx) { return vA.at(idx); }
     void bfunc();
private:
     vector<A> vA;
     vector<int> vi;

}

class C {
public:
     void cfunc();
private:
     B b;

}

void B::bfunc()
{
   // populate vi
   for(int i=0; i<vi.size(); ++i) {
        cout << vA.printName() << endl;
   }

}

void C::cfunc()
{
   // populate vi
   vector<int> vc = b.getIds();
   for(int i=0; i<vc.size(); ++i) {
        A& a = getA(vc);
        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.


Hi,

Your code has too many syntax errors as is to give advice on how to
modify it. Can you first make it compile and then repost it.

Ivan Novick
http://www.mycppquiz.com
 
F

fgh.vbn.rty

Ivan said:
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<int> getIds { return vi; }
� � �A& getA(int idx) { return vA.at(idx); }
� � �void bfunc();
private:
� � �vector<A> vA;
� � �vector<int> vi;

}

class C {
public:
� � �void cfunc();
private:
� � �B b;

}

void B::bfunc()
{
� �// populate vi
� �for(int i=0; i<vi.size(); ++i) {
� � � � cout << vA.printName() << endl;
� �}

}

void C::cfunc()
{
� �// populate vi
� �vector<int> vc = b.getIds();
� �for(int i=0; i<vc.size(); ++i) {
� � � � A& a = getA(vc);
� � � � 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.


Hi,

Your code has too many syntax errors as is to give advice on how to
modify it. Can you first make it compile and then repost it.

Ivan Novick
http://www.mycppquiz.com


Sorry, I should've tried it out first. Anyway, the following compiles
without errors on gcc v3.0.4.

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class A {
public:
std::string getName() { return name; }
private:
std::string name;
};

class B {
public:
vector<int> getIds() { return vi; }
A& getA(int idx) { return vA.at(idx); }
void bfunc();
private:
vector<A> vA;
vector<int> vi;

};

class C {
public:
void cfunc();
private:
B b;
};

void B::bfunc()
{
// populate vA and vi in some fashion
for(int i=0; i<vi.size(); ++i) {
std::cout << vA.getName() << std::endl;
}
}

void C::cfunc()
{
// populate vA and vi in some fashion
vector<int> vc = b.getIds();
for(int i=0; i<vc.size(); ++i) {
A& a = b.getA(vc);
std::cout << a.getName() << std::endl;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top