473,396 Members | 1,827 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,396 software developers and data experts.

STL for_each

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
5 3878
"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
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
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

"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
"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 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) {
6
by: Michal Wyrebski | last post by:
Hello, I'm new in this group and I don't know if my questions are too silly, but I'm intermediate programmer and don't have enought experience. Please be charitable. I don't know how to write...
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()); ...
9
by: shaun | last post by:
I am working on code where I am handed a vector of pointers vector<T*> or a map<std::string, T*>, and I have to delete the objects and set the pointers to zero. I have been using a 'for' loop and...
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...
9
by: Chris Roth | last post by:
I have a vector of vectors: vector< vector<double v; and have initialized it with: v( 5 ); as I know I will have 5 columns of data. At this point, I read text file data into each of the the...
13
by: Sarath | last post by:
What's the advantage of using for_each than the normal iteration using for loop? Is there any 'tweak' to use stream objects as 'Function' parameter of for_each loop. Seems copy function can do...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.