std::map<K,D>
needs to be able to sort. By default is uses std::less<K>
, which for non-pointers uses <
1.
Using the rule that you demand the least you can from your users, it synthesizes "equivalence" from <
when it needs it (!(a<b) && !(b<a)
means a
and b
are equivalent, ie, neither is less than the other).
This makes it easier to write classes to use as key components for a map
, which seems like a good idea.
There are std
containers that use ==
such as std::unordered_map
, which uses std::hash
and ==
. Again, they are designed so that they require the least from their users -- you don't need full ordering for unordered_
containers, just equivalence and a good hash
.
As it happens, it is really easy to write a <
if you have access to <tuple>
.
struct Address {
long m_IPv4Address;
bool isTCP;
bool operator< (const Address& o) const {
return
std::tie( m_IPv4Address, isTCP )
< std::tie( o.m_IPv4Address, o.isTCP );
}
};
which uses std::tie
defined in <tuple>
to generate a proper <
for you. std::tie
takes a bunch of data, and generates a tuple
of references, which has a good <
already defined.
1 For pointers, it uses some comparison that is compatible with <
where <
behaviour is specified, and behaves well when <
does not. This only really matters on segmented memory model and other obscure architectures.