Re: [Boost-users] BGL: std::map<Edge, int> needs the < operator
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
{ 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
Ovanes, In your implementation you need to change edge_descriptor class. Is it possible to do the same without such modification? I was trying define such global operator bool operator<(const edge_descriptor_t& __x, const edge_descriptor_t&__y) { return __x.get_property() < __y.get_property(); } but it doesn't work for me (when I try to instantinate some std::map for edge I have compilation error: 1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const edge_descriptor_t' (or there is no acceptable conversion) Thank you, --dmitry Ovanes Markarian wrote:
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.
//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 };
Ovanes Markarian
On 4/21/06, Dmitry Bufistov
Ovanes, In your implementation you need to change edge_descriptor class. Is it possible to do the same without such modification? I was trying define such global operator
but it doesn't work for me (when I try to instantinate some std::map for edge I have compilation error: 1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const edge_descriptor_t' (or there is no acceptable conversion)
Is your global operator in the same namespace as edge_descriptor_t? -Michael Fawcett
participants (3)
-
Dmitry Bufistov
-
Michael Fawcett
-
Ovanes Markarian