zoukankan      html  css  js  c++  java
  • Linux-c glib库hash表GHashTable介绍

     

    百度云glib  链接:https://pan.baidu.com/s/1W9qdlMKWRKIFykenTVuWNQ 密码:ol6y

    hash表是一种提供key-value访问的数据结构,通过指定的key值可以快速的访问到与它相关联的value值。hash表的一种典型用法就是字典,通过单词的首字母能够快速的找到单词。关于hash表的详细介绍请查阅数据结构的相关书籍,我这里只介绍glib库中hash表的基本用法。

    要使用一个hash表首先必须创建它,glib库里有两个函数可以用于创建hash表,分别是g_hash_table_new()和g_hash_table_new_full(),它们的原型如下:

    1 GHashTable *g_hash_table_new(GHashFunc hash_func, GEqualFunc key_equal_func);
    2 
    3 GHashTable* g_hash_table_new_full(GHashFunc hash_func,
    4                                    GEqualFunc key_equal_func,
    5                                    GDestroyNotify key_destroy_func,
    6                                    GDestroyNotify value_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表的基本用法 

     1 /***************************************************************************
     2    file: g_hash.c
     3    desc: 这个文件用于演示glib库中hash表的用法
     4    compile: gcc -o g_hash g_hash.c `pkg-config --cflags --libs glib-2.0`
     5  ***************************************************************************/
     6 
     7 #include <glib.h>
     8 
     9 void print_key_value(gpointer key, gpointer value, gpointer user_data);
    10 void display_hash_table(GHashTable *table);
    11 void free_key(gpointer data);
    12 void free_value(gpointer value);
    13 
    14 void print_key_value(gpointer key, gpointer value, gpointer user_data)
    15 {
    16     printf("%s ---> %s/n", key, value);
    17 }
    18 
    19 void display_hash_table(GHashTable *table)
    20 {
    21     g_hash_table_foreach(table, print_key_value, NULL);
    22 }
    23 
    24 void free_key(gpointer data)
    25 {
    26     printf("We free key: %s /n", data);
    27     free(data);
    28 }
    29 
    30 void free_value(gpointer data)
    31 {
    32     printf("We free value: %s /n", data);
    33     free(data);
    34 }
    35 
    36 int main(int argc, char *argv[])
    37 {
    38     GHashTable *table = NULL;
    39 
    40     table = g_hash_table_new(g_str_hash, g_str_equal);
    41 
    42     g_hash_table_insert(table, "1", "one");
    43     g_hash_table_insert(table, "2", "two");
    44     g_hash_table_insert(table, "3", "three");
    45     g_hash_table_insert(table, "4", "four");
    46     g_hash_table_insert(table, "5", "five");
    47     display_hash_table(table);
    48     printf("Size of hash table: %d /n", g_hash_table_size(table));
    49 
    50     printf("Before replace: 3 ---> %s /n", g_hash_table_lookup(table, "3"));
    51     g_hash_table_replace(table, "3", "third");
    52     printf("After replace: 3 ---> %s /n", g_hash_table_lookup(table, "3"));
    53 
    54     g_hash_table_remove(table, "2");
    55     display_hash_table(table);
    56     printf("Now size of hash table: %d /n", g_hash_table_size(table));
    57 
    58     g_hash_table_destroy(table);
    59 
    60     table = g_hash_table_new_full(g_str_hash, g_str_equal, free_key, free_value);
    61     g_hash_table_insert(table, strdup("one"), strdup("first"));
    62     g_hash_table_insert(table, strdup("two"), strdup("second"));
    63     g_hash_table_insert(table, strdup("three"), strdup("third"));
    64     
    65     printf("Remove an item from hash table: /n");
    66     g_hash_table_remove(table, "two");
    67 
    68     printf("Destroy hash table: /n");
    69     g_hash_table_destroy(table);
    70 
    71     return 0;
    72 }

    通过代码我们看到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、用g_hash_table_lookup()通过key可以查找到与它相对应的value,g_hash_table_replace()可以替换掉一个key对应的value。
    3、用g_hash_table_foreach()可以遍历hash表里的每一个条目,并对它们进行相应的操作。
    4、用g_hash_table_remove()把一个条目从hash表里删除。
    5、用g_hash_table_destroy()销毁一个hash表。
    6、对于用g_hash_table_new_full()创建并提供了key_destroy_func和value_destroy_func的hash表,删除hash表中的条目或者销毁hash表的时候,库自动调用这两个函数释放内存,在销毁hash表的时候,销毁条目的顺序与插入条目的顺序并不总是相同的。代码中我们用strdup()来为hash表的key和value动态分配内

  • 相关阅读:
    C#
    if
    .net 5.0
    C# 未提供必须形参对应的实参
    EasyRTC报错 “[ERR] mod_local_stream.c:880 Unknown source default”排查
    EasyRTC通过公网进入会议室失败问题排查及解决
    【CF1558B】Up the Strip
    【AT Keyence2019E】Connecting Cities
    【洛谷P7470】岛屿探险
    【洛谷P6628】丁香之路
  • 原文地址:https://www.cnblogs.com/renweihang/p/8931117.html
Copyright © 2011-2022 走看看