我不明白 Accelerated C++ 中的以下摘录:
开始于
Because || is left-associative, and because of the relative precedence of ||,== ,and -,
r == 0 || r == rows - 1 || c == 0 || c == cols - 1 means the same as it would if we were to place all of its subexpressions in parentheses:
((r == 0 || r == (rows - 1)) || c == 0) || c == (cols - 1)
一直到
Otherwise, it does something else, which we must now define.
我不明白这个。你会如何用你自己的话告诉我同样的事情?
最佳答案
如果您有一系列要评估的条件,假设“如果 x 为 1 或 y 为 2,则调用函数 foo()”,则执行第二个测试(y 为 2)没有意义,如果你已经知道 x 是 1。 ||运算符是这样工作的:
i( x == 1 || y == 2 ) {
foo();
}
如果 x == 1,表达式 y == 2 将不会被求值,因为它不是必需的。这称为短路评估,如果评估很昂贵,可以节省大量时间。
如果这不是您要问的问题,请更明确地说明您的问题。
关于c++ - 请从 Accelerated C++ 解释以下内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1149543/