functional object/adator and find_if()

Discussion in 'C++' started by cinsk, Oct 1, 2010.

  1. cinsk

    cinsk Guest

    In the following code:

    class session {
    public:
    explicit session(int id) : id_(id) {}
    virtual ~session() {}

    int id() { return id_; }
    bool equal_id(int id) { return (id_ == id); }
    // ...
    private:
    int id_;
    };

    typedef list<session *> session_list;

    session_list all_sessions;

    If I need a global function that find a session pointer in
    'all_sessions' using given session id, I could write following
    function:

    session *find_session_by_id(int id) {
    session_list::iterator i = find_if(all_sessions.begin(),
    all_sessions.end(),

    bind2nd(mem_fun(&session::equal_id), id));
    if (i == all_sessions.end())
    return 0;
    else
    return *i;
    }

    Q1. But, if session::equal_id() is not existed, is it possible to
    write a function
    similar to the above one? Perhaps using std::equal_to<>() with
    some others??


    Q2. In stead of session::equal_id(), if class 'session' has a member
    function like this:

    bool session::equal(const session *s) { return (id_ == s->id_); }

    Can I write a find_session_by_id(int id) using session::equal
    with some
    standard functional objects/adaptors?

    Thanks in advance.
     
    cinsk, Oct 1, 2010
    #1
    1. Advertising

  2. On 10/1/2010 9:38 AM, cinsk wrote:
    > In the following code:
    >
    > class session {
    > public:
    > explicit session(int id) : id_(id) {}
    > virtual ~session() {}
    >
    > int id() { return id_; }


    int id() const { return id_; }

    > bool equal_id(int id) { return (id_ == id); }


    bool equal_id(int id) const { return id_ == id; }

    > // ...
    > private:
    > int id_;
    > };
    >
    > typedef list<session *> session_list;
    >
    > session_list all_sessions;
    >
    > If I need a global function that find a session pointer in
    > 'all_sessions' using given session id, I could write following
    > function:
    >
    > session *find_session_by_id(int id) {
    > session_list::iterator i = find_if(all_sessions.begin(),
    > all_sessions.end(),
    >
    > bind2nd(mem_fun(&session::equal_id), id));
    > if (i == all_sessions.end())
    > return 0;
    > else
    > return *i;
    > }
    >
    > Q1. But, if session::equal_id() is not existed, is it possible to
    > write a function
    > similar to the above one? Perhaps using std::equal_to<>() with
    > some others??


    You would have to create a functor that would extract the 'id' from the
    'session' and then compare it with the argument in its op() member.

    > Q2. In stead of session::equal_id(), if class 'session' has a member
    > function like this:
    >
    > bool session::equal(const session *s) { return (id_ == s->id_); }


    bool session::equal(const session *s) const { ...

    >
    > Can I write a find_session_by_id(int id) using session::equal
    > with some
    > standard functional objects/adaptors?


    I don't know. Can you?

    Think of creating a fake session and then searching the list as you
    usually would with your new 'equal' as the comparator. You will likely
    need to bind that temporary session to that member function using
    std::bind1st.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Oct 1, 2010
    #2
    1. Advertising

  3. cinsk

    Gil Guest

    On Oct 1, 9:38 am, cinsk <> wrote:
    > In the following code:
    >
    >   class session {
    >   public:
    >     explicit session(int id) : id_(id) {}
    >     virtual ~session() {}
    >
    >     int id() { return id_; }
    >     bool equal_id(int id) { return (id_ == id); }
    >     // ...
    >   private:
    >     int id_;
    >   };
    >
    >   typedef list<session *> session_list;
    >
    >   session_list all_sessions;
    >
    > If I need a global function that find a session pointer in
    > 'all_sessions' using given session id, I could write following
    > function:
    >
    >   session *find_session_by_id(int id) {
    >     session_list::iterator i = find_if(all_sessions.begin(),
    >                                        all_sessions.end(),
    >
    > bind2nd(mem_fun(&session::equal_id), id));
    >     if (i == all_sessions.end())
    >       return 0;
    >     else
    >       return *i;
    >   }
    >
    > Q1. But, if session::equal_id() is not existed, is it possible to
    > write a function
    >     similar to the above one?  Perhaps using std::equal_to<>() with
    > some others??
    >
    > Q2. In stead of session::equal_id(), if class 'session' has a member
    > function like this:
    >
    >     bool session::equal(const session *s) { return (id_ == s->id_); }
    >
    >        Can I write a find_session_by_id(int id) using session::equal
    > with some
    >       standard functional objects/adaptors?
    >
    > Thanks in advance.


    /**
    * @file: find_if_id.cpp
    * @author: gil
    */

    #include <iostream>
    #include <list>
    #include <algorithm>
    #include <boost/lambda/lambda.hpp>
    #include <boost/lambda/bind.hpp>
    #include <boost/lambda/construct.hpp>

    class session {
    public:
    explicit session( int id ) : id_( id ) {}
    virtual ~session() { }

    int id() { return id_; }
    // ...
    private:
    int id_;
    };

    typedef std::list< session * > session_list;
    session_list all_sessions;

    using namespace boost::lambda;

    session *
    find_session_by_id( int id ) {
    session_list::iterator i = std::find_if(
    all_sessions.begin( ),
    all_sessions.end( ),
    bind( &session::id, _1 ) == id
    );
    return all_sessions.end( ) == i ? NULL: *i;
    }

    int main( ) {
    int index = 0;

    std::generate_n(
    std::back_inserter( all_sessions ),
    10,
    bind( new_ptr< session >( ), ++var( index ) )
    );

    std::for_each(
    all_sessions.begin( ),
    all_sessions.end( ),
    std::cout << bind( &session::id, _1 )
    << constant( ' ' )
    << _1
    << constant( '\n' )
    );

    std::cout << "session with id equal to 3: "
    << find_session_by_id( 3 )
    << std::endl;

    std::for_each(
    all_sessions.begin( ),
    all_sessions.end( ),
    delete_ptr( )
    );
    }
     
    Gil, Oct 1, 2010
    #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. marco_segurini
    Replies:
    3
    Views:
    2,286
    Karl Heinz Buchegger
    Jul 7, 2004
  2. Michael

    functors and stl find_if

    Michael, Feb 19, 2005, in forum: C++
    Replies:
    4
    Views:
    6,280
    SnaiL
    Feb 19, 2005
  3. Misiu
    Replies:
    3
    Views:
    2,389
    Misiu
    Jan 31, 2007
  4. Replies:
    1
    Views:
    484
  5. Urs Thuermann
    Replies:
    1
    Views:
    1,736
    Michael DOUBEZ
    Jul 7, 2011
Loading...

Share This Page