牛骨文教育服务平台(让学习变的简单)
博文笔记

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

创建时间:2012-09-09 投稿人: 浏览次数:2272

在對容器進行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中使用的比較函數(仿函數)。

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。