Boost logo

Boost Users :

From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2006-04-21 05:58:29


 Another way would be to implement a global less than operator overload. You can also implement
 this operator within a class, so that std::less_than is able to call it.

 Here are some examples...

 //gloabl operator overlaod sample
 class Edge
 {

         friend bool operator < (const Edge&, const Edge&);

         public:
                 //your implementation

         private:
                 //data members

 };

 inline bool operator < (const Edge& e1, const Edge& e2)
 {
         return (e1.prop1 < e2.prop1) && (e1.prop2 < e2.prop2) && ...;
 }

 //implementig the comaprison operator in the class
 // if you don't want this operator to be accessible from other classes,
 // declare the std::less_than<Edge> as friend and make this operator protected
 class Edge
 {
         friend class std::less_than<Edge>;

         protected:
                 inline bool operator <(const Edge& e)
                 {
                         return (prop1 < e.prop1) && (prop2 < e.prop2) && ...;
                 }

         //your class implementation
 };

 I do not say that this implementation is better then the one suggested by Dmitry, I would only
 like to show up all possiblities. Dmitri's implementation is more flexible in regards of compile
 time type calculations or if you don't have the source code of Edge. My implementation does not
 involve additional overhead required to create a function object for comparison and delegate
 comparison through it. In this case the object is only 1 byte big, but if you add additional
 fields to it memory usage will grow with every comparison.

 With Kind Regards,

 Ovanes Markarian

> On Fri, April 21, 2006 10:41, Dmitry Bufistov said:
>> I'm using the following for a couple of monthes. Seems to work.
>> //////////////////////////////////////////
>> struct edge_less_than
>> : public std::binary_function<edge_descriptor_t, edge_descriptor_t,
>> bool>
>> {
>> bool operator()(const edge_descriptor_t& __x,
>> const edge_descriptor_t& __y) const
>> {
>> //return __x.second.get_property() < __y.second.get_property();
>> //return &__x < &__y;
>> return __x.get_property() < __y.get_property();
>> }
>> };
>>
>> You can do something better depending of what you need. Possible this
>> will help
>> http://lists.boost.org/boost-users/2004/08/7579.php
>> Regards,
>> --dima
>> Thomas Costa wrote:
>>> write your own total ordering/comparison function object for Edge
>>> objects and declare the map type using this function.
>>>
>>> On Apr 20, 2006, at 7:55 PM, Irek Szczesniak wrote:
>>>
>>>
>>
>>
>>
>
>
>

With Kind Regards,

Ovanes Markarian


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