布尔递归函数中的真/假优先级
我已经阅读了以前的帖子,并且学到了一些东西,但想验证一些循环是如何工作的。在阅读中,我是否正确理解“真”比“假”具有更高的优先级?例如:
/.../
return (true || false);
会返回“true”(无论顺序如何)?
如果我有一个布尔递归函数,它调用自身的 3 个变体...我所需要的只是一个版本返回 true,以便整个函数返回 true,对吗?下面的函数创建它的堆栈帧,然后返回调用创建另外 3 个堆栈帧并运行调用,然后如果一个返回 true,则整个函数返回 true,因为 true 优先于 false...这个假设正确吗?
即:
/* This function is taking a given weight and seeing if it can be offset by available
* weights. Depending on what weights are available, the weights can be directly opposed
* to "weight" (opposite side of scale) or added to... The recursive calls will either all
* return false, all return true, or a variation thereof. All that I can is that if one
* branch returns true, the overall function returns true...
*/
bool CanMeasure(int weight, std::vector<int> &availableWeights, int index = 0)
{
/.../
// the below can return all true, all false, or some variation thereof...
return (CanMeasure(weight + availableWeights[index], availableWeights, index + 1) ||
CanMeasure(weight - availableWeights[index], availableWeights, index + 1) ||
CanMeasure(weight, availableWeights, index + 1));
}
谢谢大家!
I've done some reading of previous posts and I've learned some things but want to verify how some loops are working. In reading, am I right in understanding that "true" has higher precedence than "false"? For instance:
/.../
return (true || false);
will return "true" (regardless of order)?
If I have a boolean recursive function that calls 3 variations of itself...all I need is for one version to return true for the whole funct to return true, correct? The function below creates it's stack frame, then the return call creates 3 more stack frames and runs through the calls, then if one returns true the whole funct returns true because true has precedence over false... Is this assumption correct?
Ie:
/* This function is taking a given weight and seeing if it can be offset by available
* weights. Depending on what weights are available, the weights can be directly opposed
* to "weight" (opposite side of scale) or added to... The recursive calls will either all
* return false, all return true, or a variation thereof. All that I can is that if one
* branch returns true, the overall function returns true...
*/
bool CanMeasure(int weight, std::vector<int> &availableWeights, int index = 0)
{
/.../
// the below can return all true, all false, or some variation thereof...
return (CanMeasure(weight + availableWeights[index], availableWeights, index + 1) ||
CanMeasure(weight - availableWeights[index], availableWeights, index + 1) ||
CanMeasure(weight, availableWeights, index + 1));
}
Thanks guys!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
true
和false
是值,而不是运算符 - 因此它们没有优先级。但是,如果结果已知,
&&
和||
运算符确实可以简化计算过程。因此,如果左侧表达式产生true
并且您应用||
,则不会计算右侧表达式;这同样适用于false
和&&
。true
andfalse
are values, not operators - as such they have no precedence.However, the
&&
and||
operators do shortcut the evaluation if the result is known; so if the lefthand expression yieldstrue
and you apply||
, the righthand expression will not be evaluated; the same applies tofalse
and&&
.return (true || false);
不评估 false 位的原因是短路布尔评估。请参阅 C++ 中的短路评估是否有保证和 Java 中一样吗?。与 && 类似,(false && true)
不会评估“true”。The reason why
return (true || false);
doesn't evaluate the false bit is because of short circuit boolean evaluation. See Is Short Circuit Evaluation guaranteed In C++ as it is in Java?. Similary with &&,(false && true)
won't evaluate 'true'.是的(无论顺序如何,它都会返回
true
)。or
中的条件从左到右进行评估,当遇到第一个true
时,整个条件返回true
。在您的示例中:
并非所有条件都需要评估。如果第一个函数的计算结果为
true
,则其他函数将不会执行,并且该函数将仅返回true
。这就是所谓的短路。
让我们看一下一些反汇编代码:
在这个例子中,
foo()
和goo()
都是返回 bool 的函数。该指令
告诉运行时如果
foo()
计算结果为 true 则跳出条件。此代码未经过优化,因此它不是优化功能。
Yes (it will return
true
regardless of order). The conditions in theor
are evaluated from left to right, and when the firsttrue
is stumbled upon, the whole condition returnstrue
.In your example:
not all conditions are necesarily evaluated. If the first one evaluates to
true
, the others will not execute, and the function will just returntrue
.It's called short-circuiting.
Let's take a look at some dissasembled code:
In this example,
foo()
andgoo()
are both functions returning bool.The instruction
tells the runtime to jump out of the conditional if
foo()
evaluated to true.This code is not optimized, so it's not an optimization feature.
|| 的规则运算:
如果其中一个操作数为真,则无论其位置如何,结果均为真。
即使你有类似
false || 的东西假||假...假|| true
结果将为 true。The rules for || operation:
If one of the operands are true, no matter of it's position, the result will be true.
Even if you have something like
false || false || false ... false || true
the result will be true.是的,一个返回值(true)足以确定结果。
这是通过短路评估来定义的。
Yes, one return value, which is true does suffice to determine the result.
This is defined by short circuit evaluation.
http://en.wikipedia.org/wiki/Short-circuit_evaluation
我不认为这个问题是关于短路评估的。而“true 的优先级高于 false”是没有意义的。 True 是一个值,而不是一个操作。
true || 的原因false
返回true
只是逻辑,与编程无关。逻辑运算 AND 和 OR 不关心其输入的顺序。他们只关心“有多少是真的”。特别是,如果两个都为真,则AND返回真,而如果至少一个为真,则OR返回真。短路评估是另一个主题(尽管它可能对您避免某些函数调用仍然有用)。
I don't think this question is about short circuit evaluation. And "true has higher precedence than false" does not make sense. True is a value, not an operation.
The reason why
true || false
returnstrue
is just logic, and has nothing to do here with programming. The logic operation AND and OR don't care about the order of their inputs. They just care about "how many are true". In particular, AND returns true if both are true, while OR returns true if at least one is true.The short circuit evaluation is another topic (which might still be useful to you to avoid some function call though).