Boost logo

Boost Users :

Subject: Re: [Boost-users] [lambda] std::sort problem
From: Stuart Dootson (stuart.dootson_at_[hidden])
Date: 2009-01-25 08:48:21


2009/1/25 Micha³ Nowotka <mmmnow_at_[hidden]>:
> Right, there is a mistake.
>
> So the problem is:
>
> Suppose i defined:
> typedef std::vector<std::pair<const SomeClass*,double>* > MyType;
>
> So if I want to keep DRY i should write:
>
> lambda::_1 ->*&MyType::value_type::second > lambda::_2
> ->*&MyType::value_type::second);
>
> I also tried:
> bind(&MyType::value_type::second, lambda::_1) >
> bind(&MyType::value_type::second, lambda::_2)
>
> but none of this expression works for me.

Well, it's hardly surprising, as the value_type of the vector is a
pointer, so won't have a member called 'second'.

Let's refactor the DRY-ness a little:

typedef std::pair<const MyClass*,double> ElemType;
typedef std::vector<ElemType *> MyType;
MyType v;

Then you can use this:
std::sort(v.begin(), v.end(), bind(&ElemType::second, _1) >
bind(&ElemType::second, _2));

?

Stuart Dootson


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net