如何简化这个 if 语句?
How can this if statement be simplified?
我正在使用 CLion IDE 来编写我的 C++ 项目。有时碰巧 IDE 试图比我更聪明并给我建议。我在代码检查(由 CLion)期间遇到了一个简单的问题。它说可以简化以下代码,尽管我相信这是我能想到的最简单的形式:
代码:
if (node.first >= 0 && node.first <= 45 &&
node.second >= 0 && node.second <= 30)
return true;
else
return false;
假设节点的类型是std::pair<int, int>
我从 CLion IDE 得到的建议如下:
代码检查评论:
Inspection info: This inspection finds the part of the code that can be simplified, e.g. constant conditions, identical if branches, pointless boolean expressions, etc.
您认为这可以进一步简化吗?
CLion 是在暗示你这一点...
if (node.first >= 0 && node.first <= 45 &&
node.second >= 0 && node.second <= 30)
return true;
else
return false;
可能只是 re-written 和
return node.first >= 0 && node.first <= 45 &&
node.second >= 0 && node.second <= 30;
因为在控制语句中用作条件的表达式显然可以自然地转换为 true 和 false。
我正在使用 CLion IDE 来编写我的 C++ 项目。有时碰巧 IDE 试图比我更聪明并给我建议。我在代码检查(由 CLion)期间遇到了一个简单的问题。它说可以简化以下代码,尽管我相信这是我能想到的最简单的形式:
代码:
if (node.first >= 0 && node.first <= 45 &&
node.second >= 0 && node.second <= 30)
return true;
else
return false;
假设节点的类型是std::pair<int, int>
我从 CLion IDE 得到的建议如下:
代码检查评论:
Inspection info: This inspection finds the part of the code that can be simplified, e.g. constant conditions, identical if branches, pointless boolean expressions, etc.
您认为这可以进一步简化吗?
CLion 是在暗示你这一点...
if (node.first >= 0 && node.first <= 45 &&
node.second >= 0 && node.second <= 30)
return true;
else
return false;
可能只是 re-written 和
return node.first >= 0 && node.first <= 45 &&
node.second >= 0 && node.second <= 30;
因为在控制语句中用作条件的表达式显然可以自然地转换为 true 和 false。