如何根据顺序比较具有相等元素集的两个一维数组的元素?

How can I compare elements of two one-dimensional arrays with equal sets of elements according their order?

提问人:Ivan 提问时间:11/28/2022 最后编辑:Vlad from MoscowIvan 更新时间:11/29/2022 访问量:59

问:

我正在尝试根据它们的顺序比较两个数组的元素 例如:

int a[] = { 1, 2, 3 };
int b[] = { 1, 2, 4 };

有 3 个元素,-loop 和条件没有问题for

if (a[0] == b[0] && a[1] == b[1] && a[2] == b[2])

但是我怎样才能对数组中的 n 个元素做同样的事情呢? 条件应如下所示:但不合适。if (a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && ... && a[n] == b[n])

// Comparing elements of two one-dimensional arrays with
// equal sets of elements according their order.

#include <stdio.h>

int main(void) {
    // two one-dimensional arrays with 3 elements for example
    int a[] = { 1, 2, 3 };
    int b[] = { 1, 2, 4 };

    // comparing elements:

    for (int i = 0; i < 3; i++) {
        if (a[0] == b[0] && a[1] == b[1] && a[2] == b[2]) // but if I have n - elements the problem appears
        {
            printf("all right\n");
        }
        else
        {
            printf("no all right\n");
        }
    }
}
数组 C for 循环 if 语句 比较

评论


答:

0赞 chqrlie 11/28/2022 #1

同时具有循环和显式比较表达式是多余的。您可以使用一个简单的函数来解决任意数量的元素的问题,该函数同时获取数组及其长度,比较数组元素并返回布尔值:for

#include <stdbool.h>
#include <stdio.h>

bool compare_arrays(const int *a, const int *b, size_t n) {
    for (size_t i = 0; i < n; i++) {
        if (a[i] != b[i])
            return false;
    }
    return true;
}

int main() {
    // two one-dimensional arrays with 3 elements for example
    int a[] = { 1, 2, 3 };
    int b[] = { 1, 2, 4 };
    size_t a_len = sizeof(a) / sizeof(*a);

    // comparing elements:
    if (compare_arrays(a, b, a_len)) {
        printf("all right\n");
    } else {
        printf("no all right\n");
    }
    return 0;
}
0赞 Axolotl 11/28/2022 #2

for 循环的伟大之处在于,递增变量 i 实际上可以用来索引到你的数组中!请注意,您将 a 的每个元素与 b 的每个元素进行比较,并递增 1。您的变量 i 也递增 1。您的代码现在会检查整个数组 3 次完整时间,使您的运行时比必要的时间长三倍,而且不可扩展。通过比较 a[i] 和 b[i] 来修复它。如果它们不相同,请打印您的消息并使用 break 关键字中断循环。如果您到达循环的末尾并且它们都相同,请打印您的成功消息。

0赞 Vlad from Moscow 11/28/2022 #3

This for 循环

for (int i = 0; i < 3; i++) {
    if (a[0] == b[0] && a[1] == b[1] && a[2] == b[2]) // but if I have n - elements the problem appears
    
    {
        printf("all right\n");
    }
    else
    {
        printf("no all right\n");
    }
}

没有意义,因为在循环的每次迭代中,数组的所有三个元素都在 if 语句中进行比较。

    if (a[0] == b[0] && a[1] == b[1] && a[2] == b[2]) // but if I have n - elements the problem appears

你需要的是,在循环的每次迭代中,将数组的相应元素与给定的索引进行比较。i

所以你可以写例如

int i = 0;
while ( i < 3 && a[i] == b[i] ) ++i;

if ( i == 3 )
{       
    printf("all right\n");
}
else
{
    printf("no all right\n");
}

例如,您可以将两个数组作为单独的函数进行比较

int compare_arrays( const int a[], const int b[], size_t n )
{
    size_t i = 0;

    while ( i < n && a[i] == b[i] ) ++i;

    return i == n;
}

如果两个数组彼此相等,则返回该函数。10

所以在主要情况下,你可以写例如

if ( compare_arrays( a, b, 3 ) )
{       
    printf("all right\n");
}
else
{
    printf("no all right\n");
}
0赞 Luis Colorado 11/29/2022 #4

但是我怎样才能对数组中的 n 个元素做同样的事情呢?条件应如下所示:但不合适。if (a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && ... && a[n] == b[n])

使用循环:

int result = 1;
for (int i = 0; i <= n; i++)
    if (a[i] != b[i])
        break;
if (i <= n) result = 0; /* if we exited prematurely, change result into a 0 */

注意:你使用了一个循环来检查从 0 到 的元素(这是元素,这就是测试在循环中的编写方式),但在文本中,你谈到了检查元素数组(应该从 到 索引),在这种情况下,循环应该是:nn + 1forn0n-1

for (int i = 0; i < n; i++) /* pay attention to the comparison operator */

之后的测试应同样修改:

if (i < n) result = 0;

我之所以评论这一点,是因为在语句中错误地编写测试子句是一个非常常见的错误。for