关于std::sort中的比较函数使用时的严格弱排序(strict weak order)

来源:互联网 发布:网络直播平台搭建 编辑:程序博客网 时间:2024/05/01 09:47

在對容器進行std::sort算法排序時,算法所使用的仿函數或者普通函數必須保證符合嚴格若排序(strict weak order),否則,算法會報錯,從而終止程序。

引用wikipedia中的說明:

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).(即在stl的算法函數中若兩個相同的容器對象做比較,返回false)
  • For all x ≠ y, if x < y then it is not the case that y < x (asymmetric).
  • For all xy, and z, if x < y and y < z then x < z (transitivity).
  • For all xy, and z, if x is incomparable with y, and y is incomparable with z, then x is incomparable with z (transitivity of equivalence).

這裡的<可以是根據排序需求而變換的任意的關係,并不僅僅局限於“<”:比如在stl中使用的比較函數(仿函數)。

原创粉丝点击