473,396 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

map comparison function for a user defined key

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
2 6742
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
125
by: Rhino | last post by:
One of my friends, Scott, is a consultant who doesn't currently have newsgroup access so I am asking these questions for him. I'll be telling him how to monitor the answers via Google Newsgroup...
37
by: spam.noam | last post by:
Hello, Guido has decided, in python-dev, that in Py3K the id-based order comparisons will be dropped. This means that, for example, "{} < " will raise a TypeError instead of the current...
0
by: HKSHK | last post by:
This list compares the error codes used in VB.NET 2003 with those used in VB6. Error Codes: ============ 3: This Error number is obsolete and no longer used. (Formerly: Return without GoSub)...
32
by: ma740988 | last post by:
template <class T> inline bool isEqual( const T& a, const T& b, const T epsilon = std::numeric_limits<T>::epsilon() ) { const T diff = a - b; return ( diff <= epsilon ) && ( diff >= -epsilon );...
7
by: matt | last post by:
hello, i have been given a challenging project at my org. i work on an inventory management web application -- keeping tracking of parts assigned to projects. in the past, i built an in-house...
0
by: SvenMathijssen | last post by:
Hi, I've been wrestling with a problem for some time that ought to be fairly simple, but turns out to be very difficult for me to solve. Maybe someone here knows the answer. What I try to do is...
11
by: Andrus | last post by:
I created dynamic extension methods for <= and < SQL comparison operators: public static IQueryable<TLessThanOrEqual<T>(this IQueryable<Tsource, string property, object value); public static...
5
by: evanevankan2 | last post by:
I have a question about the warning 'comparison between signed and unsigned' I get from my code. It comes from the conditional in the outer for loop. I understand the warning, but I'm not sure...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.