The comparison function of sort in c++<algorithm>

Source: Internet
Author: User

Transferred from: http://www.wl566.com/biancheng/98907.html

The comparison function of the sort in c++<algorithm>, the need for friends can refer to the next.

To define a sort function:

Method 1: Declare an external comparison function

BOOL Less (constconst student& s2) {    return// from small to large } Std::sort (Sutvector.begin (), Stuvector.end (), less);

Note: The comparison function must be written outside the class (global zone) or declared as a static function

When comp is a member function of a class, the default has a this pointer, which is different from the sort function type that the sort function needs to use.

Otherwise, an error will occur

Method 2: Overloading the class's comparison operators

BOOL operator< (constconst student& s2) {    return//  From small to large sort }std::sort (Sutvector.begin (), Stuvector.end ());

Method 3: Declare the comparison class

struct less{    booloperator() (constconst student& s2)    {         return// from small to large sort     }};std::sort (Sutvector.begin (), Stuvector.end (), Less ());

Comparison function notation for sort in c++<algorithm> (RPM)

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.