When I call the STL sort function today, I keep saying "invalid" in the result. I haven't found any related answers for a long time on the Internet. After a long time, I finally figured out why .. For example, note that the following comparison function ComparePoint returns true when the two elements to be compared are equal:
[Cpp]
Class TrajPoint
{
Public:
Double distance;
Int edgeId;
};
Bool ComparePoint (TrajPoint a, TrajPoint B)
{
If (a. distance <B. distance)
Return true;
If (a. distance = B. distance)
Return true;
Return false;
}
Int main ()
{
...........
Sort (vec. begin (), vec. end (), ComparePoint );
............
}
Originally vs2008 and vs2010 are both strictly compared. If the two elements are equal, false must be returned. You can see the source code of STL:
[Cpp]
Template <class _ Pr, class _ Ty1, class _ Ty2> inline
Bool _ Debug_lt_pred (_ Pr _ Pred,
_ Ty1 & _ Left, _ Ty2 & _ Right,
_ Dbfile_t _ File, _ Dbline_t _ Line)
{// Test if _ Pred (_ Left, _ Right) and _ Pred is strict weak ordering
If (! _ Pred (_ Left, _ Right ))
Return (false );
<Strong> else if (_ Pred (_ Right, _ Left ))
_ DEBUG_ERROR2 ("invalid operator <", _ File, _ Line); </strong>
Return (true );
}
If both left and right are the same, STL will report an error .. Therefore, you can only modify your own code. When the values are equal, false is returned. Solve the problem. Www.2cto.com
[Cpp]
Bool ComparePoint (TrajPoint a, TrajPoint B)
{
If (a. distance <B. distance)
Return true;
Return false;
}