本文介绍了逻辑或表达式c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在C ++中使用逻辑OR运算符时遇到问题。问题出现,如果左侧是真的,则不评估右侧表达式。我有两个deque-s,我需要popLeft从他们一段时间,但如果我可以从第一个deque pop,我不会从第二个pop,因为没有评估,通过OR运算符。我如何克服这个问题。下面是一段代码:
I have a problem using the Logical OR operator in C++. The problem is coming that the right-side expression is not evaluated if the left-side is true. I have two deque-s and I need to popLeft from them with a while, but if I can pop from the first deque, I don't pop from the second because is not evaluated, by the OR operator. How can I overcome this problem. Here is the piece of code:
while( D.popLeft( k ) || E.popLeft( m ) )
{
if( k < m )
{
C.pushRight( k );
E.pushLeft( m );
}
else
{
C.pushRight( m );
D.pushLeft( k );
}
}
推荐答案
您要保留 ||
(而不是使用&&
),您可以分别评估: / p>
Assuming you want to keep your ||
(and not use a &&
), you can evaluate separatly:
bool canPopE = E.popLeft( m );
bool canPopD = D.popLeft( k );
bool canPop = canPopD || canPopE;
while( canPop )
{
if( k < m )
{
C.pushRight( k );
E.pushLeft( m );
}
else
{
C.pushRight( m );
D.pushLeft( k );
}
canPopE = E.popLeft( m );
canPopD = D.popLeft( k );
canPop = canPopD || canPopE;
}
这篇关于逻辑或表达式c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!