試試這個:http://uthash.sourceforge.net/
頭版上的例子是相當多的自我解釋。
struct Stock {
float price, change;
char name[3];
UT_hash_handle hh;
};
struct Stock * stocksHash = NULL;
struct Stock * stockItem;
HASH_ADD_STR(stocksHash, name, stockItem);
HASH_FIND_STR(stocksHash, "IBM", stockItem);
//edit
void new_stock(struct Stock * stocksHash, char *name, float price, float change) {
struct Stock *s;
s = malloc(sizeof(struct Stock));
strncpy (s->name, name, 3);
s->price = price; s->change = change;
s->id = user_id;
HASH_ADD_STR(stocksHash, name, s);
}
void update_stock (struct Stock * stocksHash, char *name,
float price, float change) {
struct Stock *s;
HASH_FIND_STR (stocksHash, name, s);
if (s) {
s->price = price; s->change = change;
}
}
您可以使用'struct'來分組相關的信息。當然,有一些公有領域的hashtable代碼可以用於查找。 http://stackoverflow.com/questions/1138742/looking-for-a-good-hash-table-implementation-in-c –
你真的應該只是切換到C++。然後你就有了list,map,unordered_map等等。 –