|
Boost : |
From: Sam Partington (Sam.Partington_at_[hidden])
Date: 2002-07-25 04:40:50
We use something similar here, but we go for the general functions:
binary_select_member and unary_select_member
to work on general function objects, but since 99% of the time you use them
with either less<> or equal_to<> (more often than not with bind2nd as well)
and so we provide extra helper functions less_member and equal_to_member.
Finally since nearly all data is private and is accessed through accessor
functions there are equivalent functions for all functions to act on the
results of a function, which are the same names appended with _func.
basically allows us to do the following:
class T
{
public:
int x;
int GetY() const { return y; }
private:
int y;
};
std::vector<T> avector;
...
std::sort(avector.begin(), avector.end(), less_member(&T::x));
std::sort(avector.begin(), avector.end(), less_member_func(&T::GetY));
std::sort(avector.begin(), avector.end(),
binary_select_member(std::greater<int>(), &T::x));
std::sort(avector.begin(), avector.end(),
unary_select_member(std::greater<int>(), &T::GetY));
std::find_if(avector.begin(), avector.end(), equal_to_member(1, &T::x));
std::find_if(avector.begin(), avector.end(), equal_to_member(1, &T::GetY));
you get the picture.
sam
> -----Original Message-----
> From: boost-bounces_at_[hidden]
> [mailto:boost-bounces_at_[hidden]]On Behalf Of Victor A. Wagner, Jr.
> Sent: 25 July 2002 09:22
> To: boost_at_[hidden]
> Subject: Re: [boost] Small utility templates
>
>
> only one small nit...see below
> At Thursday 2002/07/25 00:38, you wrote:
> >Maciej Sobczak wrote:
> >>Some time ago I wrote the adaptor class that allows you to wrap
> >>*arbitrary* functor (less, greater, etc.) in a uniform manner:
> >
> >Of course, forgot to mention: that adaptor was for different, specific
> >purposes.
> >The one for sorting (for operations on two aggregate objects) can look
> >like here (with complete test program, stripped from #includes):
> >
> >template <class T, typename M, class OP>
> >class oper_on_mem_t : binary_function<T, M, bool>
> >{
> >public:
> > explicit oper_on_mem_t(M T:: *m, OP op) : m_(m), op_(op) {}
> >
> > bool operator()(const T &lhs, const T &rhs) const
> > {
> > return op_(lhs.*m_, rhs.*m_);
> > }
> >private:
> > M T::* m_;
> > OP op_;
> >};
> >
> >template <class T, typename M, class OP>
> >oper_on_mem_t<T, M, OP> oper_on_mem(M T:: *m, OP op)
> >{
> > return oper_on_mem_t<T, M, OP>(m, op);
> >}
> >
> >struct S { int a, b, c; };
> >
> >int main ()
> >{
> > S s1 = {1,2,3};
> > S s2 = {3,1,2};
> > S s3 = {2,3,1};
> >
> > vector<S> v;
> > v.push_back(s1);
> > v.push_back(s2);
> > v.push_back(s3);
> >
> > sort(v.begin(), v.end(), oper_on_mem(&S::a, less<int>()));
>
> it's a shame you have to say <int> here.... it's deducible from S::a
> then again...maybe there is a way... I'm by no means an expert on writing
> templates
>
>
> > for (vector<S>::iterator i = v.begin(); i != v.end(); ++i)
> > cout << "(" << i->a << ", " << i->b
> > << ", " << i->c << ")" << endl;
> >
> > return 0;
> >}
> >
> >Prints:
> >(1, 2, 3)
> >(2, 3, 1)
> >(3, 1, 2)
> >
> >The motivation is to write the adaptor that will be able to accommodate
> >different needs, so that users will be able to use it uniformly with
> >different, supplied operations. This allows also to write generic code
> >that is independent of the actually provided comparison functor.
> >
> >Functors like less_element or greater_element cannot achieve this.
> >
> >Does Boost have something similar to this?
> >
> >Cheers,
> >
> >Maciej Sobczak
> >http://www.maciejsobczak.com/
> >
> >_______________________________________________
> >Unsubscribe & other changes:
http://lists.boost.org/mailman/listinfo.cgi/boost
Victor A. Wagner Jr. http://rudbek.com
PGP RSA fingerprint = 4D20 EBF6 0101 B069 3817 8DBF C846 E47A
PGP D-H fingerprint = 98BC 65E3 1A19 43EC 3908 65B9 F755 E6F4 63BB 9D93
The five most dangerous words in the English language:
"There oughta be a law"
_______________________________________________
Unsubscribe & other changes:
http://lists.boost.org/mailman/listinfo.cgi/boost
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk