Quote Originally Posted by kmdv View Post
By comparing fields like this:

Code:
bool operator<(const Foo& lhs, const Foo& rhs)
{
    if (lhs.A != rhs.A)
    {
       return lhs.A < rhs.A;
    }
    if (lhs.B != rhs.B)
    {
       return lhs.B < rhs.B;
    }
    if (lhs.C != rhs.C)
    {
       return lhs.C < rhs.C;
    }
    return false; // lhs and rhs are equivalent.
}


It is needed for O(log N) operations. Map stores its elements in a tree.



You should take into account every field (every state visible to the user of your class) to avoid a situation where two values which look and feel like two different things are recognised as equivalent values.
This applies to every comparison operator.



You feed your map with two different values: A and B. If !(A < B) && !(B < A) then A == B. This makes the map think that it recieves two pairs with the same key.
Ahh...I see. So something like this then, right:

C++ code - 161 lines - codepad

(Yay, it works!)