hash表是一种提供key-value访问的数据结构,通过指定的key值可以快速的访问到与它相关联的value值。hash表的一种典型用法就是字典,通过单词的首字母能够快速的找到单词。关于hash表的详细介绍请查阅数据结构的相关书籍,我这里只介绍glib库中hash表的基本用法。
要使用一个hash表首先必须创建它,glib库里有两个函数可以用于创建hash表,分别是g_hash_table_new()和g_hash_table_new_full(),它们的原型如下:
其中hash_func是一个函数,它为key创建一个hash值;key_equal_func用于比较两个key是否相等;
key_destroy_func当你从hash表里删除、销毁一个条目时,glib库会自动调用它释放key所占用的内存空间,
这对于key是动态分配内存的hash表来说非常有用;value_destroy_func的作用与key_destroy_func相似,
只是它释放的是value占用的内存空间。
下面以一段代码来演示glib库中hash表的基本用法
通过代码我们看到glib库为hash表接供了非常简单的接口。下面是代码的输出
[plusboy@localhost c]$ ./g_hash
1 ---> one
2 ---> two
3 ---> three
4 ---> four
5 ---> five
Size of hash table: 5
Before replace: 3 ---> three
After replace: 3 ---> third
1 ---> one
3 ---> third
4 ---> four
5 ---> five
Now size of hash table: 4
Remove an item from hash table:
We free key: two
We free value: second
Destroy hash table:
We free key: three
We free value: third
We free key: one
We free value: first
对代码的说明:
1、首先我们调用了g_hash_table_new()来创建一个hash表,然后用g_hash_table_insert()向hash表里插入条目,插入的条目必须是一个key-value对,要查看hash表里有多少个条目可以用g_hash_table_size()。
2、用<layer style="COLOR: black; BACKGROUND-COLOR: yellow">g_hash_table_lookup()通过key可以查找到与它相对应的value,g_hash_table_replace()可以替换掉一个key对应的value。<br>3、用g_hash_table_foreach()可以遍历hash表里的每一个条目,并对它们进行相应的操作。<br>4、用g_hash_table_remove()把一个条目从hash表里删除。<br>5、用g_hash_table_destroy()销毁一个hash表。<br>6、对于用g_hash_table_new_full()创建并提供了key_destroy_func和value_destroy_func的hash 表,删除hash表中的条目或者销毁hash表的时候,库自动调用这两个函数释放内存,在销毁hash表的时候,销毁条目的顺序与插入条目的顺序并不总是相同的。代码中我们用strdup()来为hash表的key和value动态分配内存。<br><br>对glib库更多参考:<br><a href="http://developer.gnome.org/doc/API/2.0/glib/index.html"><font color="#fd83c9">http://developer.gnome.org/doc/API/2.0/glib/index.html</font></a> </layer>
要使用一个hash表首先必须创建它,glib库里有两个函数可以用于创建hash表,分别是g_hash_table_new()和g_hash_table_new_full(),它们的原型如下:
GHashTable*g_hash_table_new(GHashFunchash_func,GEqualFunckey_equal_func);
GHashTable*g_hash_table_new_full(GHashFunchash_func,
GEqualFunckey_equal_func,
GDestroyNotifykey_destroy_func,
GDestroyNotifyvalue_destroy_func);
GHashTable*g_hash_table_new_full(GHashFunchash_func,
GEqualFunckey_equal_func,
GDestroyNotifykey_destroy_func,
GDestroyNotifyvalue_destroy_func);
其中hash_func是一个函数,它为key创建一个hash值;key_equal_func用于比较两个key是否相等;
key_destroy_func当你从hash表里删除、销毁一个条目时,glib库会自动调用它释放key所占用的内存空间,
这对于key是动态分配内存的hash表来说非常有用;value_destroy_func的作用与key_destroy_func相似,
只是它释放的是value占用的内存空间。
下面以一段代码来演示glib库中hash表的基本用法
/***************************************************************************
file:g_hash.c
desc: 这个文件用于演示glib库中hash表的用法
compile:gcc-og_hashg_hash.c`pkg-config--cflags--libsglib-2.0`
***************************************************************************/
#include<glib.h>
voidprint_key_value(gpointerkey,gpointervalue,gpointeruser_data);
voiddisplay_hash_table(GHashTable*table);
voidfree_key(gpointerdata);
voidfree_value(gpointervalue);
voidprint_key_value(gpointerkey,gpointervalue,gpointeruser_data)
{
printf("%s--->%s/n",key,value);
}
voiddisplay_hash_table(GHashTable*table)
{
g_hash_table_foreach(table,print_key_value,NULL);
}
voidfree_key(gpointerdata)
{
printf("Wefreekey:%s /n",data);
free(data);
}
voidfree_value(gpointerdata)
{
printf("Wefreevalue:%s /n",data);
free(data);
}
intmain(intargc,char*argv[])
{
GHashTable*table=NULL;
table=g_hash_table_new(g_str_hash,g_str_equal);
g_hash_table_insert(table,"1","one");
g_hash_table_insert(table,"2","two");
g_hash_table_insert(table,"3","three");
g_hash_table_insert(table,"4","four");
g_hash_table_insert(table,"5","five");
display_hash_table(table);
printf("Sizeofhashtable:%d /n",g_hash_table_size(table));
printf("Beforereplace:3--->%s /n",<layer style="COLOR: black; BACKGROUND-COLOR: yellow">g_hash_table_lookup</layer>(table,"3"));
g_hash_table_replace(table,"3","third");
printf("Afterreplace:3--->%s /n",<layer style="COLOR: black; BACKGROUND-COLOR: yellow">g_hash_table_lookup</layer>(table,"3"));
g_hash_table_remove(table,"2");
display_hash_table(table);
printf("Nowsizeofhashtable:%d /n",g_hash_table_size(table));
g_hash_table_destroy(table);
table=g_hash_table_new_full(g_str_hash,g_str_equal,free_key,free_value);
g_hash_table_insert(table,strdup("one"),strdup("first"));
g_hash_table_insert(table,strdup("two"),strdup("second"));
g_hash_table_insert(table,strdup("three"),strdup("third"));
printf("Removeanitemfromhashtable: /n");
g_hash_table_remove(table,"two");
printf("Destroyhashtable: /n");
g_hash_table_destroy(table);
return0;
}
file:g_hash.c
desc: 这个文件用于演示glib库中hash表的用法
compile:gcc-og_hashg_hash.c`pkg-config--cflags--libsglib-2.0`
***************************************************************************/
#include<glib.h>
voidprint_key_value(gpointerkey,gpointervalue,gpointeruser_data);
voiddisplay_hash_table(GHashTable*table);
voidfree_key(gpointerdata);
voidfree_value(gpointervalue);
voidprint_key_value(gpointerkey,gpointervalue,gpointeruser_data)
{
printf("%s--->%s/n",key,value);
}
voiddisplay_hash_table(GHashTable*table)
{
g_hash_table_foreach(table,print_key_value,NULL);
}
voidfree_key(gpointerdata)
{
printf("Wefreekey:%s /n",data);
free(data);
}
voidfree_value(gpointerdata)
{
printf("Wefreevalue:%s /n",data);
free(data);
}
intmain(intargc,char*argv[])
{
GHashTable*table=NULL;
table=g_hash_table_new(g_str_hash,g_str_equal);
g_hash_table_insert(table,"1","one");
g_hash_table_insert(table,"2","two");
g_hash_table_insert(table,"3","three");
g_hash_table_insert(table,"4","four");
g_hash_table_insert(table,"5","five");
display_hash_table(table);
printf("Sizeofhashtable:%d /n",g_hash_table_size(table));
printf("Beforereplace:3--->%s /n",<layer style="COLOR: black; BACKGROUND-COLOR: yellow">g_hash_table_lookup</layer>(table,"3"));
g_hash_table_replace(table,"3","third");
printf("Afterreplace:3--->%s /n",<layer style="COLOR: black; BACKGROUND-COLOR: yellow">g_hash_table_lookup</layer>(table,"3"));
g_hash_table_remove(table,"2");
display_hash_table(table);
printf("Nowsizeofhashtable:%d /n",g_hash_table_size(table));
g_hash_table_destroy(table);
table=g_hash_table_new_full(g_str_hash,g_str_equal,free_key,free_value);
g_hash_table_insert(table,strdup("one"),strdup("first"));
g_hash_table_insert(table,strdup("two"),strdup("second"));
g_hash_table_insert(table,strdup("three"),strdup("third"));
printf("Removeanitemfromhashtable: /n");
g_hash_table_remove(table,"two");
printf("Destroyhashtable: /n");
g_hash_table_destroy(table);
return0;
}
通过代码我们看到glib库为hash表接供了非常简单的接口。下面是代码的输出
[plusboy@localhost c]$ ./g_hash
1 ---> one
2 ---> two
3 ---> three
4 ---> four
5 ---> five
Size of hash table: 5
Before replace: 3 ---> three
After replace: 3 ---> third
1 ---> one
3 ---> third
4 ---> four
5 ---> five
Now size of hash table: 4
Remove an item from hash table:
We free key: two
We free value: second
Destroy hash table:
We free key: three
We free value: third
We free key: one
We free value: first
对代码的说明:
1、首先我们调用了g_hash_table_new()来创建一个hash表,然后用g_hash_table_insert()向hash表里插入条目,插入的条目必须是一个key-value对,要查看hash表里有多少个条目可以用g_hash_table_size()。
2、用<layer style="COLOR: black; BACKGROUND-COLOR: yellow">g_hash_table_lookup()通过key可以查找到与它相对应的value,g_hash_table_replace()可以替换掉一个key对应的value。<br>3、用g_hash_table_foreach()可以遍历hash表里的每一个条目,并对它们进行相应的操作。<br>4、用g_hash_table_remove()把一个条目从hash表里删除。<br>5、用g_hash_table_destroy()销毁一个hash表。<br>6、对于用g_hash_table_new_full()创建并提供了key_destroy_func和value_destroy_func的hash 表,删除hash表中的条目或者销毁hash表的时候,库自动调用这两个函数释放内存,在销毁hash表的时候,销毁条目的顺序与插入条目的顺序并不总是相同的。代码中我们用strdup()来为hash表的key和value动态分配内存。<br><br>对glib库更多参考:<br><a href="http://developer.gnome.org/doc/API/2.0/glib/index.html"><font color="#fd83c9">http://developer.gnome.org/doc/API/2.0/glib/index.html</font></a> </layer>