使用C类型uuid_t作为std :: map中的键的最佳方法是什么?

Dav*_*ave 5 c++ uuid dictionary stdmap std

这是在地图中提供唯一键的合适方法吗?换句话说,键是由uuid中包含的唯一值构成的,还是由指向uuid_tstruct 的指针构成的?一个问题是,当我不关心容器内的按键排序时,是否有更高效的容器?

#include <uuid/uuid.h>

int main(int argc, char **argv)
{    
   std::map<uuid_t,int> myMap;         

   uuid_t id1;
   uuid_t id2;

   uuid_generate( (unsigned char *)&id1 );  
   uuid_generate( (unsigned char *)&id2 );

   myMap[id1] = 5;
   myMap[id2] = 4;

}
Run Code Online (Sandbox Code Playgroud)

ony*_*ony 5

我想使用第三方C结构的最佳方法是通过友好的功能使用它们.所以如果你想在STL中使用uuid_t,我建议你为这个结构创建一些C++接口/包装器

struct Uuid {
  uuid_t uuid;
  Uuid(const Uuid &other) { uuid_copy(uuid, other.uuid); }
  Uuid(const uuid_t other_uuid) { uuid_copy(uuid, other_uuid); }
  void generateInplace() { uuid_generate(uuid); }
  static Uuid generate() { Uuid wrapped; uuid_generate(wrapped.uuid); return wrapped; }
  bool operator<(const Uuid &other) { return uuid_compare(uuid, other.uuid) < 0; }
  bool operator==(const Uuid &other) { return uuid_compare(uuid, other.uuid) == 0; }
  // ...
};
Run Code Online (Sandbox Code Playgroud)

那应该向你隐瞒uuid_t不是结构的事实,而是指向数组的指针(即typedef unsigned char uuid_t[16]).

注意:有uuid库的升级版本