I like your solution best of the 3 but:
Thomas Tutone wrote:
>
add this here:
int getAge() const { return age; }
}
****************
how do I sort groupA by age?
#include <functional>
using namespace std;
typedef const mytype& crMytype;
struct less_age : public binary_function<crMytype, crMytype, bool{
bool operator()(crMytype x, crMytype y)
{ return x.getAge() < y.getAge(); }
};
of course you wouldn't put using namespace std; in a header. But you
might want this functor in a header. You might even nest it in the
class.
Now is there a binder for this? I don't know, let's try to create one:
template < typename T >
struct less_mem_fun_ref : public binary_function< const T & t1, const T
& t2, bool >
{
typedef bool (* T::Pred )(void); // sorry I can't remember the
syntax
Pred pred;
less_mem_fun_ref( Pred p ) : pred( p ) {}
bool operator() ( const T& t1, const T& t2 )
{
return t1.pred() < t2.pred();
}
};
then go through the usual process of writing a function to obtain one.
We might also template the use of operator<.
How to do it with boost, anyone?
sort(groupA.begin(), groupA.end(), less_age());
With mine you'd put in make_less_mem_fun_ref( &Mytype::getAge ) as the
last parameter. Of course you need to include <algorithmas well to
use sort.
With mine you'll write the functor once only then invoke it with
different classes, different members etc.