When you call STL sort, the error message is invalid & lt;

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.