本文介绍了双对比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能做到这一点的C ++?

 如果(4℃,5℃; 6)
 COUT<<有效<< ENDL;

即双比较呢?因为我知道我可以

 布尔一个;
一个= 1 + 2&3+ 4℃; 5→6; //等


解决方案

是的,你可以做到这一点,但它不会是你所期望的。它解析为

  IF((4℃; 5)6;)

这将产生

 如果(1 6;)

由于 4℃; 5 计算结果为真正这是晋升为 1 ,这将产生很明显,真实的。

您需要

  IF((4℃; 5)及及(5℃; 6))

此外,是的,你可以做

  A = 1 + 2'3 + 4℃; 5> 6;

但也被分析为

  A =((1 + 2)≤((3 + 4)小于5))> 6;

这将计算为,因为(1 + 2)≤((3 + 4)小于5)产生一个布尔值,它始终是小于6。

Can I do this in C++?

if (4<5<6)
 cout<<"valid"<<endl;

i.e a double comparison? Since I know that I can

bool a;
a = 1+2<3+4<5>6;//etc
解决方案

Yes, you can do it, but it won't be what you expect. It's parsed as

if ( (4<5) < 6 )

which yields

if ( 1 < 6 ) 

because 4<5 evaluates to true which is promoted to 1, which yields, obviously, true.

You'll need

if ( (4<5) && (5<6) )

Also, yes, you can do

a = 1+2<3+4<5>6;

but that as well is parsed as

a = ((1+2)<((3+4)<5))>6;

which will evaluate to false since (1+2)<((3+4)<5) yields a boolean, which is always smaller than 6.

这篇关于双对比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 19:33