Could anybody explain me what is the difference between overload ==
and <
?
For example, if I use a map:
map
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.)
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.
Because std::map<T, U>
has such behaviour, it uses std::less<T>
functor or your functor for many operations.
Behaviour can be different, but usual checks that lhs is less than rhs.
std::map
is an ordered container, therefore it needs an operator <
to define the order of elements.
A separate operator ==
is not needed because it is implemented in terms of operator <
, a==b
being equivalent to !a<b && !b<a
Probably because map is implemented as a balanced tree. Use unordered_map if you require a hash table.
The operator <
is needed, because the find operation bases on the inner implementation of map (on the tree). To be able to find in complexity better than linear, it can't have to compare with every element.
A good example of the similar algorithm is Binary Search. As you can see in sample pseudocode, it doesn't use the identity operator at all.