|
Boost : |
From: Marc Guiot (marc.guiot_at_[hidden])
Date: 2000-09-11 04:32:21
--- In boost_at_[hidden], Gary Powell <Gary.Powell_at_s...> wrote:
>
> > -----Original Message-----
> >
> > I'd like to know if you plan to add things like the
> > mem_fun_t but for member attributs.
> Nope: as a Lambda expression does this nicely.
I don't find it in the boost library so
where can I find the Lambda or Peter's library.
Thank
>
> > 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>()));
> ------------
> l.sort(bind(free1,&pair<string, int>::first) >
> bind(free2,&pair<string, int>::first) );
>
> or:
> l.sort(free1->*(&pair<string, int>::first) >
> (free2->*(&pair<string, int>::first) );
>
>
> --------------
>
> > 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());
> ___________
>
> // with Lambda
> for_each(l.begin(), l.end(),
> cout << free1->*(&pair<string,int>::first)
> << free2->*(&pair<string,int>::second) << endl);
>
> l.sort(free1->*(&pair<string,int>::first) <
> free2->*(&pair<string,int>::first) );
>
> > return 0;
> > }
> >
> -----------
> The advantage is no need for writing "Out()", and the algorithm is
on the
> same line as the binary, or unary op.
>
> Yet more shameless pushing for people to try Lambda or Peter's
library.
>
> -gary-
>
> gary.powell_at_s...
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk