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)