如果我有此代码:
array[0] = a < b ? c : d
array[1] = a < b ? d : c
有没有办法优雅地简化它?
我知道我可以做这样的事情来避免双重布尔检查:
if (a < b)
{
array[0] = c;
array[1] = d;
}
else
{
array[0] = d;
array[1] = c;
}
但这很冗长。
我是否缺少明显的东西?
计算索引以将变量分配给该索引,而不是为每个索引使用哪个变量。
int cLocation = a < b ? 0 : 1;
array[cLocation] = c;
array[1 - cLocation] = d;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句