将std :: array与传统的数组C ++进行比较

时间:2021-02-17 15:58:25

I am trying to perform a comparison of elements in:

我正在尝试执行以下元素的比较:

std::vector<std::array<uint8_t, 6> > _targets =
{
  { 0x00, 0x00, 0x00, 0x00, 0x00, 0x11 }
  { 0x00, 0x00, 0x00, 0x00, 0x00, 0x22 }
};

to a traditional array:

传统阵列:

 uint8_t _traditional[6] =  { 0x00, 0x00, 0x00, 0x00, 0x00, 0x33 }

as:

  for (auto target : _targets) 
  {

     if (! memcmp(target, _traditional, 6)) {
        known = 1;
     } 
  }

and am receiving a data conversion error:

并收到数据转换错误:

error: cannot convert 'std::array<unsigned char, 6u>' to 'const void*' for argument '1' to 'int memcmp(const 
void*, const void*, size_t)

What is the propert byte wise comparison operation I can perform to accomplish equality evaluation?

我可以执行什么属性字节比较操作来完成平等评估?

1 个解决方案

#1


8  

You can use the data() member of array to get a pointer to the contained array:

您可以使用数组的data()成员来获取指向包含数组的指针:

if (! memcmp(target.data(), _traditional, 6))

The alternative of using &target[0] will work in this case (where you're storing a uint8_t) but won't work if you store a class that overloads the unary & (address) operator. But you could use std::addressof(target[0]) which will work even in the presence of an overloaded address operator.

使用&target [0]的替代方法将适用于这种情况(存储uint8_t的情况)但如果存储重载一元&(地址)运算符的类则不起作用。但是你可以使用std :: addressof(target [0]),它甚至可以在存在重载地址运算符的情况下工作。

#1


8  

You can use the data() member of array to get a pointer to the contained array:

您可以使用数组的data()成员来获取指向包含数组的指针:

if (! memcmp(target.data(), _traditional, 6))

The alternative of using &target[0] will work in this case (where you're storing a uint8_t) but won't work if you store a class that overloads the unary & (address) operator. But you could use std::addressof(target[0]) which will work even in the presence of an overloaded address operator.

使用&target [0]的替代方法将适用于这种情况(存储uint8_t的情况)但如果存储重载一元&(地址)运算符的类则不起作用。但是你可以使用std :: addressof(target [0]),它甚至可以在存在重载地址运算符的情况下工作。