MCP*_*MCP 1 c++ recursion boolean
我已经阅读了之前的帖子并且我已经学到了一些东西,但是想要验证一些循环是如何工作的.在阅读中,我是否正确理解"真实"的优先级高于"虚假"?例如:
/.../
return (true || false);
Run Code Online (Sandbox Code Playgroud)
将返回"true"(无论顺序)?
如果我有一个布尔递归函数调用它自己的3个变量...我需要的是一个版本返回true以使整个函数返回true,对吗?下面的函数创建它的堆栈帧,然后返回调用创建3个堆栈帧并运行调用,然后如果一个返回true,整个funct返回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));
}
Run Code Online (Sandbox Code Playgroud)
多谢你们!
true并且false是价值,而不是运营商 - 因此他们没有优先权.
然而,&&和||运营商做快捷评估,如果结果是已知的; 因此,如果左手表达式产生true并且您应用||,则不会评估右手表达式; 这同样适用于false和&&.