检查2d数组的所有元素是否相等的最快方法 [英] Fastest method to check if all elements of 2d array are equal

查看:79
本文介绍了检查2d数组的所有元素是否相等的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二维数组 houses [5] [2] = {{1,1},{1,1},{1,1},{1,1},{1,1}}检查该数组中的所有元素是否相等的最快方法是什么?这是我迄今为止尝试过的:```

I have a 2d array houses[5][2] = {{1,1},{1,1},{1,1},{1,1},{1,1}} What is the fastest way to check if all the elements inside that array are equal? Here is what I have tried so far: ```

for(int j=0;j<5;j++){
 for(int k=0;k<6;k++){
   if(houses[j][k] == houses[j+1][k+1] && j+1 != 5 && k + 1 != 6)
     equal = true;
    else{
     equal = false;
     break;
     }
}
}

这不会比较所有元素,我知道如何比较所有元素,但这似乎是一个很长的循环..有没有更快的方法呢?

This won't compare all the elements tho, I know how to compare all of them, but it seems to be a very long loop .. is there a faster way to do that?

推荐答案

您当前的代码将失败,因为 break 只会使您退出一个循环.您必须同时退出两者,这需要再次检查,就像这样:

Your current code will fail because break will only take you out of one loop. You must exit both, which requires a second check, like so:

auto the_value = houses[0][0];
bool equal     = true;

for(int j=0;j<5;j++){
  for(int k=0;k<6;k++){
    if(houses[j][k]!=the_value){
      equal = false;
      goto done;
    }
  }
  if(!equal)
    break
}

(将第一个元素存储在变量中,然后遍历所有元素以检查它们是否等于该变量,从而消除了通过检查相邻元素而引起的混乱.)

(Storing the first element in a variable and then looping over all of the elements to check to see if they are equal to that variable obviates the mess you invoke by checking adjacent elements.)

要同时突破两个循环,需要使用Dark Arts( goto ),但如果受过严格训练,则可能更具可读性/可维护性,并且可能稍快一些,具体取决于您的编译器:

Breaking out of both loops simultaneously requires the Dark Arts (goto), but may be more readable/maintainable if you are disciplined and may be slightly faster, depending on your compiler:

auto the_value = houses[0][0];
bool equal     = true;

for(int j=0;j<5;j++)
for(int k=0;k<6;k++)
  if(houses[j][k]!=the_value){
    equal = false;
    goto done; //Danger, Will Robinson!
  }

done:
//More stuff

您可能会发现平面阵列更快:

You may find a flat array to be faster:

auto the_value = houses[0][0];
bool equal     = true;
for(int i=0;i<5*6;i++)
  if(houses[i]!=the_value){
    equal = false;
    break;
  }

2D数组作为1D连续数组存储在内存中.使用平面数组寻址可访问相同的存储器位置,但显式避免了强制执行内部算术.对于高性能代码,您不妨考虑默认使用平面数组.

The 2D array is stored as a 1D contiguous array in memory. Using flat array addressing accesses the same memory locations, but explicitly avoids forcing the internal arithmetic. For highly performant code you may wish to consider using flat arrays by default.

由于您可能会多次使用此类函数或将其嵌入其他复杂的代码中,因此您可能希望对其进行抽象:

Since you might use a function such as this a number of times or have it embedded in otherwise complex code, perhaps you'd like to abstract it:

template<class T>
bool AllEqual(const T& arr, size_t N){
  T the_value = arr[0];
  for(int i=0;i<N;i++)
    if(arr[i]!=the_value)
      return false;
  return true;
}

AllEqual(houses, 5*6);

由于您使用C ++进行编码,因此您可能始终不想使用原始数组.假设使用平面数组,让我们使用STL重写代码:

Since you're coding in C++, you probably don't want to be using raw arrays anyway. Let's rewrite your code using the STL, assuming flat arrays:

template<class T>
bool AllEqual(const std::vector<T>& arr){
  return std::all_of(arr.begin(), arr.end(), [&](const T& x){ return x==arr[0]; });
}

std::vector<int> houses = {}; //Replace with appropriate initialization
if(AllEqual(houses))
  //Do stuff

(另外:正如另一个回答者所述,将数据添加到数组的方式似乎暗示它应该是2x6/6x2数组而不是5x6/6x5.)

(Also: as another answerer mentioned, the way you are adding data to your array seems to imply that it should be 2x6/6x2 array instead of 5x6/6x5.)

这篇关于检查2d数组的所有元素是否相等的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆