944,000 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 16295
  • C++ RSS
May 11th, 2005
0

Help with < (less than) operator on classes

Expand Post »
Hi,
I'm trying to use the STL map class to be able to store keys and associated values. I started out using it with keys of pre-existing data types like int, string, etc. and was working fine. I was also able to use it with values of custom classes.

I'm now trying to use it to store keys of custom classes. To do so, it appears to require the creation of the < operator (presumably so that the map can sort the keys).

My question is this - what is the proper code inside the < operator? Below is what I have, (and it compiles and seems to work), but I'm not sure it makes sense. How would it need to change if I had other data types inside my KeyObj class as well? Also, should I somehow account for the fact that if lastName is blank on one object and firstName is blank on the other object being compared, that they might be considered equal? - would I handle that by putting a separator in between when comparing?)
C++ Syntax (Toggle Plain Text)
  1.  
  2. class KeyObj
  3. {
  4. public:
  5. string lastName;
  6. string firstName;
  7. bool operator< (const KeyObj &compare)
  8. {
  9. return lastName+firstName < compare.lastName+compare.firstName;
  10. }
  11. };
  12. bool operator< (const KeyObj &source, const KeyObj &compare)
  13. {
  14. return source.lastName+source.firstName < compare.lastName+compare.firstName;
  15. }
  16. int main( int argc, char * argv[] )
  17. {
  18. map<KeyObj,string> myMap;
  19. KeyObj k;
  20. k.lastName="JONES";
  21. k.firstName="BOB";
  22. myMap[k] = "MR. BOB JONES";
  23. KeyObj k2;
  24. k2.lastName="JONES";
  25. k2.firstName="FRED";
  26. myMap[k2] = "MR. FRED JONES";
  27. cout<<"Searching for FRED JONES:"<<endl;
  28. KeyObj test;
  29. test.lastName="JONES";
  30. test.firstName="FRED";
  31. if ( myMap.find(test) != myMap.end() )
  32. {
  33. cout<<myMap[test]<<endl;
  34. }
  35. else
  36. cout<<"COULDN't FIND IT"<<endl;
------------------------

Searching for FRED JONES:
MR. FRED JONES
Last edited by winbatch; May 11th, 2005 at 9:41 am. Reason: Code tags
Similar Threads
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
May 11th, 2005
0

Re: Help with < (less than) operator on classes

Code tags don't help much if your code isn't formatted to begin with.

>what is the proper code inside the < operator?
That depends on the needs of the class. Does it make more sense to compare by first name, last name, or both? How you define operator< depends heavily on that decision. However, once it's made, the implementation is somewhat trivial in your case:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4.  
  5. class Person {
  6. std::string _first, _last;
  7. public:
  8. Person(const std::string& first, const std::string& last)
  9. : _first(first), _last(last)
  10. {}
  11.  
  12. bool operator<(const Person& p) const
  13. {
  14. //return _last < p._last;
  15. //return _first < p._first;
  16. return _last + _first < p._last + p._first;
  17. }
  18.  
  19. friend std::ostream& operator<<(std::ostream& out, const Person& p)
  20. {
  21. return out<< p._last <<", "<< p._first;
  22. }
  23. };
  24.  
  25. int main()
  26. {
  27. std::map<Person, int> m;
  28.  
  29. ++m[Person("John", "Doe")];
  30. ++m[Person("Jane", "Doe")];
  31. ++m[Person("", "Doe")];
  32. ++m[Person("John", "Doe")];
  33. ++m[Person("John", "")];
  34.  
  35. std::map<Person, int>::iterator it = m.begin();
  36.  
  37. while (it != m.end()) {
  38. std::cout<< it->first <<" -- "<< it->second <<std::endl;
  39. ++it;
  40. }
  41. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 11th, 2005
0

Re: Help with < (less than) operator on classes

Narue,

Assuming I want to compare by both last and first (I created this as a trivial example - I have many more fields, all of which are important) - do I need to deal with an 'empty' last or an 'empty' first which might make two items equal? (or even better a last name of "AB" with a first name of "CD" vs. a last name of "A" and a first name of "BCD"..

(ie would lastname + "-" + firstname < other.lastname + "-" + other.firstname )

make it 'safer'?

Also, in the syntax you are using for inserting into the map(which I've never seen before), what int is being assigned for the value?
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
May 11th, 2005
0

Re: Help with < (less than) operator on classes

Quote originally posted by winbatch ...
Also, in the syntax you are using for inserting into the map(which I've never seen before), what int is being assigned for the value?
Beautiful isn't it?
Reputation Points: 44
Solved Threads: 1
Junior Poster
subtronic is offline Offline
117 posts
since Aug 2003
May 11th, 2005
0

Re: Help with < (less than) operator on classes

>do I need to deal with an 'empty' last or an 'empty' first which might make two items equal?
The only situation where this would be true is if the last name is the same and the first name is empty for both objects (or vice versa). Of course, they would be equal and it's hard to figure out a way to avoid that since you would basically be saying "Strings that are equal are equal, except...", which is nonsensical.

>or even better a last name of "AB" with a first name of "CD" vs. a last name of "A" and a first name of "BCD".
Now that makes more sense, and you're on the right track for how to solve it.

>what int is being assigned for the value?
If not value is given, it defaults to T(), which is 0 for int. So for each name added, I'm incrementing the value starting with 0.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 11th, 2005
0

Re: Help with < (less than) operator on classes

Narue,

You mentioned in one of the earlier posts that it depends on how I want to compare whether I compare both last name and first name or just last name, etc. If a map is unique/sorted, wouldn't I be required to include every class field in the < operator?
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
May 11th, 2005
0

Re: Help with < (less than) operator on classes

>If a map is unique/sorted, wouldn't I be required to include every class field in the < operator?
Only if you want every member to be a part of the key. It depends on the class what it means for one object to be less than another.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: fastest compiler
Next Thread in C++ Forum Timeline: string input not working as expected





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC