Question on ptr_vector class

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Question on ptr_vector class

Sebastien Fortier
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
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Question on ptr_vector class

David Klein-2
[hidden email] 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
> [hidden email]
> 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 mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Question on ptr_vector class

David Klein-2
In reply to this post by Sebastien Fortier
[hidden email] 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
> [hidden email]
> 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 mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Question on ptr_vector class

Thorsten Ottosen
In reply to this post by Sebastien Fortier
Sebastien Fortier 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


You don't need to use indirect_fun<>, the algorithms do that themselves.

What you is a functor that can be properly contructed:

struct SortOnValue
{
   typedef bool result_type;
   template< class M >
   bool operator()( const M& l, const M& r ) const
   { return l.value() < r.value(); }
};

I assume that you have added a function called value to
meteorlogicalObj. Alternatively you can make the class SortOnValue a
friend to the class.

-Thorsten

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users