C++:检查所有数组元素是否相等的最快方法 [英] C++: Fastest method to check if all array elements are equal
本文介绍了C++:检查所有数组元素是否相等的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
检查数组(最好是整数数组)的所有元素是否相等的最快方法是什么.到目前为止,我一直在使用以下代码:
What is the fastest method to check if all elements of an array(preferable integer array) are equal. Till now I have been using the following code:
bool check(int array[], int n)
{
bool flag = 0;
for(int i = 0; i < n - 1; i++)
{
if(array[i] != array[i + 1])
flag = 1;
}
return flag;
}
推荐答案
int check(const int a[], int n)
{
while(--n>0 && a[n]==a[0]);
return n!=0;
}
这篇关于C++:检查所有数组元素是否相等的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文