关于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:

· Strict: pred (X, X) is always false.

· Weak: If ! pred (X, Y) && !pred (Y, X), X==Y.

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

时间: 2024-10-11 13:17:47

关于strict weak order的相关文章

std::sort要求strict weak ordering

strict weak ordering简单地说就是小于语义,非小于等于语义,也就是说对于相等的或者异常的元素比较应当返回false 后果很严重,在google搜一下violating strict weak ordering make std::sort crash能看到很多种后果, 经测试,当待排序元素大于16个时使用std::sort,若传入的callable违反strict weak ordering,则可能死循环也可能越界访问. 待排序元素小于等于16个不会有问题是因为std::sor

c++ stl sort 自定义排序函数cmp要遵循 strict weak ordering

满足strict weak ordering的运算符能够表达其他所有的逻辑运算符(logical operator): <(a, b)  : (a < b) <=(a, b): !(b < a) ==(a, b): !(a < b) && !(b < a) !=(a, b) : (a < b) || (b < a) >(a, b)  : (b < a) >=(a, b): !(a < b) 引用自https://www

C++中 sort 函数的使用详解

STL主要包含容器,迭代器,算法三块内容,用户可以对容器进行一系列的操作,比如遍历和计算,而STL提供的迭代器和容器完美地提供了这样的接口.其中std::vector是最常用的容器之一,vector是一个模板类,定义在命名空间namespace下,使用vector需要在包含相关头文件.今天主要讲解对vector的排序的使用. 常见的排序算法有快速排序.冒泡排序.归并排序等.STL中sort函数的实现跟STL的版本有关,而往往sort函数是由多种排序算法混合而成的. 1. vector元素为内置数

Uva 642 - Word Amalgamation sort qsort

 Word Amalgamation  In millions of newspapers across the United States there is a word game called Jumble. The object of this game is to solve a riddle, but in order to find the letters that appear in the answer it is necessary to unscramble four wor

容器适配器之priority_queue

template <class T, class Container = vector<T>,                class Compare = less<typename Container::value_type> > class priority_queue; Priority queuePriority queues are a type of container adaptors, specifically designed such that i

STL之multimap

参见http://www.cplusplus.com/reference/map/multimap/ 多重映射multimap和map映射很相似,但是multimap允许重复的关键字,这使得multimap在某些情况下会更有用,比如说在电话簿中同一个人可以有多个电话号码 multimap中并没有像map那样提供重载的operator[],因此不能通过operator[]给元素赋值template < class Key,                                     /

STL make_heap push_heap pop_heap sort_heap

make_heap: default (1) template <class RandomAccessIterator> void make_heap (RandomAccessIterator first, RandomAccessIterator last); custom (2) template <class RandomAccessIterator, class Compare> void make_heap (RandomAccessIterator first, Ra

STL heap usage

简介 heap有查找时间复杂度O(1),查找.插入.删除时间复杂度为O(logN)的特性,STL中heap相关的操作如下: make_heap() push_heap() pop_heap() sort_heap() reverse() 本次着重介绍make_heap() ,根据其创出的堆有大小堆之分. 其函数原型如下: default (1) template <class RandomAccessIterator> void make_heap (RandomAccessIterator

STL之map

参见http://www.cplusplus.com/reference/map/map/ template < class Key,                                     // map::key_type                     class T,                                        // map::mapped_type                     class Compare = less<