I believe that the problem here is that your method of comparing two MY_orderID_t
's is not a strict weak order, the type of ordering relation required by the C++ STL. To be a strict weak order, your less-than operator must have the following four properties:
- Irreflexivity: x < x is always false.
- Antisymmetry: If x < y, then y < x is always false.
- Transitivity: If x < y and y < z, then x < z is always true.
- Transitivity of Equivalence: If x and y are incomparable and y and z are incomparable, then x and z are incomparable.
Right now, your ordering doesn't obey properties (2) or (3).
*First, (2) is violated by the following:
(0, 4) < (2, 2)
(2, 2) < (0, 4)
*Second, (3) is violated, because
(0, 1) < (2, 0) < (-1, 1)
// but
(0, 1) < (-1, 1) // Fail
To fix this, instead of using the comparison you currently have, instead use a lexicographical comparison like this one:
return std::lexicographical_compare(k1.orderID.begin(), k1.orderID.end(),
k2.orderID.begin(), k2.orderID.end());
This comparison is a strict weak ordering and is what's used by all the STL containers by default. Switching to this comparison obeys properties (1) - (4) and should cause everything to work correctly.
Hope this helps!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…