The sort function is overloaded as two functions in the algorithm file.
Function 1:
Template Inline
Voidsort (_ RanIt _ First, _ RanIt _ Last)
{// Order [_ First, _ Last), using operator <
_ DEBUG_RANGE (_ First, _ Last );
_ Sort (_ CHECKED_BASE (_ First), _ CHECKED_BASE (_ Last), _ Last-_ First );
}
By default, "<" is used to determine the order.
Function 2:
Template
Class_Pr> inline
Voidsort (_ RanIt _ First, _ RanIt _ Last, _ Pr _ Pred)
{// Order [_ First, _ Last), using _ Pred
_ DEBUG_RANGE (_ First, _ Last );
_ DEBUG_POINTER (_ Pred );
_ Sort (_ CHECKED_BASE (_ First), _ CHECKED_BASE (_ Last), _ Last-_ First, _ Pred );
}
_ Pred: User-Defined judgment function object.
Function 1 is a special case of function 2. Therefore, only function 2 can be analyzed here.
The sort function finally calls the following function to compare the size of two parameters:
Template Inline
Bool _ CLRCALL_OR_CDECL _ Debug_lt_pred (_ Pr _ Pred, _ Ty1 & _ Left, _ Ty2 & _ Right,
Constwchar_t * _ Where, unsigned int _ Line)
{// Test if _ Pred (_ Left, _ Right) and _ Pred is strict weakordering
If (! _ Pred (_ Left, _ Right ))
Return (false );
Elseif (_ Pred (_ Right, _ Left ))
_ DEBUG_ERROR2 (, _ Where, _ Line );
Return (true );
}
The blue annotation marked in the Code determines that the function _ Pred must be strict weak ordering. What is strict weak ordering? Refer to Microsoft for support: http://support.microsoft.com/kb/949171>
The STL algorithmsfor stable_sort () and sort () require the binary predicate to be strict weakordering.
For example:
· Strict: pred (X, X) is always false.
· Weak: If! Pred (X, Y )&&! Pred (Y, X), X = Y.
· Ordering: Ifpred (X, Y) & pred (Y, Z), then pred (X, Z ).
To understand Strict, compare two identical parameters and return false. If false is not returned, we can see from the source code that the program will throw an exception prompt "invalid operator <". Article 21st in objective STL also mentions that Item 21. Always have comparison functions return false for equal values.
Understand Weak. If! Pred (X, Y) and! If both _ Pred (X, Y) is satisfied, it means that X and Y are equal. EQUAL is not changed by default.
To understand Ordering, compare X, Y, Y, Z, and then X and Z.
If it is a comparison struct, you can use the overload operator <,> to complete the comparison, or you can directly customize the comparison function.
Typedef structST_ZONE_BLK
{
ST_ZONE_BLK (int a, intb): nFirst (a), nSecond (B ){}
Int nFirst;
Int nSecond;
Bool operator <(const ST_ZONE_BLK & _ other) const
{
Return nFirst> _ other. nFirst;
}
} ZONE_BLK;
Bool MySort (constST_ZONE_BLK & _ first, const ST_ZONE_BLK & _ second)
{
Return _ first. nFirst> _ second. nFirst;
}
Main ()
{
// Test
Vector VZoneBlk;
VZoneBlk. push_back (ZONE_BLK (1, 2 ));
VZoneBlk. push_back (ZONE_BLK (3, 2 ));
VZoneBlk. push_back (ZONE_BLK (1, 3 ));
VZoneBlk. push_back (ZONE_BLK (4, 2 ));
Sort (vZoneBlk. begin (), vZoneBlk. end (), MySort );
Sort (vZoneBlk. begin (), vZoneBlk. end ());
}
P.S.
MSDN: Several algorithms make use of a predicate that must impose a strict weak ordering on pairs of elements from a sequence. for the predicate pr (X, Y): Strict means that pr (X, X) is false. weak means that X and Y have an equivalent ordering if! Pr (X, Y )&&! Pr (Y, X) (X = Y does not need to be defined ). ordering means that pr (X, Y) & pr (Y, Z) implies pr (X, Z ).
Wikipedia introduces strict weak sequences: A strict weak ordering is a binary relation <on a set S that is a strict partial order (a transitive relation that is irreflexive, or equivalently, [5] that isasypolicric) in which the relation "neither a <B nor B <a" is transitive. [1] The equivalence classes of this "incomparability relation" partition the elements of S, and are totally ordered by <. conversely, any total order on a partition of S gives rise to a strict weak ordering in which x <y if and only if there exists sets A and B in the partition with x in, y in B, and A <B in the total order. as a non-example, consider the partial order in the set {a, B, c} defined by the relationship B <c. the pairs a, B and a, c are incomparable but B and c are related, so incomparability does not form an equivalence relation and this example is not a strict weak ordering. A strict weak ordering has the following properties. for all x and y in S, For all x, it is not the case that x <x (irreflexivity ). for all x, y, if x <y then it is not the case that y <x (asypolicry ). for all x, y, and z, if x <y and y <z then x <z (transitivity ). for all x, y, and z, if x is incomparable with y, and y is incomparable with z, then x is incomparable with z (transitivity of incomparability ). this list of properties is somewhat redundant, as asypolicry follows readily from irreflexivity and transitivity. transitivity of incomparability (together with transitivity) can also be stated in the following forms: If x <y, then for all z, either x <z or z <y or both. or: If x is incomparable with y, then for all z =x, z =y, either (x <z and y <z) or (z <x and z <y) or (z is incomparable with x and z is incomparable with y ).