[英]C++ Compare two multidimensional 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},
};
我想比較此數組,但函數strcmp僅適用於一維數組。 我嘗試使用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
{
/*...*/
}
}
}
但是當應用程序運行時,該指令顯示:內存故障
如果我使用:
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
{
/*..*/
}
}
它們在程序上是不同的。
我該怎么辦?
以下內容可能會有所幫助:
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;
}
甚至(感謝Hanno Binder)
bool is_equal(const unsigned char (&lhs)[7][12], const unsigned char (&rhs)[7][12])
{
return memcmp(lhs, rhs, sizeof(lhs)) != 0;
}
您可以簡單地使用memcmp
(sizeof(channeltab1) == sizeof(equal_channeltab1)
&& (memcmp(channeltab1, equal_channeltab1, sizeof(equal_channeltab1)) == 0)
請注意,您必須確保它們的大小相等。
我建議您實現一個新的比較函數,而不要嘗試使用這些C函數。 您可以從以下內容開始:
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”用於標准“ C”字符串比較(或ASCII字符串),而不用於您所使用的數據(“ C ++”語言中的數據和數組始終相同)。 因此,您應該改用“ memcpy” 。 因此,您的代碼將如下所示:
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*/
}
}
使用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
};
並進行比較,您現在可以執行以下操作:
if (channeltab1 == equal_channeltab1){
//do stuff
}
注意, array[x*y]
與array[x][y]
多維數組存儲在連續內存中,因此實際上memcmp
應該可以工作, memcmp
避免了memcmp
錯誤。
memcmp(channeltab1, equal_channeltab1, sizeof(channeltab1))
編輯:strncmp不起作用,因為值中可能為零。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.