Mar*_*aux 6 c++ java boolean toggle
有一个简短的方法来切换布尔值?
使用整数,我们可以执行以下操作:
int i = 4;
i *= 4; // equals 16
/* Which is equivalent to */
i = i * 4;
Run Code Online (Sandbox Code Playgroud)
那么布尔*=运算器也有一些东西(比如整数运算符)?
在C++中:
bool booleanWithAVeryLongName = true;
booleanWithAVeryLongName = !booleanWithAVeryLongName;
// Can it shorter?
booleanWithAVeryLongName !=; // Or something?
Run Code Online (Sandbox Code Playgroud)
在Java中:
boolean booleanWithAVeryLongName = true;
booleanWithAVeryLongName = !booleanWithAVeryLongName;
// Can it shorter?
booleanWithAVeryLongName !=; // Or something?
Run Code Online (Sandbox Code Playgroud)
Pet*_*hev 25
没有这样的运算符,但这有点短: booleanWithAVeryLongName ^= true;
一个简单的函数(在C++中):
void toggle (bool& value) {value = !value;}
Run Code Online (Sandbox Code Playgroud)
然后你使用它像:
bool booleanWithAVeryLongName = true;
toggle(booleanWithAVeryLongName);
Run Code Online (Sandbox Code Playgroud)