functional object/adator and find_if()

C

cinsk

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.
 
V

Victor Bazarov

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
 
G

Gil

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( )
);
}
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top