将C类型uuid_t用作std :: map中的键的最佳方法是什么? [英] what is the best way to use the C type uuid_t as a key in a std::map?
问题描述
这是在地图中提供唯一键的适当方法吗?换句话说,键是由uuid中包含的唯一值构成的,还是由指向uuid_t
结构的指针构成的?附带的问题是,当我不关心容器内按键的排序方式时,是否有一个效率更高的容器?
Is this an appropriate way to provide unique keys in a map? In other words, is the key being made from the unique value contained in the uuid, or is it being made from the pointer to the uuid_t
struct? A side question, is there a more efficient container, when I don't care about the ordering by keys inside the container?
#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;
}
推荐答案
我猜想使用第三方C结构的最佳方法是通过它们的友好函数使用它们.因此,如果您想在STL中使用uuid_t,建议您为该结构创建某种C ++接口/包装器,例如
I guess the best way of using third-party C-structures is to use them through their friendly functions. So if you wanna use uuid_t in STL, I'd suggest you to create some kind of C++ interface/wrapper to that structure like
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; }
// ...
};
这应该使您看不到uuid_t
不是结构而是指向数组的指针(即typedef unsigned char uuid_t[16]
)的事实.
That should hide from you the fact that uuid_t
isn't structure but pointer to array (i.e. typedef unsigned char uuid_t[16]
).
注意:存在 uuid库的增强版本
这篇关于将C类型uuid_t用作std :: map中的键的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!