码迷,mamicode.com
首页 > 其他好文 > 详细

关于strict weak order

时间:2017-09-19 16:45:18      阅读:333      评论:0      收藏:0      [点我收藏+]

标签:sid   UI   大小   span   rmi   apply   mic   failure   ble   

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

关于strict weak order

标签:sid   UI   大小   span   rmi   apply   mic   failure   ble   

原文地址:http://www.cnblogs.com/dfdqzp/p/7552507.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!