|
Boost : |
From: Marc Guiot (marc.guiot_at_[hidden])
Date: 2000-09-08 07:22:29
Hi,
I'd like to know if you plan to add things like the
mem_fun_t but for member attributs.
For example :
template <class T, class D, D T::*val>
class mem_var1_t : public unary_function<T*, D>
{
public :
D operator()(T *f) const
{
return f->*val;
}
};
So we can do a sort on a list of pair without
defining an adaptor.
list<pair<string, int>*> l;
l.sort(compose_f_gx_hy(greater<string>() ,
mem_var1_t<pair<string, int>, string,
&pair<string, int>::first>(),
mem_var1_t<pair<string, int>, string,
&pair<string, int>::first>()));
Thanks
--- - Marc Guiot - marc.guiot_at_[hidden] - C++, XP --- A full example : #include <list> #include <iostream> #include <string> #include <utility> #include <functional> #include <algorithm> using namespace std; // begin boost /* supplementing compose function objects * Fri Jul 16 21:01:58 MEST 1999 */ /* The following code example is taken from the book * "The C++ Standard Library - A Tutorial and Reference" * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */ template <class OP1, class OP2, class OP3> class compose_f_gx_hy_t : public std::binary_function<typename OP2::argument_type, typename OP3::argument_type, typename OP1::result_type> { private: OP1 op1; // process: op1(op2(x),op3(y)) OP2 op2; OP3 op3; public: // constructor compose_f_gx_hy_t (const OP1& o1, const OP2& o2, const OP3& o3) : op1(o1), op2(o2), op3(o3) { } // function call typename OP1::result_type operator()(const typename OP2::argument_type& x, const typename OP3::argument_type& y) const { return op1(op2(x),op3(y)); } }; /* convenience function for the compose_f_gx_hy adapter */ template <class OP1, class OP2, class OP3> inline compose_f_gx_hy_t<OP1,OP2,OP3> compose_f_gx_hy (const OP1& o1, const OP2& o2, const OP3& o3) { return compose_f_gx_hy_t<OP1,OP2,OP3>(o1,o2,o3); } // end boost // add compose where g==h template <class OP1, class OP2> inline compose_f_gx_hy_t<OP1,OP2,OP2> compose_f_gx_hy (const OP1& o1, const OP2& o2) { return compose_f_gx_hy_t<OP1,OP2,OP2>(o1,o2,o2); } // here mem_var1_t template <class T, class D, D T::*val> class mem_var1_t : public unary_function<T*, D> { public : D operator()(T *f) const { return f->*val; } }; // std only template <class T, class D, D T::*val, class Comp> class comp { public : bool operator ()(const T *f, const T *s) { return Comp()(f->*val,s->*val); } }; struct Out { void operator ()(pair<string, int>* p) const { cout << p->first << ' ' << p->second << endl; } }; int main() { list<pair<string, int>*> l; l.push_back(new pair<string, int>("b", 2)); l.push_back(new pair<string, int>("a", 1)); for_each(l.begin(), l.end(), Out()); // std only l.sort(comp<pair<string, int>, string, &pair<string, int>::first, less<string> >()); // boost and mem_var1_t for_each(l.begin(), l.end(), Out()); l.sort(compose_f_gx_hy(greater<string>() , mem_var1_t<pair<string, int>, string, &pair<string, int>::first>())); for_each(l.begin(), l.end(), Out()); return 0; }
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk