Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何在不使用计数器的情况下检查数组的元素是否都相同?_C_Arrays_Comparison - Fatal编程技术网

C 如何在不使用计数器的情况下检查数组的元素是否都相同?

C 如何在不使用计数器的情况下检查数组的元素是否都相同?,c,arrays,comparison,C,Arrays,Comparison,假设我有一个数组 bool string[N]={false}; 执行某些操作后,数组字符串的所有元素都变为true。 我想在这样的if语句中检查这个条件:- 伪码-- 我怎样才能做到这一点?我不应该使用像这样的计数器 for(int i=0;i<N;i++) //or something else like this for(int i=0;i如果可以使用指针,那么它可以是这样的: bool first = string[0]; bool* current = string

假设我有一个数组

 bool string[N]={false};
执行某些操作后,数组字符串的所有元素都变为true。 我想在这样的if语句中检查这个条件:-

伪码--

我怎样才能做到这一点?我不应该使用像这样的计数器

for(int i=0;i<N;i++)   //or something else like this 

for(int i=0;i如果可以使用指针,那么它可以是这样的:

bool first = string[0];
bool* current = string + 1;
bool* end = string + N;

bool allEqual = true;

while (current < end)
{
    if (*current != first)
    {
        allEqal = false;
        break;  // No need to loop more
    }

    ++current;
}

if (allEqual)
    std::cout << "All elements are " << std::boolalpha << first << '\n';
else
    std::cout << "First not-equal is at index " << (current - string) / sizeof(string[0]) << '\n';
bool first=string[0];
布尔*电流=字符串+1;
bool*end=string+N;
bool-allEqual=true;
while(当前<结束)
{
如果(*当前!=第一个)
{
allEqal=false;
break;//无需再循环
}
++电流;
}
如果(allEqual)

std::cout如果您可以使用指针,那么它可能是这样的:

bool first = string[0];
bool* current = string + 1;
bool* end = string + N;

bool allEqual = true;

while (current < end)
{
    if (*current != first)
    {
        allEqal = false;
        break;  // No need to loop more
    }

    ++current;
}

if (allEqual)
    std::cout << "All elements are " << std::boolalpha << first << '\n';
else
    std::cout << "First not-equal is at index " << (current - string) / sizeof(string[0]) << '\n';
bool first=string[0];
布尔*电流=字符串+1;
bool*end=string+N;
bool-allEqual=true;
while(当前<结束)
{
如果(*当前!=第一个)
{
allEqal=false;
break;//无需再循环
}
++电流;
}
如果(allEqual)

std::cout“我不应该为(int I=0;I)使用
这样的计数器。”我不应该为(int I=0;IPP)使用
这样的计数器,只需要稍微修改一下他的代码,他提到的答案是:-

if (memcmp (&string [0], &string [1], sizeof string [0] * (N - 1)) == 0)
{
  /* all elements the same */
}
N-1停止溢出缓冲区的末尾


memcmp比较字符串[0]和字符串[1],然后字符串[1]和字符串[2],然后字符串[2]和字符串[3]等等,直到字符串[n-2]和字符串[n-1]。

PP只需要稍微修改一下他的代码,他提到的答案是:-

if (memcmp (&string [0], &string [1], sizeof string [0] * (N - 1)) == 0)
{
  /* all elements the same */
}
N-1停止溢出缓冲区的末尾


memcmp比较字符串[0]与字符串[1],然后字符串[1]与字符串[2],然后字符串[2]与字符串[3]等等,直到字符串[n-2]和字符串[n-1]。

如果只想检查一个If,而不检查循环,可以尝试以下操作:

bool string[N] = {false};

if ((0 == memcmp(&string[0], &string[1], sizeof(string[0]) * (sizeof(string) - 1))) {
   //equal
}

因为两个内存区域重叠,偏移了一个,所以会对阵列中的每一对进行比较。

如果要仅使用一个If检查,而不使用循环,可以尝试以下操作:

bool string[N] = {false};

if ((0 == memcmp(&string[0], &string[1], sizeof(string[0]) * (sizeof(string) - 1))) {
   //equal
}

因为两个内存区域重叠,偏移1,所以数组中的每一对都会进行比较。

@JoachimPileborg是的。
for(bool*p=string;(p-string)
@JoachimPileborg是的。
for(bool*p=string;(p-string)
@LihO我没有注意到。听起来很公平。@Mauren没有看到C标记,只是看到了“C某物”还有
bool
类型。@LihO自C99标准以来,OPs代码确实是C。
标题包含
bool
类型和
true
false
值。@LihO我没有注意到。听起来很公平。@Mauren错过了C标记,只是看到了“C某物”和
bool
类型。@li自C99标准以来,OPs代码实际上是C。
标题包含
bool
类型和
true
false
值。