先上说明文档网址:http://web.mit.edu/barnowl/share/gtk-doc/html/glib/glib-Pointer-Arrays.html
没有什么可以多说的,因为他和动态数组太像了,如果已经会使用动态数组的话学习这个数据类型就没有一点压力。
结构体定义:
typedef struct { gpointer *pdata; guint len; } GPtrArray;
功能函数:
#include <glib.h> GPtrArray; GPtrArray* g_ptr_array_new (void); GPtrArray* g_ptr_array_sized_new (guint reserved_size); void g_ptr_array_add (GPtrArray *array, gpointer data); gboolean g_ptr_array_remove (GPtrArray *array, gpointer data); gpointer g_ptr_array_remove_index (GPtrArray *array, guint index_); gboolean g_ptr_array_remove_fast (GPtrArray *array, gpointer data); gpointer g_ptr_array_remove_index_fast (GPtrArray *array, guint index_); void g_ptr_array_remove_range (GPtrArray *array, guint index_, guint length); void g_ptr_array_sort (GPtrArray *array, GCompareFunc compare_func); void g_ptr_array_sort_with_data (GPtrArray *array, GCompareDataFunc compare_func, gpointer user_data); void g_ptr_array_set_size (GPtrArray *array, gint length); #define g_ptr_array_index (array,index_) gpointer* g_ptr_array_free (GPtrArray *array, gboolean free_seg); void g_ptr_array_foreach (GPtrArray *array, GFunc func, gpointer user_data);
直接上一小段测试代码,也是从动态数组的代码中修改过来的,基本一致
#include <glib.h> struct map { int key; char *value; } m[10] = { {1,"one"}, {2,"two"}, {3,"three"}, {4,"four"}, {5,"five"}, {6,"six"}, {7,"seven"}, {8,"eight"}, {9,"nine"}, {10,"ten"} }; typedef struct map map; static gint sort(gconstpointer p1, gconstpointer p2) { char * a, *b; a = *(char **)(p1); b = *(char **)(p2); return (*a > *b ? +1 : *a == *b ? 0 : -1); } static gint sort_r(gconstpointer p1, gconstpointer p2, gpointer user_data) { char * a, *b; a = *(char **)(p1); b = *(char **)(p2); return (*a < *b ? +1 : *a == *b ? 0 : -1); } static void print(gpointer data, gpointer user_data) { g_printf("%s,", data); g_printf("\n"); } static void test_array(void) { GPtrArray *gparray = NULL; gint i; // GPtrArray* g_ptr_array_new(); 创建动态指针数组指针 gparray = g_ptr_array_new (); // void g_ptr_array_add(GPtrArray *array,gpointer data); 添加data到数组array的后面 for (i = 0; i < sizeof(m)/sizeof(m[0]); i++) g_ptr_array_add(gparray, (gpointer)m[i].value); g_printf("There should be '%d' items now.\t\tResult: %d.\n", 10, gparray->len); g_printf("All of items:\n"); // #define g_ptr_array_index(array,index_) 返回数组array中第index_个的值 for (i = 0; i < sizeof(m)/sizeof(m[0]); i++) g_printf("%s,", g_ptr_array_index(gparray, i)); g_printf("\n"); // gpointer g_ptr_array_remove_index(GPtrArray *array, guint index_); 删除数组中的第index_值,并将后面的元素向前移动,对应的g_ptr_array_remove_index_fast函数的删除元素,然后使用最后一个元素填充,其他不移动,这样会执行的很快 g_ptr_array_remove_index(gparray, 1); g_printf("All of items[exclude the second item]:\n"); for (i = 0; i < sizeof(m)/sizeof(m[0])-1; i++) g_printf("%s,", g_ptr_array_index(gparray, i)); g_printf("\n"); // gpointer g_ptr_array_remove_index_fast(GPtrArray *array, guint index_); 删除元素,然后使用最后一个元素填充,其他不移动 g_ptr_array_remove_index_fast(gparray, 1); g_printf("All of items[exclude the second item]:\n"); for (i = 0; i < sizeof(m)/sizeof(m[0])-1-1; i++) g_printf("%s,", g_ptr_array_index(gparray, i)); g_printf("\n"); // void g_ptr_array_remove_range(GPtrArray *array, guint index_); 删除数组中从第index_开始的length多个元素,后面的元素被顺序下移 g_ptr_array_remove_range(gparray, 2, 2); g_printf("All of items[after remove 2 items from the third item]:\n"); for (i = 0; i < sizeof(m)/sizeof(m[0])-1-1-2; i++) g_printf("%s,", g_ptr_array_index(gparray, i)); g_printf("\n"); // void g_ptr_array_sort(GPtrArray *array, GCompareFunc compare_func); 通过compare_func函数规则进行排序 g_ptr_array_sort(gparray, sort); g_printf("All of items[sorted]:\n"); for (i = 0; i < sizeof(m)/sizeof(m[0])-1-1-2; i++) g_printf("%s,", g_ptr_array_index(gparray, i)); g_printf("\n"); // void g_ptr_array_sort_with_data(GPtrArray *array, GCompareDataFunc compare_func, gpointer user_data); compare_func函数带输入参数 user_data的排序 g_ptr_array_sort_with_data(gparray, sort_r, NULL); g_printf("All of items[sorted reversed]:\n"); for (i = 0; i < sizeof(m)/sizeof(m[0])-1-1-2; i++) g_printf("%s,", g_ptr_array_index(gparray, i)); g_printf("\n"); //void g_ptr_array_foreach(GPtrArray *array,GFunc func,gpointer user_data); 遍历数组,将数据元素传递给func g_ptr_array_foreach (gparray, print, (gpointer) NULL); // gchar* g_ptr_array_free(GPtrArray *array, gboolean free_segment); 释放动态数组,如果free_segment是true会同时把数据也释放掉 g_ptr_array_free(gparray, TRUE); } int main(void) { g_printf("BEGIN:\n************************************************************\n"); test_array(); g_printf("\n************************************************************\nDONE\n"); return 0; }
linux@ubuntu:~/16021/glibdemo$ gcc Point_Array.c -o Point_Array -lglib-2.0 linux@ubuntu:~/16021/glibdemo$ ./Point_Array BEGIN: ************************************************************ There should be '10' items now. Result: 10. All of items: one,two,three,four,five,six,seven,eight,nine,ten, All of items[exclude the second item]: one,three,four,five,six,seven,eight,nine,ten, All of items[exclude the second item]: one,ten,four,five,six,seven,eight,nine, All of items[after remove 2 items from the third item]: one,ten,six,seven,eight,nine, All of items[sorted]: eight,nine,one,six,seven,ten, All of items[sorted reversed]: ten,six,seven,one,nine,eight, ten, six, seven, one, nine, eight, ************************************************************ DONE linux@ubuntu:~/16021/glibdemo$