Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ overloading operators difference between == and < [duplicate]

Could anybody explain me what is the difference between overload == and <?

For example, if I use a map:

map<Type, int> a();

friend bool operator<(const Type& lhs, const Type& rhs);

friend bool operator==(const Type& lhs, const Type& rhs);

And I implement both operators, when I call:

a.find(value);

The operator function of == is being called? I think not. I debugged and saw that < is called, but why? What should be the behavior of the operator function of <?

I come from Java where the method equals is called and is pretty simple to understand the behavior.

Thanks.

like image 405
thiagoh Avatar asked Dec 19 '25 17:12

thiagoh


2 Answers

operator== overloads the == operator (and no other); operator< overloads the < operator (and no other).

std::map is defined to use std::less (and only std::less) by default, and std::less is defined to use < by default. In general, however, I would recommend not overloading operator< unless ordered comparison makes sense for your class, in which case, you should overload all six comparison operators, in a coherent fashion. Otherwise, you can specify a comparison functional type as an additional template argument to std::map; the comparison functional object should define a strict weak ordering relationship. If the type is designed to be used as a key, but the ordering is still purely arbitrary, you might specialize std::less.

As for Java, without operator overloading, it obviously can't use <; by default, SortedMap (the Java equivalent to std::map) requires the keys to be Comparable, however, which in turn requires a compare function, which returns a value <, == or > 0, depending on whether this is <, == or > than other. I'll admit that I find this a little bit more logical, but the difference is very, very small. (The rationale behind the C++ decision is that build-in types like int or double can be used as keys. In Java, you'ld have to box them.)

like image 83
James Kanze Avatar answered Dec 21 '25 10:12

James Kanze


The requirements that the C++ standard places on std::map mean that it is implemented as a self-balancing binary search tree. This means that some kind of ordering comparison between elements must be available. In the case of std::map, the requirement is a strict weak ordering, and the default is a less-than comparison. This is all that is required to arrange elements in a binary tree, and the equality condition is met when one element (call it A) is not less than another one (call it B), and the converse is also true, i.e. B is not less than A. An equality comparison could have been used, but this would open some scope for inconsistency. If you look at hash tables, such as std::unordered_map, you will find that an equality comparison is indeed required, although this is only to resolve collisions.

like image 45
juanchopanza Avatar answered Dec 21 '25 10:12

juanchopanza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!