473,386 Members | 1,647 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,386 software developers and data experts.

pls help to explain stl multiset's behavior for user-defined objec

5
Thanks for your patience to read the entire post to understand my
confusion I have the user-defined class "tools":

Expand|Select|Wrap|Line Numbers
  1. class tools{
  2.   public:
  3.     tools( ......) {}
  4.     friend bool operator< (const tools& lhs, const tools& rhs)  {
  5.        cout<<"lhs="<<lhs.company<<"."<<lhs.name
  6.               <<" rhs="<<rhs.company<<"."<<rhs.name<<endl;
  7.        return lhs.company < rhs.company;
  8.    }
  9.     friend bool operator== (const tools& lhs, const tools& rhs) {
  10.        cout<<" in op=="<<endl;
  11.        return lhs.company == rhs.company ;
  12.    //note, without &&lhs.name==rhs.name; // line 1
  13.    }
  14.   private:
  15.     string company;
  16.     string name;
  17. };
  18.  
  19.  
in main(),I want to use multiset on user-defined objects:
Expand|Select|Wrap|Line Numbers
  1. tools toolArr[] = {tools("MS", "VC"),
  2.     tools("SUN", "Java"),
  3.     tools("MS", "Word") ,
  4.     tools("Apple", "Mac")
  5.   };
  6.   int Size = sizeof(toolArr)/sizeof(tools);
  7.   multiset<tools> toolSet(toolArr, toolArr+Size);
  8.   string cmpy("MS");
  9.   cout << "There are " <<toolSet.count(tools(cmpy, "SQL"))
  10.     << " " << cmpy << " tools in the set" << endl << endl;
  11.    }
  12.  
What bothers me is that the above program generates the following output:
Expand|Select|Wrap|Line Numbers
  1. lhs=SUN.Java rhs=MS.VC
  2. lhs=SUN.Java rhs=MS.VC
  3. lhs=MS.Word rhs=SUN.Java
  4. lhs=MS.Word rhs=MS.VC
  5. lhs=MS.Word rhs=SUN.Java
  6. lhs=MS.Word rhs=SUN.Java
  7. lhs=Apple.Mac rhs=SUN.Java
  8. lhs=Apple.Mac rhs=MS.Word
  9. lhs=Apple.Mac rhs=MS.VC
  10. lhs=Apple.Mac rhs=MS.VC
  11.  
  12.  
  13. lhs=MS.SQL rhs=MS.Word
  14. lhs=MS.SQL rhs=SUN.Java
  15. lhs=MS.Word rhs=MS.SQL
  16. lhs=MS.VC rhs=MS.SQL
  17. lhs=Apple.Mac rhs=MS.SQL
  18.  
  19. There are 2 MS tools in the set
  20.  
  21.  
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.
Feb 20 '08 #1
2 1964
Laharl
849 Expert 512MB
Multiset, like set, uses a red-black tree, which is a faster variation on the binary search tree we all know and love. This would be an n-ary tree rather than a binary tree, but the idea is similar.
Feb 20 '08 #2
parvtb
5
yeah, I figured out it uses RB-tree.
But my main doubt is how the == operator works.
In my case, each Key inserted into the multiset is an object.
I've defined my own == operator to compare two objects.
Well, it is not used by the program obviously.
So it boils down to why the == operator comes with multiset
decides the two objects, tools("MS", "SQL") and tools("MS", "VC"),
equal to each other.
Feb 20 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: joeboatertx | last post by:
I have some simple C++ code I am trying to compile using VS.NET 2003. (Below) Every time I try to compile I get numerous C2784 errors. If I simply comment out both of the chores.insert() lines,...
10
by: Arthur J. O'Dwyer | last post by:
I'm seeing a bug at the moment that I can't track down. It's part of a moderately large program, but here is a small program that exhibits the bug on gcc. (The program code follows at the bottom...
1
by: SUPER_SOCKO | last post by:
I am new to STL. I don't know how to access a multiset of a list. My code is the following: #include <list> #include <iostream> #include <set> using namespace std;
2
by: gacek | last post by:
I can't find a clear example showing me how to use multiset with a custom compare function. (like here: http://www.codeproject.com/vcpp/stl/setandmap.asp - I have no idea why they have the 'struct...
5
by: Dale Marchand | last post by:
I'm trying to use an object in two different multiset containers, each with it's own sort method. For the most frequently used, I overrode the operator< method in the class, and for the second I...
2
by: =?iso-8859-1?q?Jo=E3o_Correia?= | last post by:
class CScore { public: int L; int C; CScore(int l, int c) { L = l; C = c;
9
by: neil.johnston | last post by:
I have a cut down example program that uses multiset to order some data. The data arrives from various sources and has a time stamp, data with identical timestamps can arrive and due to fifo's and...
7
by: Mike Kent | last post by:
It's often useful for debugging to print something to stderr, and to route the error output to a file using '2>filename' on the command line. However, when I try that with a python script, all...
4
Digital Don
by: Digital Don | last post by:
Hi, I was looking at the MultiSet STL structure with some "less" keyword. I was told that we can use the "MultiSet" STL structure to automatically sort the content. Is it possible to store and...
3
by: orzeech | last post by:
Hi everyone! I have a following problem: #include<iostream> #include<set> using namespace std;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.