I'm running into problems about defining a comparison function for a
map which has a user defined key. For example:
class Key {
public:
string name;
int number;
Key (na, nu) : name (na), number (nu) {}
bool operator< (const Key &key) const; //my question is how to
define this function
};
How can I most efficiently define this comparison function? I have a
workaround, which I suspect is not the right way:
bool Key::operator< (const Key &key) const {
stringstream stream1;
stringstream stream2:
stream1 << name << number;
stream2 << k.name << k.number;
return stream1.str() < stream2.str();
}
I believe this is not the right way, or is it? I tried other things
such as:
bool Key::operator< (const Key &key) const {
return number < key.number;
}
but then in this case I defeat the whole purpose of defining a key
based on a unique combination of a string, integer pair.
Thanks for any help.