STL for_each

M

Michael

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
 
M

Mike Wahler

Michael said:
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
 
D

David Harmon

On Tue, 8 Feb 2005 16:53:30 +0000 (UTC) in comp.lang.c++, "Michael"
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
 
R

Rolf Magnus

Michael said:
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));
 
M

Michael

Rolf Magnus said:
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
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top