pls help to explain stl multiset's behavior for user-defined object
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
What bothers me is that the above program generates the following output:
Code:
lhs=SUN.Java rhs=MS.VC
lhs=SUN.Java rhs=MS.VC
lhs=MS.Word rhs=SUN.Java
lhs=MS.Word rhs=MS.VC
lhs=MS.Word rhs=SUN.Java
lhs=MS.Word rhs=SUN.Java
lhs=Apple.Mac rhs=SUN.Java
lhs=Apple.Mac rhs=MS.Word
lhs=Apple.Mac rhs=MS.VC
lhs=Apple.Mac rhs=MS.VC
lhs=MS.SQL rhs=MS.Word
lhs=MS.SQL rhs=SUN.Java
lhs=MS.Word rhs=MS.SQL
lhs=MS.VC rhs=MS.SQL
lhs=Apple.Mac rhs=MS.SQL
There are 2 MS tools in the set
First of all, how come the operator== is not called at all? How could it be
possible that object tools("MS","SQL") is considered equal to tools("MS","VC")?
Please take a look at "line 1", I intentionally did not compare their names to
make them different. But it did not matter. I thought count() should return 0,
but apparently it returns 2.
Second, why is the operator< called so many times? For instance,
lhs=SUN.Java rhs=MS.VC
lhs=SUN.Java rhs=MS.VC
is printed twice in a row which indicates operator< was called twice.
What's actually happening behind STL's multiset?
Is search tree the underlying data structure of multiset?
Struggled on this for quite a while.
Really appreciate your explanation of what's going on here.
The multiset sorts the elements that you put into it. See http://www.sgi.com/tech/stl/multiset.html, in particular note that the default for Compare is less<Key>, and that it should have a Strict Weak Ordering. I think your operator< does not since (tool1 < tool2) would compare a different set of strings than (tool2 < tool1).
The multiset sorts the elements that you put into it. See http://www.sgi.com/tech/stl/multiset.html, in particular note that the default for Compare is less<Key>, and that it should have a Strict Weak Ordering. I think your operator< does not since (tool1 < tool2) would compare a different set of strings than (tool2 < tool1).
Thanks. But can you elaborate what you meant by 'I think your operator< does not since (tool1 < tool2) would compare a different set of strings than (tool2 < tool1)"?
In addition, what about operator==? what does count() actually do?
It should take Key which an object of tools and then look it up in the underlying
RB-tree, right? I just cannot understand why tools("MS", "SQL") and tools("MS", "VC")
are considered equal to each other. My plain thinking is equality should be involved in count().
Maybe I am dead wrong.
Got it. It was due to strict weak ordering used by multiset
which is defined by operator<.
The current operator< used by class tools is saying as long as
two objects have the same "company" string, they are equal.
So by changing operator<, I could see the difference.
Thanks. But can you elaborate what you meant by 'I think your operator< does not since (tool1 < tool2) would compare a different set of strings than (tool2 < tool1)"?
Consider
Code:
tools tool1("aa", "bb"), tool2("ab", "ab");
tool1 < tool2 //does ("aa" < "ab") result is true
tool2 < tool1 //does ("ab" < "bb") result is true
As you can see according to your operator< tool1 is less than tool2 and tool2 is less than tool1, kind of like an M.C. Escher painting...
Quote:
In addition, what about operator==? what does count() actually do?
It should take Key which an object of tools and then look it up in the underlying
RB-tree, right? I just cannot understand why tools("MS", "SQL") and tools("MS", "VC")
are considered equal to each other. My plain thinking is equality should be involved in count().
Maybe I am dead wrong.
The multiset makes the minimum assumptions about your type, so it only uses operator< not operator==. Since your operator< behaves strangely, functions like count() don't work right.
tools tool1("aa", "bb"), tool2("ab", "ab");
tool1 < tool2 //does ("aa" < "ab") result is true
tool2 < tool1 //does ("ab" < "bb") result is true
As you can see according to your operator< tool1 is less than tool2 and tool2 is less than tool1, kind of like an M.C. Escher painting...
The multiset makes the minimum assumptions about your type, so it only uses operator< not operator==. Since your operator< behaves strangely, functions like count() don't work right.
I cannot quite get why tool2 < tool1 also returns true because I think what happens
is: return string("ab") < string("aa"). I don't know why it should do "ab" < "bb", assuming
you meant they are actually strings, not char[]. In the function operator<, the second field,
i.e., name, is not used at all.
I changed function operator< to this and it seems work the way I preferred:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.