三元运算符和自增运算符
Ternary operator and increment operator
如果不满足条件,这是复位计数器的有效/合理的方法吗?这是我能想到的最紧凑的方法。
int counter = 0;
int a,b;
// Do .. and assign a and b
counter = ((a<b) ? counter++ : 0);
您已经分配给counter
,所以不要再使用++
。
counter = condition ? (counter + 1) : 0;
由于没有排序点,counter = (condition ? counter++ : 0);
的行为未定义。(三元数没有排序,赋值也没有排序)。
形式与i = i++;
相似
相关文章:
- 三元运算符和 if constexpr
- 在 c++ 中三元运算符中不允许继续(关键字)吗?
- 三元运算符在返回语句中给出意外的结果
- Boost Spirit x3 条件(三元)运算符解析器
- 递归中三元运算符的奇怪行为
- 如何在三元条件运算符中添加换行符和连接? :在 C++ 中
- 错误:三元运算符无法在对象中正常工作"cout"
- 是否允许三元运算符在C++中计算两个操作数?
- 有条件地选择带有 decltype() 和三元运算符的类型
- 三元运算符在C++中的意外行为
- 有没有办法将 for 循环结果返回到像三元运算符这样的函数中?
- 变量值,在三元运算符之后
- 是否可以在C++中使用三元运算符在 if 语句中选择比较运算符?
- c++中的增量和三元运算符优先级
- C++中三元运算符的意外行为
- 用于返回开关的三元运算符
- 替换模板元编程中的三元运算符
- 三元运算符 '?:' 在 4.9.0 之前的 GCC 版本中推断出不正确的类型?
- 在 std::map 上使用三元运算符和 std::更大的附加参数
- C++中的三元条件运算符问题