Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > in-place functor comparison instead of std::binary_function

Reply
Thread Tools

in-place functor comparison instead of std::binary_function

 
 
news.online.no
Guest
Posts: n/a
 
      01-30-2006
Hi all

Rather than writing std::binary:functions I thought it would be nice to
create a comparison expression directly into the algorithm expression.
Typical use is

struct Person
{
std::string name () const;
};

// Find "John"
std::vector<Person>::iterator it = std::find_if (persons.begin(),
persons.end(), somehow bind Person::name method == "John");

While std::equal could be used in some simple cases, it does not accept
mixed input like &Person::name and "John" (at least to my knowledge)

Is there a way to accomplish this type of comparison using a mix of
binding of functions /return values and plain values ?

Thanks,

Anders


 
Reply With Quote
 
 
 
 
peter steiner
Guest
Posts: n/a
 
      01-30-2006
news.online.no wrote:
> Hi all
>
> Rather than writing std::binary:functions I thought it would be nice to
> create a comparison expression directly into the algorithm expression.
> Typical use is
>
> struct Person
> {
> std::string name () const;
> };
>
> // Find "John"
> std::vector<Person>::iterator it = std::find_if (persons.begin(),
> persons.end(), somehow bind Person::name method == "John");
>
> While std::equal could be used in some simple cases, it does not accept
> mixed input like &Person::name and "John" (at least to my knowledge)
>
> Is there a way to accomplish this type of comparison using a mix of
> binding of functions /return values and plain values ?


boost::lambda provides a solution to your problem.

std::vector<Person>::iterator it = std::find_if (persons.begin(),
persons.end(), bind(&Person::name, _1) == "John");

it is perfectly suited for simple ad-hoc functor declarations in
std::algorithms and the like.

have a look at http://boost.org/doc/html/lambda.

-- peter

 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      01-30-2006
In article <(E-Mail Removed)>,
"news.online.no" <(E-Mail Removed)> wrote:

> Hi all
>
> Rather than writing std::binary:functions I thought it would be nice to
> create a comparison expression directly into the algorithm expression.
> Typical use is
>
> struct Person
> {
> std::string name () const;
> };
>
> // Find "John"
> std::vector<Person>::iterator it = std::find_if (persons.begin(),
> persons.end(), somehow bind Person::name method == "John");
>
> While std::equal could be used in some simple cases, it does not accept
> mixed input like &Person::name and "John" (at least to my knowledge)
>
> Is there a way to accomplish this type of comparison using a mix of
> binding of functions /return values and plain values ?


compose1(bind2nd(equal_to<string>(), "John"), mem_fun_ref(&Person::name))

The above predicate takes a Person&, calls '.name()' on it, the passes
the result to a binder2nd which it to "John" using equal_to<string>.

Warning: compose1 is not part of the standard library, but is part of
the original STL. Look it up in <http://www.sgi.com/tech/stl/> for
example.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparison of 2 files and generating the output based on comparison Deepu Perl Misc 1 02-07-2011 03:09 PM
Price Comparison Service. Best Deal. Special Coupon atBest-Price-Comparison.com rapee Digital Photography 0 03-14-2008 06:46 AM
Strange FUNCTOR inconsistency RCS C++ 5 09-08-2003 01:45 PM
stl for_each makes two additional copies of functor Sean C++ 2 07-01-2003 07:19 AM
functor object in template class Chandra Shekhar Kumar C++ 5 06-26-2003 04:55 AM



Advertisments