简体   繁体   English

C ++比较两个多维数组

[英]C++ Compare two multidimensional arrays

I have two arrays: 我有两个数组:

unsigned char channeltab1[7][12]; //array which I receive from socket(Array is the same as below)
unsigned char equal_channeltab1[7][12] //arrays which I wants to compare with channeltab1
{
    {0x10, 0x0f, 0x02, 0x02, 0x01, 0x00, 0x00, 0x06, 0x10, 0x0e, 0xff, 0xef},
    {0x10, 0x0f, 0x02, 0x02, 0x02, 0x00, 0x00, 0x06, 0x10, 0x0e, 0xff, 0xef},
    {0x10, 0x0f, 0x02, 0x02, 0x03, 0x00, 0x00, 0x06, 0x10, 0x0e, 0xff, 0xef},
    {0x10, 0x0f, 0x02, 0x02, 0x04, 0x00, 0x00, 0x06, 0x10, 0x0e, 0xff, 0xef},
    {0x10, 0x0f, 0x02, 0x02, 0x05, 0x00, 0x00, 0x06, 0x10, 0x0e, 0xff, 0xef},
    {0x10, 0x0f, 0x02, 0x02, 0x06, 0x00, 0x00, 0x06, 0x10, 0x0e, 0xff, 0xef},
    {0x10, 0x0f, 0x02, 0x02, 0x07, 0x00, 0x00, 0x06, 0x10, 0x0e, 0xff, 0xef},
};

I want compare this arrays, but function strcmp work only with one-dimensional arrays. 我想比较此数组,但函数strcmp仅适用于一维数组。 I try use strncmp: 我尝试使用strncmp:

for(int x = 0; x<7; x++)
{
    for(int i =1; x<12;i++)
    {
        if (strncmp (reinterpret_cast<const char*>(channeltab1[x][i]),reinterpret_cast<const char*>(equal_channeltab1[x][i]),2) == 0)
        {
            /*...*/
        }
        else
        {
            /*...*/
        }
    }

}

But when application run this instruction says: Memory fault 但是当应用程序运行时,该指令显示:内存故障

If i use: 如果我使用:

for(int x = 0; x<7; x++)
{
    if (strncmp (reinterpret_cast<const char*>(channeltab1[x]),reinterpret_cast<const char*>(equal_channeltab1[x]),2) == 0)
    {
        /*..*/
    }
    else
    {
        /*..*/
    }   
}

They are not the same for program. 它们在程序上是不同的。

What should I do? 我该怎么办?

Following may help: 以下内容可能会有所帮助:

bool is_equal(const unsigned char (&lhs)[7][12], const unsigned char (&rhs)[7][12])
{
    for (int i = 0; i != 7; ++i) {
        if (memcmp(lhs[i], rhs[i], 12) != 0) {
            return false;
        }
    }
    return true;
}

or even (thanks to Hanno Binder) 甚至(感谢Hanno Binder)

bool is_equal(const unsigned char (&lhs)[7][12], const unsigned char (&rhs)[7][12])
{
    return memcmp(lhs, rhs, sizeof(lhs)) != 0;
}

You can simply use memcmp 您可以简单地使用memcmp

(sizeof(channeltab1) == sizeof(equal_channeltab1)
&& (memcmp(channeltab1, equal_channeltab1, sizeof(equal_channeltab1)) == 0)

Note you have to make sure theirs sizes are equal. 请注意,您必须确保它们的大小相等。

I would suggest you to implement a new comparing function rather then trying to use these C functions. 我建议您实现一个新的比较函数,而不要尝试使用这些C函数。 You can start from something like this: 您可以从以下内容开始:

bool checkEquals(unsigned char** a, unsigned char** b, size_t outterSize, size_t innerSize)
{
    for (size_t i = 0; i < outterSize; ++i)
    {
        for (size_t j = 0; j < innerSize; ++j)
        {
            if (a[i][j] != b[i][j])
            {
                return false;
            }
        }
    }
    return true;
}

"strncmp" is used for standard "C" strings comparsion (or ASCII ones) not for data as it's in your case (data and arrays are always the same in "C++" language). “ strncmp”用于标准“ C”字符串比较(或ASCII字符串),而不用于您所使用的数据(“ C ++”语言中的数据和数组始终相同)。 So you should use 'memcpy' instead. 因此,您应该改用“ memcpy” So your code will look something like this: 因此,您的代码将如下所示:

for(int x = 0; x<7; x++)
{
        if (memcmp((void*)channeltab1[x],(void*)equal_channeltab1[x], 12 * sizeof(unsigned char)) == 0)
        {
            /*equal*/
        }
        else
        {
            /*not equal*/
        }   
}

Use std::Array 使用std::Array

std::array<unsigned char, 7*12> channeltab1;
std::array<unsigned char, 7*12> equal_channeltab1 = {
    0x10, 0x0f, 0x02, 0x02, 0x01, 0x00, 0x00, 0x06, 0x10, 0x0e, 0xff, 0xef,
    0x10, 0x0f, 0x02, 0x02, 0x02, 0x00, 0x00, 0x06, 0x10, 0x0e, 0xff, 0xef,
    0x10, 0x0f, 0x02, 0x02, 0x03, 0x00, 0x00, 0x06, 0x10, 0x0e, 0xff, 0xef,
    0x10, 0x0f, 0x02, 0x02, 0x04, 0x00, 0x00, 0x06, 0x10, 0x0e, 0xff, 0xef,
    0x10, 0x0f, 0x02, 0x02, 0x05, 0x00, 0x00, 0x06, 0x10, 0x0e, 0xff, 0xef,
    0x10, 0x0f, 0x02, 0x02, 0x06, 0x00, 0x00, 0x06, 0x10, 0x0e, 0xff, 0xef,
    0x10, 0x0f, 0x02, 0x02, 0x07, 0x00, 0x00, 0x06, 0x10, 0x0e, 0xff, 0xef
};

and to compare, you can now do: 并进行比较,您现在可以执行以下操作:

if (channeltab1 == equal_channeltab1){
    //do stuff
}

note, an array[x*y] is the same as an array[x][y] 注意, array[x*y]array[x][y]

Multidimensional arrays are store in continuous memory so actually memcmp should work, memcmp avoids mistakes in the cast. 多维数组存储在连续内存中,因此实际上memcmp应该可以工作, memcmp避免了memcmp错误。

memcmp(channeltab1, equal_channeltab1, sizeof(channeltab1))

Edit: strncmp doesn't work because there could be zero's in the values. 编辑:strncmp不起作用,因为值中可能为零。

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

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