sort函数中的参数之判定函数对象

  sort 函数在 algorithm 文件中被重载为两个函数。

 

函数 1:

template<class_RanIt> inline

voidsort(_RanIt _First, _RanIt _Last)

{        //order [_First, _Last), using operator<

_DEBUG_RANGE(_First,_Last);

_Sort(_CHECKED_BASE(_First),_CHECKED_BASE(_Last), _Last - _First);

}

默认是用"<"来判定顺序的。

 

函数 2:

template<class_RanIt,

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,用户自定义的判定函数对象。

 

函数1算是函数2的一个特例,所以这里只分析函数2即可。

sort函数最终是调用 如下函数来比较两个参数的大小的:

template<class_Pr, class _Ty1, class _Ty2> 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);

}

 

代码中标注的蓝色注释说明,判定函数_Pred必须是严格的弱序化(strict weak ordering),什么是严格的弱序化呢?参考Microsoft给的支持: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).

 

理解Strict,两个相同的参数比较,必须返回false。如果不返回false,通过源代码我们可以看到,程序将会抛出异常提示"invalid operator<"。在《Effective STL》中的第21条也提到,Item 21. Always have comparison functions return false for equal values.

理解Weak,如果!Pred(X, Y)!_Pred(X, Y)同时满足的话,即说明XY是相等的。相等默认不改变。

理解Ordering,先比较完X,YY,Z,再来比较X,Z

 

如果是比较结构体,即可以使用重载操作符<,>来完成,也可以直接自定义比较函数来完成。

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<ZONE_BLK>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).

维基百科对严格弱序列的介绍:
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 isasymmetric) 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 A, 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 (asymmetry).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 asymmetry 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).  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值