By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,422 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,422 IT Pros & Developers. It's quick & easy.

map comparison function for a user defined key

P: n/a
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.

May 2 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
eastern_strider wrote:
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.


First, you should probably make your comparison a non-member function
in the same namespace as Key. That will allow implicit conversions on
the left-hand argument. See _C++ Coding Standards_, Items 27 and 44.
Ordinarily, you might also make it a friend function, but because of
the public data and lack of member functions, it looks like your Key
class is really just an aggregation, rather than an abstraction, and so
friendship is unnecessary.

Now, to answer your question: You can make the less-than operator
behave however is appropriate for your class. It looks like what you
want might be something like this:

bool Key::operator< ( const Key& k1, const Key& k2 )
{
return (k1.name < k2.name) && (k1.number < k2.number);
}

Cheers! --M

May 2 '06 #2

P: n/a

mlimber wrote:
eastern_strider wrote:
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.


First, you should probably make your comparison a non-member function
in the same namespace as Key. That will allow implicit conversions on
the left-hand argument. See _C++ Coding Standards_, Items 27 and 44.
Ordinarily, you might also make it a friend function, but because of
the public data and lack of member functions, it looks like your Key
class is really just an aggregation, rather than an abstraction, and so
friendship is unnecessary.

Now, to answer your question: You can make the less-than operator
behave however is appropriate for your class. It looks like what you
want might be something like this:

bool Key::operator< ( const Key& k1, const Key& k2 )
{
return (k1.name < k2.name) && (k1.number < k2.number);
}


Oops. Drop the member function qualifier (Key::) above.

Cheers! --M

May 2 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.