Boost logo

Boost Users :

From: Daniel Wallin (dalwan01_at_[hidden])
Date: 2004-03-25 18:48:49


Chris Jurney wrote:

> I've had this same pattern come up 4 or 5 times now, and each time I've
> been unable to find a stl + boost solution that allowed me to do what I
> want without writing my own less function. Basically, I want something
> that will let me do a sort, or define a map on structs or pairs without
> writing a less that compares the member variable or .first.
> <snip>
> No such luck. It looks like I can do operator * and & on lambda
> objects, but not access members through them. Is there anything in
> boost, stl, or elsewhere that lets me not write this:
>
>
>
> class myLess
>
> {
>
> bool operator() (pair<float, float> a, pair<float, float>b)
>
> {return a.first < b.first;}
>
> }

Yes:

   struct X
   {
       int a;
   };

   std::vector<X> v;

With boost.bind:

   std::sort(
       v.begin()
     , v.end()
     , boost::bind(
           std::less<int>()
         , boost::bind(&X::a, _1)
         , boost::bind(&X::a, _2)
       )
   );

and with boost.lambda:

     std::sort(
         v.begin()
       , v.end()
       , bind(&X::a, _1) < bind(&X::a, _2)
     );

HTH,

-- 
Daniel Wallin

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