【发布时间】:2016-06-10 08:46:00
【问题描述】:
我 99% 确定这不会起作用,但剩下的 1% 让我很困扰
int x;
//is this if statement
if(x == 1, 5, 7)
{
//do something here
}
//equivalent to this if statement
if((x == 1) || (x == 5) || (x == 7))
{
//do something here
}
【问题讨论】:
-
你可以试试看它是否有效。
-
这是我第一次看到有人用逗号尝试这个。他们通常会尝试类似
if (x == 1 || 5 || 7)。 -
如果您想要一个快速整数“X 在集合 Y”操作(其中集合 Y 在编译时已知),请使用
switch语句,因为它被编译为文字哈希表(一个分支表),速度非常快。 -
@Barmar 为什么要在我能得到快速响应并通过这种方式找到答案时尝试一下,很容易。
-
@ThatGuy “我有一个可能的值列表来检查它很长” 将该列表放入
std::array或std::vector并使用std::find_if().
标签: c++ if-statement syntax