【官網(wǎng)下載】:官網(wǎng)下載
下載zip后,找到src目錄,將其復(fù)制到C語言的頭文件夾中。
如果使用的是vs:
1.隨便打開一個c語言的文件,ctrl+鼠標正建打開頭文件。
2.
打開文件夾后,復(fù)制進去,重啟vs即可。
typedef struct my_map{int key;
int value;
UT_hash_handle hh; // make this structure hashable
}map;
//結(jié)構(gòu)體中的key與value的類型可以根據(jù)情況自己定義
map* hashMap = NULL;
添加元素void hashMapAdd(int key,int value) {map* s;
HASH_FIND_INT(hashMap, &key, s);
if (s == NULL) {s = (map*)malloc(sizeof(map));
s->key = key;
HASH_ADD_INT(hashMap, key, s);
}
s->value = value;
}
? ?HASH_FIND_INT(hashMap, &key, s);
? hashmap:從哪里查找,傳入指針
? key:查找的元素,傳入指針
? s:out,結(jié)構(gòu)存放,指針
HASH_ADD_INT(hashMap, key, s);
? hashmap:添加到哪里,傳入指針
? key:添加的元素
? s:要添加的結(jié)構(gòu)指針
HASH_ADD_INT表示添加的鍵值為int類型
HASH_ADD_STR表示添加的鍵值為字符串類型
HASH_ADD_PTR表示添加的鍵值為指針類型
HASH_ADD表示添加的鍵值可以是任意類型
查找int hashMapFind(int key) {map* s;
s = (map*)malloc(sizeof(map));
HASH_FIND_INT(hashMap, &key, s);
return s->value;
}
刪除void hashMapDelete(int key) {map* s;
HASH_FIND_INT(hashMap, &key, s);
if (s != NULL) {HASH_DEL(hashMap, s);
}
}
清除void hashMapClean() {map* current, * tmp;
HASH_ITER(hh, hashMap, current, tmp) {HASH_DEL(hashMap, current);
free(current);
}
}
遍歷void Traverse() {map* s;
for (s = hashMap; s != NULL; s = s->hh.next) {printf("key: % d, value : % d\n", s->key, s->value);
}
}
全部代碼#includetypedef struct my_map{int key;
int value;
UT_hash_handle hh; // make this structure hashable
}map;
map* hashMap = NULL;
void hashMapAdd(int key,int value) {map* s;
HASH_FIND_INT(hashMap, &key, s);
if (s == NULL) {s = (map*)malloc(sizeof(map));
s->key = key;
HASH_ADD_INT(hashMap, key, s);
}
s->value = value;
}
int hashMapFind(int key) {map* s;
s = (map*)malloc(sizeof(map));
HASH_FIND_INT(hashMap, &key, s);
return s->value;
}
void hashMapDelete(int key) {map* s;
HASH_FIND_INT(hashMap, &key, s);
if (s != NULL) {HASH_DEL(hashMap, s);
}
}
void hashMapClean() {map* current, * tmp;
HASH_ITER(hh, hashMap, current, tmp) {HASH_DEL(hashMap, current);
free(current);
}
}
void Traverse() {map* s;
for (s = hashMap; s != NULL; s = s->hh.next) {printf("key: % d, value : % d\n", s->key, s->value);
}
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
當前標題:利用uthash.h實現(xiàn)hashmap-創(chuàng)新互聯(lián)
標題URL:http://www.rwnh.cn/article32/pojpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、品牌網(wǎng)站制作、網(wǎng)站收錄、App開發(fā)、外貿(mào)建站、品牌網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容