Connecting Tech Pros Worldwide Help | Site Map

About for_each usage

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 2nd, 2008, 10:35 PM
fgh.vbn.rty@gmail.com
Guest
 
Posts: n/a
Default 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.

  #2  
Old July 2nd, 2008, 11:55 PM
Ivan Novick
Guest
 
Posts: n/a
Default Re: About for_each usage

On Jul 2, 3:27*pm, fgh.vbn....@gmail.com wrote:
Quote:
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
  #3  
Old July 3rd, 2008, 12:25 AM
fgh.vbn.rty@gmail.com
Guest
 
Posts: n/a
Default Re: About for_each usage



Ivan Novick wrote:
Quote:
On Jul 2, 3:27�pm, fgh.vbn....@gmail.com wrote:
Quote:
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;
}
}
  #4  
Old July 3rd, 2008, 01:55 AM
Daniel T.
Guest
 
Posts: n/a
Default Re: About for_each usage

fgh.vbn.rty@gmail.com wrote:
Quote:
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'
Quote:
private:
string name;
};
>
class B {
public:
vector<intgetIds { return vi; }
error: invalid member function declaration
Quote:
A& getA(int idx) { return vA.at(idx); }
void bfunc();
private:
vector<AvA;
vector<intvi;
}
error: missing semi-colon
Quote:
class C {
public:
void cfunc();
private:
B b;
}
error: missing semi-colon
Quote:
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'
Quote:
}
}
>
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
Quote:
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.
Quote:
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.
Quote:
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));
}

Quote:
3) How would I do this using the Boost.Lambda functions?
Differently.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.