简体   繁体   English

Stroustrup 比较,勘误表?

[英]Stroustrup on comparisons, errata?

In Stroustrup C++ 4th Ed Page 891, where comparisons properties are described.在 Stroustrup C++ 第 4 版第 891 页中,其中描述了比较属性。 He explains that the function cmp can be represented by less than < for a strict weak ordering.他解释说,对于严格的弱排序,function cmp可以用小于<表示。 I'm confused by his explanation of "Transitivity of equivalence" as follows;我对他对“等价的传递性”的解释感到困惑,如下所示;

Transitivity of equivalence: Define equiv(x,y) to be,(cmp(x,y)||cmp(yx)), If equiv(x,y) and equiv(y,z), then equiv(xz), The last rule is the one that allows us to define equality (x==y) as,(cmp(xy)||cmp(y,x)) if we need ==.等价的传递性:定义equiv(x,y)为,(cmp(x,y)||cmp(yx)),如果equiv(x,y)和equiv(y,z),则equiv(xz),最后一条规则允许我们在需要 == 时将等式 (x==y) 定义为 (cmp(xy)||cmp(y,x))。

Should this instead be defined as follows?这应该定义如下吗?

cmp is <= and equiv(x,y) = (cmp(x,y) && cmp(y,x)) cmp<=equiv(x,y) = (cmp(x,y) && cmp(y,x))

Appreciate your guidance.感谢您的指导。

This is not errata.这不是勘误表。

equiv(x,y) := !(cmp(x,y)||cmp(y,x))
x := x
y := x

substituting in:
!((x < x) || (x < x))
!((false) || (false))
!(false)
true

Can this instead be defined as follows?可以改为定义如下吗?

cmp is <= and equiv(x,y) = (cmp(x,y) && cmp(y,x)) cmp<=equiv(x,y) = (cmp(x,y) && cmp(y,x))

Yes, that also gives you consistent definitions.是的,这也为您提供了一致的定义。

Should this instead be defined as follows?应该定义如下吗?

It isn't better than the definitions we use, so I'd suggest no , mostly because there's loads of existing code written for the current definition.它并不比我们使用的定义好,所以我建议不要,主要是因为有大量为当前定义编写的现有代码。

Instead of the current definition of Compare而不是当前的Compare定义

Compare is a set of requirements expected by some of the standard library facilities from the user-provided function object types.比较是一些标准库设施从用户提供的 function object 类型中预期的一组要求。

The return value of the function call operation applied to an object of a type satisfying Compare , when contextually converted to bool , yields true if the first argument of the call appears before the second in the strict weak ordering relation induced by this type, and false otherwise.应用于 object 类型满足Compare的 function 调用操作的返回值,当上下文转换为bool时,如果调用的第一个参数出现在由此类型引起的严格弱排序关系中的第二个之前,则返回true ,否则返回false除此以外。

  • For all a , cmp(a,a)==false对于所有acmp(a,a)==false
  • If cmp(a,b)==true then cmp(b,a)==false如果cmp(a,b)==true那么cmp(b,a)==false
  • If cmp(a,b)==true and cmp(b,c)==true then cmp(a,c)==true如果cmp(a,b)==truecmp(b,c)==true那么cmp(a,c)==true

It would instead be相反,它会是

The return value of the function call operation applied to an object of a type satisfying Compare , when contextually converted to bool , yields false if the first argument of the call appears after the second in the strict weak ordering relation induced by this type, and true otherwise. function 调用操作应用于满足Compare的类型的false的返回值,当上下文转换为bool时,如果调用的第一个参数出现在由该类型引起的严格弱排序关系中的第二个参数之后,则返回 false,而返回true除此以外。

  • For all a , cmp(a,a)==true对于所有acmp(a,a)==true
  • If cmp(a,b)==false then cmp(b,a)==true如果cmp(a,b)==false那么cmp(b,a)==true
  • If cmp(a,b)==true and cmp(b,c)==true then cmp(a,c)==true如果cmp(a,b)==truecmp(b,c)==true那么cmp(a,c)==true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM