STL and operator< (for the less<>function)
I have defined a class T with 2 attributes, overloaded the operator< , and used this as the first parameter in an the stl multimap.
The problem is the compiler complains that stl can't handle the overloaded operator<. Some error about using the this pointer
and how it doesn't qualify as a parameter in the stl function.h
I hope you know better than I what that all means, because I was sure that operator overloading was necessary for user defined classes used as the key in a multimap and using the less<> function to do insertions into it.
Please email me any suggestions at: [email protected]
Re: STL and operator< (for the less<>function)
Hi ! It's not very clear what errors you was getting, but define "operator<()" in your class as in this example. Defining of "operator<" is not only way to use your class as the key for the multimap - but I'm sure - the simplest one... Here is the example:
struct MyKeyData
{
int iValue;
double dValue;
bool operator<(MyKeyData const& rhs) const
{
return iValue < rhs.iValue;
}
};
also you might define "operator<" just as the non-member function (I prefer this way because
it's sipmlifying the interface of the class and for other minor reasons):
bool operator<(MyKeyData const& key1, MyKeyData const& key2)
{
return key1.iValue < key2.iValue;
}
Please - rate answer if it helped you
It gives me inspiration when I see myself in the top list =)
Best regards,
-----------
Igor Soukhov (Brainbench/Tekmetrics ID:50759)
[email protected] | ICQ:57404554 | http://soukhov.com
Member of Russian Software Developer Network http://rsdn.ru