in-place functor comparison instead of std::binary_function

N

news.online.no

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
 
P

peter steiner

news.online.no said:
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
 
D

Daniel T.

"news.online.no said:
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.
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top