关于strict weak order

template<class _Pr, class _Ty1, class _Ty2> inline
bool _Debug_lt_pred(_Pr _Pred,
const _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);
else if (_Pred(_Right, _Left))
_DEBUG_ERROR2("invalid operator<", _File, _Line);
return (true);
}

 

STL的所有有序容器 模板的 比较函数的定义,

都必须符合stict weak ordering的二元关系

既能比较出不等,也能比较出大小.

否则在key的比较的时候,执行到这里一定会在else if的分支报错

=========以下是STL文档的原话:

http://www.cplusplus.com/reference/map/map/

CompareA binary predicate that takes two element keys as arguments and returns a bool. The expression comp(a,b), where comp is an object of this type and a and b are key values, shall return true if a is considered to go before b in the strict weak ordering the function defines.
The map object uses this expression to determine both the order the elements follow in the container and whether two element keys are equivalent (by comparing them reflexively: they are equivalent if !comp(a,b) && !comp(b,a)). No two elements in a map container can have equivalent keys.
This can be a function pointer or a function object (see constructor for an example). This defaults to less<T>, which returns the same as applying the less-than operator (a<b).
Aliased as member type map::key_compare.

 

MSDN上关于sort的QA列表中对STL的解读

The STL algorithms for stable_sort ( ) and sort() require the binary predicate to be strict weak ordering.

For example: 

· Strictpred (X, X) is always false.

· WeakIf ! pred (X, Y) && !pred (Y, X), X==Y.

· Ordering: If pred (X, Y) && pred (Y, Z), then pred (X, Z). 

转载于:https://www.cnblogs.com/dfdqzp/p/7552507.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值