By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 882 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

STL for_each

P: n/a
Hi,
I am having some problems with for_each:
class Universe
{
public:
bool CheckIsValidImprovement( Improvement* );

bool ValidateTech();
list<TechImprovement> techImprovements
};

class Improvement
{

};

class TechImprovement : public Improvement
{

};

I have written a
bool Universe::ValidateTech()
{
for( it = techImprovements.begin(); it != techImprovements.end();it++)
if( ! CheckIsValidImprovement(&(*it)) ) return false;
return true;
}

but I'd like to write it as a for_each or a find_if for practice but am
struggling with the error msgs!

find_if( techImprovements.begin(),
techImprovements.end(),
ptr_fun( &CheckIsValidImprovement ) );
can't use it because its a member function.

how is this problem normally solved? do you need bind second to the Universe
using a this pointer? Have I got the wrong end of the stick??
Regards

Mike

Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Michael" <sl***********@hotmail.com> wrote in message
news:cu**********@hercules.btinternet.com...
Hi,
I am having some problems with for_each:
[snip]
but I'd like to write it as a for_each or a find_if for practice but am
struggling with the error msgs!

find_if( techImprovements.begin(),
techImprovements.end(),
ptr_fun( &CheckIsValidImprovement ) );
can't use it because its a member function.

how is this problem normally solved? do you need bind second to the Universe using a this pointer? Have I got the wrong end of the stick??


Look up 'mem_fun', 'mem_fun1', 'mem_fun_ref', 'mem_fun1_ref'

Book recommendation: www.josuttis.com/libbook

-Mike
Jul 23 '05 #2

P: n/a
On Tue, 8 Feb 2005 16:53:30 +0000 (UTC) in comp.lang.c++, "Michael"
<sl***********@hotmail.com> wrote,
find_if( techImprovements.begin(),
techImprovements.end(),
ptr_fun( &CheckIsValidImprovement ) );
can't use it because its a member function.

how is this problem normally solved? do you need bind second to the Universe
using a this pointer?


No, you need std:mem_fun to bind with an instance.
Stroustrup section 18.4.4.2

Or perhaps boost::mem_fn or boost::bind from http://boost.org

Jul 23 '05 #3

P: n/a
Michael wrote:
Hi,
I am having some problems with for_each:
class Universe
{
public:
bool CheckIsValidImprovement( Improvement* );

bool ValidateTech();
list<TechImprovement> techImprovements
};

class Improvement
{

};

class TechImprovement : public Improvement
{

};

I have written a
bool Universe::ValidateTech()
{
for( it = techImprovements.begin(); it != techImprovements.end();it++)
if( ! CheckIsValidImprovement(&(*it)) ) return false;
return true;
}

but I'd like to write it as a for_each or a find_if for practice but am
struggling with the error msgs!

find_if( techImprovements.begin(),
techImprovements.end(),
ptr_fun( &CheckIsValidImprovement ) );
can't use it because its a member function.


Then use mem_fun. This should work:

find_if(techImprovements.begin(), techImprovements.end(),
bind1st(mem_fun(&Universe::CheckIsValidImprovement ), this));

Jul 23 '05 #4

P: n/a

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:cu*************@news.t-online.com...
Michael wrote:
Hi,
I am having some problems with for_each:
class Universe
{
public:
bool CheckIsValidImprovement( Improvement* );

bool ValidateTech();
list<TechImprovement> techImprovements
};

class Improvement
{

};

class TechImprovement : public Improvement
{

};

I have written a
bool Universe::ValidateTech()
{
for( it = techImprovements.begin(); it != techImprovements.end();it++) if( ! CheckIsValidImprovement(&(*it)) ) return false;
return true;
}

but I'd like to write it as a for_each or a find_if for practice but am
struggling with the error msgs!

find_if( techImprovements.begin(),
techImprovements.end(),
ptr_fun( &CheckIsValidImprovement ) );
can't use it because its a member function.


Then use mem_fun. This should work:

find_if(techImprovements.begin(), techImprovements.end(),
bind1st(mem_fun(&Universe::CheckIsValidImprovement ), this));


Ah ha! however I now have a level of indirection problem, as the original
vector stores instances but the function needs a pointer. Is there a way of
doing the equivalen of '&'??

Regards

Michael
Jul 23 '05 #5

P: n/a
"Michael" <sl***********@hotmail.com> wrote in message

[snip]
{
public:
bool CheckIsValidImprovement( Improvement* );

bool ValidateTech();
list<TechImprovement> techImprovements
};
[snip]
for( it = techImprovements.begin(); it != techImprovements.end();it++) if( ! CheckIsValidImprovement(&(*it)) ) return false;
return true;

Why don't you rewrite

bool CheckIsValidImprovement( Improvement* )

as

bool CheckIsValidImprovement( Improvement & )

because you have a list of objects, not pointers, anyway, and use

mem_fun_ref

as someone told you in the first reply ? ;)

HTH
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.