在對容器進行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 x, y, and z, if x < y and y < z then x < z (transitivity).
- For all x, y, and z, if x is incomparable with y, and y is incomparable with z, then x is incomparable with z (transitivity of equivalence).
這裡的<可以是根據排序需求而變換的任意的關係,并不僅僅局限於“<”:比如在stl中使用的比較函數(仿函數)。