好的伙计们,这应该很简单......
我有一个 int 数组,我想看看数组中的数字是否连续。
出于某种原因,当我执行此操作(如下)时,我的数组从 int 值变为 ascii 并且变得很古怪并且不起作用。任何建议将不胜感激。在此示例中,它应该返回 true。
int numArray[5] = {1,2,3,4,5};
for( int i = 0 ; i < 4 ; i++ )
{
if ( numArray[i] == numArray[i+1] - 1 )
{
continue;
}
else
{
return false;
}
}
return true;
最佳答案
我会写这样的东西,读起来更简单一些。
bool issequential(const int* array, int size)
{
for (int i = 1; i < size; ++i)
if (array[i - 1] + 1 != array[i])
return false;
return true;
}
关于c++ - 比较数组c++中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7864162/