shorthand c++ if else statement(简写 C++ if else 语句)
问题描述
所以我很好奇是否有一个简短的声明:
So I'm just curious if there is a short hand statement to this:
if(number < 0 )
bigInt.sign = 0;
else
bigInt.sign = 1;
我看到了所有这些关于 if a < 的简短陈述.b 等等.
I see all these short hand statements for if a < b and such.
我不确定如何正确执行此操作,并希望对此提供一些意见.
I'm not sure on how to do it properly and would like some input on this.
谢谢!
其实我刚在你们回答之前就想通了.
I actually just figured it out right before you guys had answered.
最短的解决方案是 bigInt.sign = (number <0) ?0 : 1
推荐答案
是:
bigInt.sign = !(number < 0);
! 运算符的计算结果始终为 true 或 false.当转换为 int 时,它们分别变为 1 和 0.
The ! operator always evaluates to true or false. When converted to int, these become 1 and 0 respectively.
当然这相当于:
bigInt.sign = (number >= 0);
这里的括号是多余的,但为了清楚起见,我添加了它们.所有比较和关系运算符的计算结果为 true 或 false.
Here the parentheses are redundant but I add them for clarity. All of the comparison and relational operator evaluate to true or false.
这篇关于简写 C++ if else 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:简写 C++ if else 语句
基础教程推荐
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 初始化列表*参数*评估顺序 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- CString 到 char* 2021-01-01
