glib 数组
原文地址: http://hi.baidu.com/study_together/blog/item/7440fe81f66265af0df4d23f.html
编译:gcc -g -Wall -O0 fuck.c -o fuck `pkg-config --libs --cflags glib-2.0`
1
基本操作
这里是向数组添加和删除数据的一些主要方法:
#include <glib.h>
#include <stdio.h>
int main(int argc, char** argv) {
GArray* a = g_array_new(FALSE, FALSE, sizeof(char*));
char* first = "hello", *second = "there", *third = "world";
g_array_append_val(a, first);
g_array_append_val(a, second);
g_array_append_val(a, third);
printf("There are now %d items in the array\n", a->len);
printf("The first item is '%s'\n", g_array_index(a, char*, 0));
printf("The third item is '%s'\n", g_array_index(a, char*, 2));
g_array_remove_index(a, 1);
printf("There are now %d items in the array\n", a->len);
g_array_free(a, FALSE);
return 0;
}***** Output *****
There are now 3 items in the array
The first item is 'hello'
The third item is 'world'
There are now 2 items in the array
2
更多 new/free 选项
本示例中包含创建和销毁 GArray 的一些不同方法:
#include <glib.h>
#include <stdio.h>
int main(int argc, char** argv) {
GArray* a = g_array_sized_new(TRUE, TRUE, sizeof(int), 16);
printf("Array preallocation is hidden, so array size == %d\n", a->len);
printf("Array was init'd to zeros, so 3rd item is = %d\n", g_array_index(a, int, 2));
g_array_free(a, FALSE);
// this creates an empty array, then resizes it to 16 elements
a = g_array_new(FALSE, FALSE, sizeof(char));
g_array_set_size(a, 16);
g_array_free(a, FALSE);
a = g_array_new(FALSE, FALSE, sizeof(char));
char* x = g_strdup("hello world");
g_array_append_val(a, x);
g_array_free(a, TRUE);
return 0;
}***** Output *****
Array preallocation is hidden, so array size == 0
Array was init'd to zeros, so 3rd item is = 0
3
添加数据的更多方法
到目前为止您已经看到如何使用 g_array_append_val 将数据添加到数组。不过,有其他的方式可以将数据置入数组,如下所示:
#include <glib.h>
#include <stdio.h>
void prt(GArray* a) {
printf("Array holds: ");
int i;
for (i = 0; i < a->len; i++)
printf("%d ", g_array_index(a, int, i));
printf("\n");
}
int main(int argc, char** argv) {
GArray* a = g_array_new(FALSE, FALSE, sizeof(int));
printf("Array is empty, so appending some values\n");
int x[2] = {4,5};
g_array_append_vals(a, &x, 2);
prt(a);
printf("Now to prepend some values\n");
int y[2] = {2,3};
g_array_prepend_vals(a, &y, 2);
prt(a);
printf("And one more prepend\n");
int z = 1;
g_array_prepend_val(a, z);
prt(a);
g_array_free(a, FALSE);
return 0;
}***** Output *****
Array is empty, so appending some values
Array holds: 4 5
Now to prepend some values
Array holds: 2 3 4 5
And one more prepend
Array holds: 1 2 3 4 5
4
插入数据
也可以向数组中各个不同的位置插入数据;不是限于只能附加或者向最前添加条目。这里是其工作方式:
#include <glib.h>
#include <stdio.h>
void prt(GArray* a) {
printf("Array holds: ");
int i;
for (i = 0; i < a->len; i++)
printf("%d ", g_array_index(a, int, i));
printf("\n");
}
int main(int argc, char** argv) {
GArray* a = g_array_new(FALSE, FALSE, sizeof(int));
int x[2] = {1,5};
g_array_append_vals(a, &x, 2);
prt(a);
printf("Inserting a '2'\n");
int b = 2;
g_array_insert_val(a, 1, b);
prt(a);
printf("Inserting multiple values\n");
int y[2] = {3,4};
g_array_insert_vals(a, 2, y, 2);
prt(a);
g_array_free(a, FALSE);
return 0;
}***** Output *****
Array holds: 1 5
Inserting a '2'
Array holds: 1 2 5
Inserting multiple values
Array holds: 1 2 3 4 5
5
删除数据
有三种方法可以从 GArray 删除数据:
* g_array_remove_index 和 g_array_remove_range,这两个函数会保持现有顺序
* g_array_remove_index_fast,不保持现有顺序
这里是所有三种方法的示例:
#include <glib.h>
#include <stdio.h>
void prt(GArray* a) {
int i;
printf("Array holds: ");
for (i = 0; i < a->len; i++)
printf("%d ", g_array_index(a, int, i));
printf("\n");
}
int main(int argc, char** argv) {
GArray* a = g_array_new(FALSE, FALSE, sizeof(int));
int x[6] = {1,2,3,4,5,6};
g_array_append_vals(a, &x, 6);
prt(a);
printf("Removing the first item\n");
g_array_remove_index(a, 0);
prt(a);
printf("Removing the first two items\n");
g_array_remove_range(a, 0, 2);
prt(a);
printf("Removing the first item very quickly\n");
g_array_remove_index_fast(a, 0);
prt(a);
g_array_free(a, FALSE);
return 0;
}***** Output *****
Array holds: 1 2 3 4 5 6
Removing the first item
Array holds: 2 3 4 5 6
Removing the first two items
Array holds: 4 5 6
Removing the first item very quickly
Array holds: 6 5
6
排序
对 GArray 排序很直观;它使用的是在 GList 和 GSList 部分已经出现过的 GCompareFunc:
#include <glib.h>
#include <stdio.h>
void prt(GArray* a) {
int i;
printf("Array holds: ");
for (i = 0; i < a->len; i++)
printf("%d ", g_array_index(a, int, i));
printf("\n");
}
int compare_ints(gpointer a, gpointer b) {
int* x = (int*)a;
int* y = (int*)b;
return *x - *y;
}
int main(int argc, char** argv) {
GArray* a = g_array_new(FALSE, FALSE, sizeof(int));
int x[6] = {2,1,6,5,4,3};
g_array_append_vals(a, &x, 6);
prt(a);
printf("Sorting\n");
g_array_sort(a, (GCompareFunc)compare_ints);
prt(a);
g_array_free(a, FALSE);
return 0;
}***** Output *****
Array holds: 2 1 6 5 4 3
Sorting
Array holds: 1 2 3 4 5 6
7
指针数组 有错误需要调试
GLib 还提供了 GPtrArray,这是一个为保存指针专门设计的数组。使用它比使用基本的 GArray 更简单,因为在创建或者添加、
索引元素时不需要指定具体类型。它与 GArray 非常类似,所以我们将只是回顾基本操作的一些示例:
#include <glib.h>
#include <stdio.h>
int main(int argc, char** argv) {
GPtrArray* a = g_ptr_array_new();
g_ptr_array_add(a, g_strdup("hello "));
g_ptr_array_add(a, g_strdup("again "));
g_ptr_array_add(a, g_strdup("there "));
g_ptr_array_add(a, g_strdup("world "));
g_ptr_array_add(a, g_strdup("\n"));
printf(">Here are the GPtrArray contents\n");
g_ptr_array_foreach(a, (GFunc)printf, NULL);
printf(">Removing the third item\n");
g_ptr_array_remove_index(a, 2);
g_ptr_array_foreach(a, (GFunc)printf, NULL);
printf(">Removing the second and third item\n");
g_ptr_array_remove_range(a, 1, 2);
g_ptr_array_foreach(a, (GFunc)printf, NULL);
printf("The first item is '%s'\n", g_ptr_array_index(a, 0));
g_ptr_array_free(a, TRUE);
return 0;
}***** Output *****
>Here are the GPtrArray contents
hello again there world
>Removing the third item
hello again world
>Removing the second and third item
hello
The first item is 'hello '
8
字节数组
GLib 提供了另一个特定类型的数组是 GByteArray。它与您已经了解的类型非常类似,不过有一些区别,因为它是为存储二进制数据而设计的。
它非常便于在循环中读取二进制数据,因为它隐藏了“read into a buffer-resize buffer-read some more”的周期。这里是一些示例代码:
#include <glib.h>
#include <stdio.h>
int main(int argc, char** argv) {
GByteArray* a = g_byte_array_new();
guint8 x = 0xFF;
g_byte_array_append(a, &x, sizeof(x));
printf("The first byte value (in decimal) is %d\n", a->data[0]);
x = 0x01;
g_byte_array_prepend(a, &x, sizeof(x));
printf("After prepending, the first value is %d\n", a->data[0]);
g_byte_array_remove_index(a, 0);
printf("After removal, the first value is again %d\n", a->data[0]);
g_byte_array_append(g_byte_array_append(a, &x, sizeof(x)), &x, sizeof(x));
printf("After two appends, array length is %d\n", a->len);
g_byte_array_free(a, TRUE);
return 0;
}***** Output *****
The first byte value (in decimal) is 255
After prepending, the first value is 1
After removal, the first value is again 255
After two appends, array length is 3
完