473,327 Members | 2,090 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,327 software developers and data experts.

About for_each usage

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.
Jul 2 '08 #1
3 1499
On Jul 2, 3:27*pm, fgh.vbn....@gmail.com wrote:
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.
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
Jul 2 '08 #2


Ivan Novick wrote:
On Jul 2, 3:27�pm, fgh.vbn....@gmail.com wrote:
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.

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<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 vA and vi in some fashion
for(int i=0; i<vi.size(); ++i) {
std::cout << vA[i].getName() << std::endl;
}
}

void C::cfunc()
{
// populate vA and vi in some fashion
vector<intvc = b.getIds();
for(int i=0; i<vc.size(); ++i) {
A& a = b.getA(vc[i]);
std::cout << a.getName() << std::endl;
}
}
Jul 3 '08 #3
fg*********@gmail.com wrote:
I'm having problems setting up the functors to use for_each. Here's an
example:

class A {
public:
void getName() { return name; }
error: return-statement with a value, in function returning 'void'
private:
string name;
};

class B {
public:
vector<intgetIds { return vi; }
error: invalid member function declaration
A& getA(int idx) { return vA.at(idx); }
void bfunc();
private:
vector<AvA;
vector<intvi;
}
error: missing semi-colon
class C {
public:
void cfunc();
private:
B b;
}
error: missing semi-colon
void B::bfunc()
{
// populate vi
for(int i=0; i<vi.size(); ++i) {
cout << vA[i].printName() << endl;
error: 'class A' has no member named 'printName'
}
}

void C::cfunc()
{
// populate vi
vector<intvc = b.getIds();
for(int i=0; i<vc.size(); ++i) {
A& a = getA(vc[i]);
error: 'getA' was not declared in this scope
cout << a.getName() << endl;
}
}

Here vA is just a vector of As and vi is a vector of integers that
index into vA.
Frankly, there a so many errors, that I don't think the loops do what
you want them to do, especially bfunc.
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?
Don't use for_each, use transform instead:

transform(vA.begin(), vA.begin() + vi.size(),
ostream_iterator<string>(cout, "\n"), mem_fun_ref(&A::getName));

But again, I don't think that's what you want.
2) How do I replace the for loop in cfunc with a for_each?
That function really should be in class B. It pulls B's vi variable out,
and then iterates over B's vA variable. So let's put it there instead:

string B::getNameOf(int x)
{
return vA.at(x).getName();
}

void B::cfunc()
{
transform(vi.begin(), vi.end(), ostream_iterator<string>(cout, "\n"),
bind1st(mem_fun(&B::getNameOf), this));
}

3) How would I do this using the Boost.Lambda functions?
Differently.
Jul 3 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Alex Vinokur | last post by:
Functor-parameters in the for_each() and transform() algorithms are passed by value. Might it make sense to have also algorithms for_each2() and transform2() which pass Functor-parameters by...
3
by: Griff | last post by:
#include <iostream> using namespace std; #include <vector> #include <string> #include <fstream> #include <algorithm> template<class C>void PrintAll(C&v) {
2
by: flyaflya | last post by:
i use a class static member function to for_each: class t1 { static test(string s); } main() { list<string> slist; ...
1
by: Capstar | last post by:
Hi NG, I have a question on std::for_each. I try to use this in combination with std::bind2nd to call a method of all functions in a container (std::set or std::map) and pass that method the...
12
by: sashan | last post by:
Sometimes I write code using std::for_each. Sometimes I use it on several container classes that are the same size. for_each(v.begin(), v.end(), f1()); for_each(u.begin(), u.end(), f2()); ...
3
by: PolkaHead | last post by:
I was wondering if there's a way to traverse a two-dimensional vector (vector of vectors) with a nested for_each call. The code included traverses the "outer" vector with a for_each, than it...
3
by: Yan | last post by:
Hi, I don't seem to be able to use for_each if it should replace a 'for' loop in a method (constructor in my case) and inside that 'for' loop a class member variable is being accessed. The...
4
by: Kenneth Porter | last post by:
I'm trying to create a class that represents a bucket of locks, and I want to lock a set of objects specified by a container of pointers. I can't seem to find the right combination of function...
9
by: nguillot | last post by:
Hello I used to loop on a std::map<k, dto act on the data (d) like that (d being a class with setIntMember method): typedef std::map<k, dtMap; struct setIntMember { setIntMember(int j) :...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.