我可以在 C++ 中使用 not 运算符吗?关于 int 值?

发布于 2024-10-01 07:30:34 字数 303 浏览 0 评论 0原文

奇怪的问题,但有人向我展示了这个, 我想知道你能用 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;
}

Strange question, but someone showed me this,
I was wondering can you use the not ! operator for int in C++? (its strange to me).

#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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

扮仙女 2024-10-08 07:30:34

是的。对于整型,如果操作数为零,! 返回 true,否则返回 false

所以这里的 !b 仅仅意味着 b == 0


这是一种将值转换为 bool 的特殊情况。 !b 可以被视为 !((bool)b) 所以问题是 b 的“真实性”是什么。在 C++ 中,算术类型、指针类型和枚举都可以转换为 bool。当值为 0 或 null 时,结果为 false,否则为 true (C++ §4.1.2)。

当然,自定义类甚至可以重载 operator!operator<类型可以转换为 bool>允许其类使用 !b。例如,std::stream 已经重载了 operator!operator void* 来检查失败位,因此

while (std::cin >> x) {   // <-- conversion to bool needed here
  ...

可以使用类似的习惯用法。

(但是你的代码 !( a > b && b <= c) || a > c && !b 只是神秘的。)

Yes. For integral types, ! returns true if the operand is zero, and false otherwise.

So !b here just means 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).

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

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.)

冰雪梦之恋 2024-10-08 07:30:34

最初,C(C++ 的基础)中没有布尔类型。相反,值“true”被分配给任何非零值,而值“false”被分配给任何计算为零的值。这种行为在 C++ 中仍然存在。因此,对于 int x 来说,表达式 !x 表示“x not true”,即“x not非零”,即如果x为零则为真。

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.

贪恋 2024-10-08 07:30:34

可以,!b 相当于 (b == 0)

You can, !b is equivalent to (b == 0).

初心未许 2024-10-08 07:30:34

int 的测试对于非零值是 true,对于零值是 false,因此 not 仅对于零值是 true,对于非零值是 false。

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.

你是我的挚爱i 2024-10-08 07:30:34

内置 ! 运算符将其参数转换为 bool。该标准指定存在任何算术类型的转换(intchar、....floatdouble...) 到 bool。如果源值为 0,则结​​果为 true,否则为 false

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文