【问题标题】:C++ how to check if elements in an array are equal?C ++如何检查数组中的元素是否相等?
【发布时间】:2015-09-04 06:02:13
【问题描述】:

我正在尝试编写一个程序,该程序使用 for 循环检查数组中的所有值是否相等,但我无法找出 if 语句检查数组中每个值是否相等的方法,除非不断重复"if a[i] == a[1] && a[i] == a[0]" 等等。我不想这样做,因为我希望它适用于任何大小的任何数组。非常感谢任何帮助!

    for (unsigned i = 0; i < val; i++){
       if (a[i] == a[0])
          return true;
       else
          return false;
    }

【问题讨论】:

  • 假设这是一个函数,这将在第一个周期返回。它不检查数组中的所有其他项目
  • 我建议检查此链接。 stackoverflow.com/questions/14120346/… 祝你好运。
  • 平等是可传递的。如果 a == b 且 b == c,则 a == c。您的关系不满足,则它不是真正的平等关系。
  • 它可能与 stackoverflow.com/questions/15531258/… 重复。只需使用指针而不是插入器。
  • @DavidHeffernan,例如,如果这是一个不传递的浮点 epsilon 等式怎么办?然后怎样呢? :)

标签: c++ loops for-loop


【解决方案1】:
for (unsigned i = 0; i < val; i++) {
    if (a[i] != a[0]) {
        return false;
    }
}
return true;

应该这样做。

在这种情况下,代码将在不匹配的值上立即失败。然而,在匹配值上它只是继续检查(我们知道无论如何我们都需要测试数组的每个元素)。一旦完成,它就知道一切顺利(因为我们没有提前返回)并返回 true。

【讨论】:

    【解决方案2】:
    #include <algorithm>
    #include <vector>
    #include <iostream>
    
    int main(int argc, char** argv)
    {
        std::vector<int> eq{ 1, 1, 1, 1 };
        std::vector<int> nq{ 1, 2, 1, 1 };
    
        bool eq_res = std::all_of(std::begin(eq), std::end(eq),
            [&eq](int c) -> bool
        {
            return eq[0] == c;
        });
        bool nq_res = std::all_of(std::begin(nq), std::end(nq),
            [&nq](int c) -> bool
        {
            return nq[0] == c;
        });
    
        std::cout << "eq: " << eq_res << std::endl;
        std::cout << "nq: " << nq_res << std::endl;
    }
    

    编译 g++ --std=c++11 main.cpp

    【讨论】:

      【解决方案3】:

      只是为了好玩,使用 lambda 表达式

      #include <algorithm>
      using namespace std;
      
      template<size_t N>
      bool func(int (&arr)[N])
      {
          int* pOddValue = std::find_if(begin(arr), end(arr), 
              [&] (int val){ return val != arr[0];});
          return pOddValue != end(arr);
      }
      

      【讨论】:

        【解决方案4】:

        使用分治法,如果 n = 2^k,我们可以将比较数减少到 n-1,如下所示:

        bool divide(int arr[],int size)
        {
            if( size == 2 ) return arr[0] == arr[1];
        
            if( divide(arr,size/2) && divide(arr+size/2,size/2) )
                return arr[0] == arr[size/2];
        
            return false;
        }
        

        另一种类似的方法:

        for (unsigned i = 1; i < val; i++) {
            if (a[i] != a[i-1]) {
                return false;
            }
        }
        return true;
        

        【讨论】:

          【解决方案5】:

          看来你不需要处理 val = 0。 您可以在 1 行中完成。

          #include <functional>
          #include <algorithm>
          using namespace std;
          return all_of(
              a+1, a+val,
              bind(equal_to<remove_pointer<decltype(a)>::type>(), a[0], placeholders::_1));
          

          【讨论】:

            猜你喜欢
            • 2021-02-03
            • 1970-01-01
            • 2018-04-22
            • 2020-08-12
            • 2015-02-12
            • 2018-05-13
            • 2012-12-16
            • 1970-01-01
            相关资源
            最近更新 更多