Boost logo

Boost Users :

From: David Klein (d.klein_at_[hidden])
Date: 2006-03-21 11:17:15


boost-users-bounces_at_[hidden] wrote:
> I would like to define two different sorting predicates for a class
> but I do not understand how to do this.
>
> //meteorlogicalObj.hpp
> class meteorlogicalObj {
> public:
> meteorlogicalObj(int val, Point pt) : val_(val), pt_(pt){}
> virtual ~meteorlogicalObj() {}
>
> // how would I go about creating these sorting functions that would
> work with the ptr_vector class
> bool sortOnValue(const meteorlogicalObj&, const meteorlogicalObj&);
> bool sortOnPoint(const meteorlogicalObj&, const meteorlogicalObj&);
> // ...
> private:
> int val_;
> Point pt_;
> };
>
> //meteorlogicalObj.cpp
> #include "meteorlogicalObj.h"
>
> bool meteorlogicalObj::sortOnValue(const meteorlogicalObj& metObj,
> const meteorlogicalObj& other)
> {
> return (metObj.val_ < other.val_);
> }
>
>
> bool meteorlogicalObj::sortOnPoint(const meteorlogicalObj& metObj,
> const meteorlogicalObj& other)
> {
> return (metObj.pt_ < other.pt_);
> }
>
> //main.cpp
> boost::ptr_vector<meteorlogicalObj> metObjVec;
>
> //... fill vector
>
> metObjVec.sort( sortOnValue() ); // this does not work, what is
> missing??? metObjVec.sort( sortOnPoint() ); // this does not work,
> what is missing???
>
> I can see that I probably need the indect_fun but I do not know how to
> wrap my sorting function to pass them to the sorting function
>
>
>
> Thank you for your assistance!
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users

hi sebastien,
try the following changes...

1) make the sort function static

class meteorlogicalObj
{
        ...
        static bool sortOnValue(const meteorlogicalObj&, const
meteorlogicalObj&);
        ...
};

2) call sort like this

metObjVec.sort(&meteorlogicalObj::sortOnValue);

HTH dave


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