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

Ric*_*ice 5 c++ arrays c++11

我正在尝试对以下元素进行比较:

std::vector<std::array<uint8_t, 6> > _targets =
{
  { 0x00, 0x00, 0x00, 0x00, 0x00, 0x11 }
  { 0x00, 0x00, 0x00, 0x00, 0x00, 0x22 }
};
Run Code Online (Sandbox Code Playgroud)

传统阵列:

 uint8_t _traditional[6] =  { 0x00, 0x00, 0x00, 0x00, 0x00, 0x33 }
Run Code Online (Sandbox Code Playgroud)

如:

  for (auto target : _targets) 
  {

     if (! memcmp(target, _traditional, 6)) {
        known = 1;
     } 
  }
Run Code Online (Sandbox Code Playgroud)

并收到数据转换错误:

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

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

Kor*_*elK 8

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

if (! memcmp(target.data(), _traditional, 6))
Run Code Online (Sandbox Code Playgroud)

使用的替代方法&target[0]将适用于这种情况(您存储的位置uint8_t),但如果存储重载一元&(地址)运算符的类,则无法使用.但std::addressof(target[0])即使存在重载的地址运算符,您也可以使用它.

  • `target.begin()`不保证产生指针. (3认同)