简体   繁体   English

如何比较 C 中的两个 2D arrays?

[英]How can I compare two 2D arrays in C?

İ try it, but this code not run stable.我试试看,但这段代码运行不稳定。

İ have two 2D arrays first[a][b] and second[x][y] .我有两个二维 arrays first[a][b]second[x][y]

I want to compare these two arrays.我想比较这两个 arrays。 How can I do this?我怎样才能做到这一点?

My code:我的代码:

    for (int i = 0; i < 10; i++) 
        for (int j = 0; j < 10; j++)
            if (first[i][j] == second[i][j])
                return 1;

An alternative way would be using memcmp .另一种方法是使用memcmp

NOTE: This will work on modern architectures, where int does not have any padding bits.注意:这适用于现代架构,其中int没有任何填充位。 Thanks for @chqrlieforyellowblockquotes to pointing this out.感谢@chqrlieforyellowblockquotes 指出这一点。

#include <stdio.h>
#include <string.h>

int main() {
  int a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
  int b[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
  int c[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 123}};
  int ret;

  ret = memcmp(a, b, 3 * 3 * sizeof(int));

  if (ret == 0)
    printf("a equal b\n");
  else
    printf("a not equal b\n");

  ret = memcmp(a, c, 3 * 3 * sizeof(int));

  if (ret == 0)
    printf("a equal c\n");
  else
    printf("a not equal c\n");

  return 0;
}

Output Output

a equal b
a not equal c

Through your code, I see that:通过您的代码,我看到:

both the arrays are equal if one of the corresponding elements in the arrays are equal.如果 arrays 中的相应元素之一相等,则 arrays 都相等。

But I think you want this:但我认为你想要这个:

both the arrays are equal if all the corresponding elements in the arrays are equal如果 arrays 中的所有相应元素都相等,则 arrays 都相等

That does not happen in your code because you are return ing immediately when one of the corresponding elements are equal ( == ), so the remaining checks are omitted by your code.这不会在您的代码中发生,因为当相应元素之一相等( == )时,您会立即return ing,因此您的代码会省略剩余的检查。

Solution:解决方案:

Change if( first[i][j]==second[i][j]) to if (first[i][j] != second[i][j]) if( first[i][j]==second[i][j])更改为if (first[i][j] != second[i][j])

This will look for the first unequal element.这将寻找第一个不相等的元素。 Then you can return the appropriate message or perhaps break depending on what exactly you are doing with that code.然后,您可以return适当的消息或break ,具体取决于您对该代码的具体操作。

You were on the right track, but the arrays are identical if all entries are.您在正确的轨道上,但如果所有条目都相同,则 arrays 是相同的。 You should return 0 when you find a difference and return 1 otherwise:发现差异时应返回 0,否则应返回 1:

int compare_matrices(int first[10][10], int second[10][10]) {
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            if (first[i][j] != second[i][j])
                return 0;
       }
    }
    return 1;
}

An alternative for regular architectures without padding bits would use memcmp() :没有填充位的常规架构的替代方案是使用memcmp()

#include <string.h>

int compare_matrices(int first[10][10], int second[10][10]) {
    return !memcmp(first, second, 10 * sizeof(*first));
}

The first code is simplistic but the compiler can optimize it by unrolling the loops and may produce performance comparable or better than that of memcmp() .第一个代码很简单,但编译器可以通过展开循环来优化它,并且可以产生与memcmp()相当或更好的性能。

Reducing the number of tests would increase the number of memory accesses but may produce better performance as the compiler can use SIMD instructions:减少测试次数会增加 memory 访问次数,但可能会产生更好的性能,因为编译器可以使用 SIMD 指令:

int compare_matrices(int first[10][10], int second[10][10]) {
    for (int i = 0; i < 10; i++) {
        int res = 0;
        for (int j = 0; j < 10; j++) {
            res |= (first[i][j] != second[i][j]);
        }
        if (res) return 0;
    }
    return 1;
}

You can use GodBolt's Compiler Explorer to compare code generators.您可以使用GodBolt 的 Compiler Explorer来比较代码生成器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM