GLib是从GTK中剥离出的一个通用工具库。提供了多种数据结构、字符串处理、正则表达式等工具。
GList是一个链表,相当于Java中的LinkedList。示例用法如下:
#include <stdio.h>
#include <glib.h>
int main() {
GList *list = g_list_alloc();
list = g_list_append(list, "some");
list = g_list_append(list, "beta");
list = g_list_append(list, "gamma");
for (GList *cursor = list->next; cursor != NULL; cursor = cursor->next) {
gpointer element = cursor->data;
printf("Element = %s\n", (char *) element);
}
return 0;
}
Element = some
Element = beta
Element = gamma
GHashTable类似于Java中的HashMap,使用示例如下:
#include <stdio.h>
#include <glib.h>
int main() {
GHashTable *table = g_hash_table_new(g_str_hash, g_str_equal);
g_hash_table_insert(table, "USA", "United States");
g_hash_table_insert(table, "JP", "Japan");
GHashTableIter iter;
g_hash_table_iter_init(&iter, table);
gchar *key, *value;
while (g_hash_table_iter_next(&iter, (gpointer *) &key, (gpointer *) &value)) {
printf("%s=>%s\n", key, value);
}
return 0;
}
JP=>Japan
USA=>United States