BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Re: find_if error searching for struct with matching member

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Non-Technical)
View previous topic :: View next topic  
Author Message
Alisdair Meredith (TeamB)
Guest





PostPosted: Fri Oct 08, 2004 4:29 pm    Post subject: Re: find_if error searching for struct with matching member Reply with quote



Leo Siefert wrote:

Quote:
struct TMatch {
AnsiString Data;
TTreeNode* Node;
bool equalsNode( TTreeNode* );
};

This function works with pointers.

Quote:
class TForm1 : public TForm
{
__published:
private:
std::vector<TMatch> Matches;
std::vector<TMatch>::iterator TMatchIterator;
public:
__fastcall TForm1( TComponent* Owner );
};

The vector stores objects-by-value.
Functor should be passing objects by reference-to-const.

Quote:
__fastcall TForm1::TForm1( TComponent* Owner ) : TForm( Owner )
{
TTreeNode *node;
TMatchIterator it =
std::find_if (
Matches.begin(),
Matches.end(),
std::bind2nd(
std::mem_fun_ref( &TMatch::equalsNode ),
node
)
); // ERROR
}

I strongly recommend taking a look at the boost::bind library, soon to
be part of the standard library Technical Report. It is *much* simpler
than bind1st/2nd. mem_fun_ref and all that. Everything is handled by
the single bind template.

std::find_if (
Matches.begin(),
Matches.end(),
boost::bind( &TMatch::equalsNode, node, _1 )
)
);

AlisdairM(TeamB)

Back to top
Gillmer J. Derge [TeamB]
Guest





PostPosted: Fri Oct 08, 2004 4:35 pm    Post subject: Re: find_if error searching for struct with matching member Reply with quote



Leo Siefert wrote:
Quote:
I would appreciate any help, including a better basic approach. (Can I
do this with just function adapters and avoid functions dedicated to
searching for specific fields?)

I think find_if is definitely the right basic approach. This is exactly
what it's for. I think you could do it with just function adapters
(adapting a getter and composing it with equals and bind2nd) if you use
some of the boost classes but not with plain old standard library C++.

As for the error, I haven't actually compiled this, but the following
things strike me looking at it.

Quote:
bool equalsNode( TTreeNode* );

Should probably be:

bool equalsNode(const TTreeNode*) const;

Quote:
std::vector<TMatch>::iterator TMatchIterator;
[...]
TMatchIterator it =

Is TMatchIterator supposed to be a type or a member? You've declared it
as a member of the class but you're using it here like it's a type. I
suspect that's the cause of the error. The compiler is as confused as I
am. :-)

--
Gillmer J. Derge [TeamB]

Back to top
Gillmer J. Derge [TeamB]
Guest





PostPosted: Fri Oct 08, 2004 7:37 pm    Post subject: Re: find_if error searching for struct with matching member Reply with quote



Leo Siefert wrote:
Quote:
What I would really like to find is a way to avoid needing to have a
dedicated function for searching on each field of the data structure.

Assuming that a getter doesn't count as a dedicated function, I think
something like this would work (untested, and I've never really used
boost::bind, but this looks like more or less the right idea) ...

bind(std::equals<TTreeNode*>(), node,
bind(mem_fun_ref(&TMatch::getNode), _1))

I think this should turn into something equivalent to:

std::equals<T>(node, x.getNode());

--
Gillmer J. Derge [TeamB]

Back to top
Chris Uzdavinis (TeamB)
Guest





PostPosted: Fri Oct 08, 2004 7:48 pm    Post subject: Re: find_if error searching for struct with matching member Reply with quote

Leo Siefert <lIHATESPAMsiefert (AT) senate (DOT) michigan.gov> writes:

Quote:
What I would really like to find is a way to avoid needing to have a
dedicated function for searching on each field of the data structure.
In this case, if I want to search for a match to the Data member it
seems that I need to add an additional functor to allow that. Not a
very scalable approach and I would like to find something better.

It does not have to be a member function, though. If you
decide--later--to add another sort ordering or comparison function,
you can do that in your own "user" code withotu touching the
declaration of the objects in the container.

--
Chris (TeamB);

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Non-Technical) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.