c++的三元操作符形式:

//条件表达式 ? 表达式1 : 表达式2;

语义:如果“条件表达式”为true,则整个表达式的值就是表达式1,忽略表达式2;如果“条件表达式”为false,则整个表达式的值就是表达式2,等价于if/else语句。

1 if (条件表达式)
2    result = 表达式1;
3 else
4    result = 表达式2;

实例1:

1 int result;
2 int first=10;
3 int second=20;
4 result=first>second?0:1;
5 
6 //执行结果:如果first>second  result=0,如果first<second result=1;

实例2:

1 string  result;
2 int first=103 int second=204 result=first>second?"true":"false";
5 
6 //执行结果:如果first>second result等于true  如果first<second  result等于false

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-23
  • 2021-11-26
  • 2022-01-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-08
  • 2021-10-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-21
  • 2021-11-24
相关资源
相似解决方案