比较两个相同长度的int数组的最佳方法是什么?

时间:2021-12-06 14:05:12

what is the best way to compare int arrays b and c with a:

将int数组b和c与a进行比较的最佳方法是什么?

int a[] = {0,1,0,0,1};
int b[] = {0,1,0,0,1};
int c[] = {1,1,0,0,1};

b and c are just examples, assume they can be any combination of 0s and 1s.

b和c只是示例,假设它们可以是0和1的任意组合。

I am trying to detect arrays identical to a. I have googled this for a while and have not found a satisfactory answer.

我试图检测与a相同的数组。我用谷歌搜索了一段时间,但没有找到满意的答案。

This is a beginners question I realise, thank you for your patience.

这是我意识到的初学者问题,谢谢你的耐心等待。

4 个解决方案

#1


20  

Use the standard memcmp function from <string.h>.

使用 中的标准memcmp函数。

memcmp(a, b, sizeof(a)) == 0

whenever a and b are equal.

每当a和b相等时。

#2


5  

If you mean

如果你的意思是

int a[] = {0,1,0,0,1};
int b[] = {0,1,0,0,1};
int c[] = {1,1,0,0,1};

then

然后

memcmp(a, b, sizeof(a)); /* returns zero for a match */
memcmp(a, c, sizeof(a)); /* returns nonzero for no match */

#3


4  

Use a loop and compare the individual elements one after another.

使用循环并逐个比较各个元素。

#4


0  

More information is needed on the question..! i can divide your question in two ways as below,

有关这个问题需要更多信息..!我可以通过以下两种方式划分你的问题,

  1. Compare array contents considering order?? Ex:char a[]={a,b,c},b[]={a,c,b} here since u r considering the order,the contents are not same so a!=b
  2. 比较数组内容考虑顺序?例:char a [] = {a,b,c},b [] = {a,c,b}这里因为你考虑顺序,内容不一样所以a!= b

  1. compare array contents irrespective of order? Ex:char a[]={a,b,c},b[]={a,c,b} here if u r not considering the order,the contents are same so a==b
  2. 比较数组内容而不管顺序?例:char a [] = {a,b,c},b [] = {a,c,b}这里如果不考虑订单,内容相同所以a == b

Solution for Question no 1: one can use memcmp for this problem.Because memcomp will compare lexicographical and return 0 or 1 or -1 as below

问题1的解决方案:可以使用memcmp解决此问题。因为memcomp将比较词典并返回0或1或-1,如下所示

 #include<stdio.h>
    #include<string.h>
    int main()
    {

        char a[]={'a','b','c'};
        char b[]={'a','b','c'};
        int x=memcmp(a,b,sizeof(a));
        printf("%d\n",x);

    return 0;
    }
***output:0***

    #include<stdio.h>
    #include<string.h>
    int main()
    {

        char a[]={'a','c','b'};
        char b[]={'a','b','c'};
        int x=memcmp(a,b,sizeof(a));
        printf("%d\n",x);

    return 0;
    }
***output:1***

    #include<stdio.h>
    #include<string.h>
    int main()
    {

        char a[]={'a','b','c'};
        char b[]={''b,'a','c'};
        int x=memcmp(a,b,sizeof(a));
        printf("%d\n",x);

    return 0;
    }
***output:-1***

Solution for Question no 2: one can use memcmp for this problem, the best solution for this problem is as below

问题2的解决方案:可以使用memcmp来解决这个问题,这个问题的最佳解决方案如下

here i answered for the above problem https://*.com/a/36130812/5206646

在这里我回答了上述问题https://*.com/a/36130812/5206646

#1


20  

Use the standard memcmp function from <string.h>.

使用 中的标准memcmp函数。

memcmp(a, b, sizeof(a)) == 0

whenever a and b are equal.

每当a和b相等时。

#2


5  

If you mean

如果你的意思是

int a[] = {0,1,0,0,1};
int b[] = {0,1,0,0,1};
int c[] = {1,1,0,0,1};

then

然后

memcmp(a, b, sizeof(a)); /* returns zero for a match */
memcmp(a, c, sizeof(a)); /* returns nonzero for no match */

#3


4  

Use a loop and compare the individual elements one after another.

使用循环并逐个比较各个元素。

#4


0  

More information is needed on the question..! i can divide your question in two ways as below,

有关这个问题需要更多信息..!我可以通过以下两种方式划分你的问题,

  1. Compare array contents considering order?? Ex:char a[]={a,b,c},b[]={a,c,b} here since u r considering the order,the contents are not same so a!=b
  2. 比较数组内容考虑顺序?例:char a [] = {a,b,c},b [] = {a,c,b}这里因为你考虑顺序,内容不一样所以a!= b

  1. compare array contents irrespective of order? Ex:char a[]={a,b,c},b[]={a,c,b} here if u r not considering the order,the contents are same so a==b
  2. 比较数组内容而不管顺序?例:char a [] = {a,b,c},b [] = {a,c,b}这里如果不考虑订单,内容相同所以a == b

Solution for Question no 1: one can use memcmp for this problem.Because memcomp will compare lexicographical and return 0 or 1 or -1 as below

问题1的解决方案:可以使用memcmp解决此问题。因为memcomp将比较词典并返回0或1或-1,如下所示

 #include<stdio.h>
    #include<string.h>
    int main()
    {

        char a[]={'a','b','c'};
        char b[]={'a','b','c'};
        int x=memcmp(a,b,sizeof(a));
        printf("%d\n",x);

    return 0;
    }
***output:0***

    #include<stdio.h>
    #include<string.h>
    int main()
    {

        char a[]={'a','c','b'};
        char b[]={'a','b','c'};
        int x=memcmp(a,b,sizeof(a));
        printf("%d\n",x);

    return 0;
    }
***output:1***

    #include<stdio.h>
    #include<string.h>
    int main()
    {

        char a[]={'a','b','c'};
        char b[]={''b,'a','c'};
        int x=memcmp(a,b,sizeof(a));
        printf("%d\n",x);

    return 0;
    }
***output:-1***

Solution for Question no 2: one can use memcmp for this problem, the best solution for this problem is as below

问题2的解决方案:可以使用memcmp来解决这个问题,这个问题的最佳解决方案如下

here i answered for the above problem https://*.com/a/36130812/5206646

在这里我回答了上述问题https://*.com/a/36130812/5206646