我想将utash库用于带有一对int
和const char *
作为复合键的哈希表:
typedef struct entry_s {
// This field is needed by the uthash library
UT_hash_handle hh;
// Values
/* ... */
// Compound key
int num;
const char *str;
} entry;
具体来说,我希望指向的字符串const char *
成为键的一部分。澄清一下:指针的不同值可能对应于同一个字符串(在 的意义上strcmp()
)。
用户指南展示了如何使用int
和char[]
作为复合键来实现类似于我想要的键:
typedef struct another_entry_s {
// This field is needed by the uthash library
UT_hash_handle hh;
// Values
/* ... */
int str_len;
// Compound key
int num;
char str[];
} another_entry;
但是,第二种方法(即(int, char[])
)假设字符串被复制到char[]
,但我想避免复制。
另外,我不是为了利用和方便宏而寻找连接int
和指向的字符串。const char *
HASH_ADD_KEYPTR()
HASH_FIND_STR()
我不知道如何使用第一种方法(即)来使用HASH_ADD()
,和其他通用宏。看起来不可能避免复制,就像 uthash 库设计一样。我理解对吗?还是有我忽略的非复制方法?HASH_FIND()
(int, const char *)