C中的hash map
由于C语言中没有提供官方的hash map支持,并且我的工作开发环境是基于linux的,所以我在做题时选用了uthash来进行。
uthash使用
1.导入
include<uthash.h>;
2.构建结构
struct hashTable{
int key;
int val;
/*each struct map each kay-value;*/
UT_hash_handle hh;// makes this structure hashable
}
3.基本使用
初始化:
struct hashTable* urHashTable = NULL;
增:
void insert(int iKey, int iVal){
struct hashTable* tmp = malloc(sizeof(struct hashTable));
tmp -> key = iKey;
tmp -> val = iVal;
HASH_ADD_INT(urHashTable, data, tmp);
}
查:
struct hushTable* find(int iKey){
struct hashTable* tmpFind;
HASH_FIND_INT(urHashTable, &iKey, tmpFind);
return tmpFind;
}
编译错误点:
1.runtime error: store to address 0x625000002048 with insufficient space for an object of type ‘unsigned int’
可能原因: malloc分配错误,在分配空间时写错。