in-place functor comparison instead of std::binary_function

Discussion in 'C++' started by news.online.no, Jan 30, 2006.

  1. 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
     
    news.online.no, Jan 30, 2006
    #1
    1. Advertising

  2. 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
     
    peter steiner, Jan 30, 2006
    #2
    1. Advertising

  3. news.online.no

    Daniel T. Guest

    In article <>,
    "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 ?


    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.
     
    Daniel T., Jan 30, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Denis Remezov

    binary_function and bind1st

    Denis Remezov, May 19, 2004, in forum: C++
    Replies:
    6
    Views:
    760
    P.J. Plauger
    May 21, 2004
  2. Christopher
    Replies:
    4
    Views:
    619
    James Kanze
    Nov 29, 2007
  3. yurec

    std::binary_function

    yurec, Mar 28, 2008, in forum: C++
    Replies:
    9
    Views:
    975
    Pete Becker
    Mar 31, 2008
  4. Frank Bergemann

    look-up table for std::binary_function's

    Frank Bergemann, Apr 23, 2008, in forum: C++
    Replies:
    0
    Views:
    322
    Frank Bergemann
    Apr 23, 2008
  5. Frank Bergemann

    look-up table for std::binary_function's

    Frank Bergemann, Apr 23, 2008, in forum: C++
    Replies:
    1
    Views:
    444
    Frank Bergemann
    Apr 23, 2008
Loading...

Share This Page