There is a "invalid operator<" problem when using sort in the afternoon, for specific information see
And then I'm quite puzzled, because this is usually the case with the default comparison operator, which is:
Template <class randomaccessiterator> void sort (randomaccessiterator first, Randomaccessiterator last);
But you clearly use the second function of the overloaded function, namely:
Template <class compare> void sort (randomaccessiterator first, Randomaccessiterator last, Compare comp);
Later, by querying the online data, I found that the cause of the problem is that VC for the comparison function will be strict weak ordering confirmation. (This has just been a bit of a surprise)
Direct excerpt from the official website explains:
Results:
It works fine in visbual C + + 2002 and Visual C + + 2003.
It throws and assertion failed error, "Expression:invalid operator <" in Visual C + + 2005 and Visual C + +/2008.
Cause:
The STL Algorithms for Stable_sort () and sort () require the binary predicate to be strict weak ordering.
For example:
· Strict:pred (x, x) is always false.
· Weak:if!pred (x, y) &&!pred (y, x), x==y.
· Ordering:if pred (x, y) && pred (Y, z), then pred (x, z).
Compare your own code and find that the problem is pred (x, x) = True, then the solution is generated (<= replaced by <).
Finally, I enclose the comparison function in my initial code.
// compare weights for two edges BOOL Const Const Edge & E2) { return e1.weight <= e2.weight;}
The Visual Studio sort function appears "Invalid operator<" cause analysis