Mad*_*den 1 c++ boolean-logic if-statement
可能是一个非常简单的问题,但我对哪些选项感兴趣.我有三个条件,每个条件应产生不同的输出
// special cases
if(!A && B)
return -1;
if(A && !B)
return 1;
if(!A && !B)
return 0;
// general case
return someOtherFunction(x, y);
Run Code Online (Sandbox Code Playgroud)
我可以归结为 -
if(!A) {
if(!B)
return 0;
return -1;
}
if(A && !B)
return 1;
return someOtherFunction(x, y);
Run Code Online (Sandbox Code Playgroud)
我可以进一步简化吗?这是用C++编写的,所以我只能使用特定于语言的运算符和函数(包括STL)等.
return (!A ? (!B ? 0 : -1) : (!B ? 1 : someOtherFunction(x, y)));
Run Code Online (Sandbox Code Playgroud)
这是使用嵌套的三元运算符.