Confused about stl's set

http://www.cplusplus.com/reference/stl/set/
Reading in the reference.

Compare: Comparison class: A class that takes two arguments of the same type as the container elements and returns a bool.

My lecturer told a friend of mine, who told me set's Compare class could return an int
+1 if a < b
0 if a = b
-1 if a> b
(or maybe it was the other way around, changing , -1 on the top, and 1 on the bottom)

However, I can't find any help on this subject. That leads me to believe this information could be false. Any help?
Last edited on
Compare class could return an int.

That's true to this point since int is convertible to bool. Reference is always right, professors not, and your friend might misunderstood something. Java (from languages which I know of) uses this int method thing in compareTo() method.
For std::set's compare, it expects it to return a bool whether the first is > then second (or something similar). If you return an int, you could return it like that, but if you convert it to a bool, then you would get the wrong information, since 0 = false and non-zero = true, you would get true if they are not equal, and false if they are, creating a possibly weird sorting method.
Last edited on
Thanks a lot for the help, ROmai and firedraco.

still, i wonder how in the world is set going to know when two elements are equal, only having operator < overloaded . Does it compares attribute to attribute? What if the attributes are pointers to memory?(i have the intuition that i should overload operator = , in that case).
Last edited on
It doesn't care whether they are equal (since std::set can only have unique elements).
how in the world is set going to know when two elements are equal, only having operator < overloaded


!(a < b) && !(b < a)
That was awesome, Abramus!
Thanks a lot, everyone. I'm marking this as solved.
how in the world is set going to know when two elements are equal, only having operator < overloaded


It doesn't! Set checks for equivalence, not equality and you need to understand the difference. For the case of a std::set<int> it makes no difference. In that case equivalence and equality will mean the same thing. For a set that contains user defined types the difference can be critical. That is a subject worth reading up on when you can make the time.
Topic archived. No new replies allowed.