【问题标题】:Can I make enumerations of variables in if statemants?我可以在 if 语句中枚举变量吗?
【发布时间】:2020-11-24 23:47:09
【问题描述】:

我正在尝试学习 C++(初学者)。但我想知道如何在if 之类的语句中枚举变量。

我只是在变量之间放一个逗号吗? 什么是正确的语法......或者这一切都很好?

#include <iostream>
using namespace std;

int main()
{
   int a, b, c, d, e;
   cin >> a >> b >> c >> d >> e;
   if (a, b, c > e && a, b, c > d) 
   {
      cout << a + b + c;
   }
}

【问题讨论】:

  • 不确定您在问什么,但这可能会对您有所帮助:stackoverflow.com/questions/8781447/…
  • 好吧,你可以做到这一点,因为 C++ 有 comma operator,这让很多初学者都望而却步,它实际上可以编译。但它不会做你想让它做的事情。所以,不,你不能这样做。
  • 提示:使用std::vector&lt;int&gt; 而不是一堆变量。
  • 那个逗号没有任何意义。这是可以编译的垃圾代码,因为它在语法上是有效的,但它并不能满足您的要求。如果您要断言前三个条目大于某个值,请在您的条目子集上使用 std::all_of
  • 要考虑的另一点:在文件的绝对顶部声明您的#include 定义,而不是在变量定义之后。在开始声明变量或函数之前,您应该包含您需要的任何内容。您还需要摆脱立即声明全局变量的习惯,将它们放在使用它们的函数中。

标签: c++ algorithm if-statement variables syntax


【解决方案1】:

不,C++ 中没有这样的东西。您需要将它们中的每一个拆分为自己的语句,例如:

if (a > e && b > e && c > e && a > d && b > d && c > d){

不过,这里的逻辑可以简化一下。

如果你想要a &gt; e AND a &gt; d,那么你只需要证明a 大于ed 中的较大者。 abc 的情况正好相反。也就是说,你只需要检查最小的a/b/c是否大于最大的e/d

所以这可以变成:

if (min({a, b, c}) > max(e, d)){

【讨论】:

    【解决方案2】:

    这是一种重新设计的方法,其中“值”和“目标”被分成两个独立的 vector 结构以方便比较:

    #include <algorithm>
    #include <iostream>
    #include <numeric>
    #include <vector>
    
    // Helper function to read an arbitrary number of entries into a vector
    void read_n(std::vector<int>& list, const size_t n) {
      for (size_t i = 0; i < n; ++i) {
        int v;
        std::cin >> v;
        list.push_back(v);
      }
    }
    
    int main() {
      // Container to hold the values
      std::vector<int> values;
    
      read_n(values, 3);
    
      // Container to hold the targets
      std::vector<int> targets;
    
      read_n(targets, 2);
    
      // Ensure that for each target...
      for (auto target : targets) {
        // ...all of the values exceed that target...
        if (!std::all_of(values.cbegin(), values.cend(), [target](int i) { return i > target; })) {
          // ...or else it's a fail.
          return -1;
        }
      }
    
      // Use accumulate to compute the sum and display it.
      std::cout << std::accumulate(values.cbegin(), values.cend(), 0) << std::endl;
    
      return 0;
    }
    

    在编写代码时,请尝试从结构循环的角度进行思考,而不仅仅是复制粘贴代码以添加更多变量。

    【讨论】:

    • @scohe001 我打算改用argv,但这似乎过于雄心勃勃。让我们看看吧。
    【解决方案3】:

    你不能做你在代码中尝试过的事情。

    @scohe001 的答案中显示了明显的方式(初学者)方式。但是,当您在某个时候学习 templatesfold expressions 时,以下解决方案会非常紧凑,并且与您尝试做的类似。

    #include <iostream>
    #include <utility>
    
    template<typename... Args>
    constexpr bool all_of_greater(const int lhs, Args&&... rhsArgs)
    {
       return ((lhs < std::forward<Args>(rhsArgs)) && ...);
    }
    

    现在您可以执行与您在代码中所做的类似的操作:

    if (all_of_greater(e, a, b, c) && all_of_greater(d, a, b, c))
    {
       std::cout << a + b + c;
    }
    

    (See Online Demo)

    【讨论】:

      猜你喜欢
      • 2018-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-11
      • 2022-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多