我可以在 C++ 中对 int 值使用 not 运算符吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4123550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I use the not operator in C++ on int values?
提问by 001
Strange question, but someone showed me this, I was wondering can you use the not ! operator for int in C++? (its strange to me).
奇怪的问题,但有人给我看了这个,我想知道你能不能使用 not !C++中int的运算符?(这对我来说很奇怪)。
#include <iostream>
using namespace std;
int main()
{
int a=5, b=4, c=4, d;
d = !( a > b && b <= c) || a > c && !b;
cout << d;
system ("pause");
return 0;
}
回答by kennytm
Yes. For integral types, !
returns true
if the operand is zero, and false
otherwise.
是的。对于整数类型,如果操作数为零则!
返回true
,false
否则返回。
So !b
here just means b == 0
.
所以!b
这里只是意味着b == 0
.
This is a particular case where a value is converted to a bool
. The !b
can be viewed as !((bool)b)
so the question is what is the "truthness" of b
. In C++, arithmetic types, pointer types and enum can be converted to bool
. When the value is 0 or null, the result is false
, otherwise it is true
(C++ §4.1.2).
这是将值转换为 的特殊情况bool
。该!b
可被视为!((bool)b)
这样的问题是什么的“truthness” b
。在 C++ 中,算术类型、指针类型和枚举可以转换为bool
. 当值为 0 或 null 时,结果为false
,否则为true
(C++ §4.1.2)。
Of course custom classes can even overload the operator!
or operator
<types can be convert to bool> to allow the !b
for their classes. For instance, std::stream
has overloaded the operator!
and operator void*
for checking the failbit, so that idioms like
当然,自定义类甚至可以重载operator!
或operator
<类型可以转换为 bool> 以允许!b
它们的类。例如,std::stream
重载了operator!
andoperator void*
来检查故障位,所以像这样的习语
while (std::cin >> x) { // <-- conversion to bool needed here
...
can be used.
可以使用。
(But your code !( a > b && b <= c) || a > c && !b
is just cryptic.)
(但你的代码!( a > b && b <= c) || a > c && !b
很神秘。)
回答by Amnon
Originally, in C (on which C++ is based) there was no Boolean type. Instead, the value "true" was assigned to any non-zero value and the value "false" was assigned to anything which evaluates to zero. This behavior still exists in C++. So for an int x
, the expressions !x
means "x
not true", which is "x
not non-zero", i.e. it's true if x
is zero.
最初,在 C(C++ 所基于的)中没有布尔类型。相反,值“真”被分配给任何非零值,值“假”被分配给任何评估为零的值。这种行为在 C++ 中仍然存在。因此,对于 an int x
,表达式的!x
意思是“x
不为真”,即“x
非非零”,即如果x
为零则为真。
回答by vitaut
You can, !b
is equivalent to (b == 0)
.
可以,!b
相当于(b == 0)
。
回答by Armen Tsirunyan
The build-in !
operator converts its argument to bool
. The standard specifies that there exists a conversion from any arithmetic type(int
, char
,.... float
, double
...) to bool. If the source value is 0 the result is true
, otherwise it is false
内置!
运算符将其参数转换为bool
. 该标准指定存在从任何算术类型( int
, char
,.... float
, double
...) 到 bool 的转换。如果源值为 0,则结果为true
,否则为false
回答by Puppy
The test for int is true for non-zero values and false for zero values, so not is just true for zero values and false for non-zero values.
int 的测试对于非零值是真的,对于零值是假的,所以不仅零值是真的,非零值是假的。