更大的运算符“>”是否满足严格的弱排序?

Does greater operator ">" satisfy strict weak ordering?

定义:

Let < be a binary relation where a < b means "a is less than b".

Let > be a binary relation where a > b means "a is greater than b".

因此,我们假设 <> 具有我们通常在日常生活中使用的含义。虽然,在某些编程语言(例如 C++)中,我们可以重载它们以赋予它们不同的定义,但此后我们不考虑这些。


上下文

据我所知,严格弱排序的数学定义(例如Wikipedia), I think both < and > satify it. However, all examples I saw in many websites refer only to <. There is even a website表示

what they roughly mean is that a Strict Weak Ordering has to behave the way that "less than" behaves: if a is less than b then b is not less than a, if a is less than b and b is less than c then a is less than c, and so on.


此外,在 N4140(C++14 国际标准)中,严格的弱排序定义为

(§25.4-4) If we define equiv(a, b) as !comp(a, b) && !comp(b, a), then the requirements are that comp and equiv both be transitive relations

其中 comp 定义为

(§25.4-2) Compare is a function object type (20.9). The return value of the function call operation applied to an object of type Compare, when contextually converted to bool (Clause 4), yields true if the first argument of the call is less than the second, and false otherwise. Compare comp is used throughout for algorithms assuming an ordering relation.


问题:

“>”是否满足严格的弱排序?期待,但没有信心。

即使标准针对任意 Compare 函数引用 "less than",这也仅意味着 "less than" 在排序 的上下文中。

如果我通过比较函数 [](int a, int b) { return a > b; } 定义一个排序,那么如果一个元素的整数值更大,则该元素是 "less than" 此排序中的另一个元素。那是因为我创建的顺序是整数 倒序 的顺序。您不应该在排序中将 < 读作 "less than"。您应该将其读作 "comes before".

只要 x < y 是严格的弱排序,那么 x > y 也是严格的弱排序,只是顺序相反。

Does greater operator “>” satisfy strict weak ordering?

数学上的严格大于关系是一个严格的弱排序。

关于C++语言中的运算符:对于所有整数类型:是的。一般来说:不,但在大多数情况下是。同样适用于严格小于运算符。


至于令人困惑的引用,"is less than" 在该上下文中旨在传达这意味着排序操作的最终结果是一个非递减序列,即对象 "less" 或等于它们之后的对象。如果使用std::greater作为比较对象,则较大的值依次为"lesser"。

这可能会造成混淆,但并不是为了排除严格大于运算符。


what is the case where > doesn't satisfy strict weak ordering?

一些示例:

  • 不满足属性的重载运算符。
  • > 对不指向同一数组的指针的运算符具有未指定的结果。
  • > 不满足 IEEE-754 表示中浮点类型的非自反性要求,除非 NaN 被排除在域之外。