首页 > 其他分享 >【glibc】glib 数组

【glibc】glib 数组

时间:2024-01-22 10:56:42浏览次数:24  
标签:glib FALSE int glibc 数组 printf array Array first

编译: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

更多 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

添加数据的更多方法

到目前为止您已经看到如何使用 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

插入数据

也可以向数组中各个不同的位置插入数据;不是限于只能附加或者向最前添加条目。这里是其工作方式:

#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

删除数据

有三种方法可以从 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

排序

对 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

指针数组   有错误需要调试

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 '

字节数组

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

 

标签:glib,FALSE,int,glibc,数组,printf,array,Array,first
From: https://www.cnblogs.com/opensmarty/p/17979538

相关文章

  • 【glibc】glib库双向链表GList介绍
    在上一篇文章里我介绍了glib库中单向链表的用法,这篇文章介绍glib库双向链表的用法,还是沿用上一篇文章的风格,采用在代码中加入注释来说明代码,最后贴出程序的运行结果,然后加以少量说明。双向链表与单向链表的区别是,从一个节点,不仅能访问到它的下一个节点,还能访问到它的上一个节点,其......
  • 【glibc】glib库数组GArray介绍
    glib库中的数组GArray类型很像C++标准容器库中的vector容器。要使用glib库中的数组中需要声明一个指向GArray类型的指针。GArray的定义如下:structGArray{gchar*data;guintlen;};然后就可以在这个数组前或者数组后添加数据,添加数据的时候数组也会像C++中的vector容器......
  • 【glibc】glib库hash表GHashTable介绍
    hash表是一种提供key-value访问的数据结构,通过指定的key值可以快速的访问到与它相关联的value值。hash表的一种典型用法就是字典,通过单词的首字母能够快速的找到单词。关于hash表的详细介绍请查阅数据结构的相关书籍,我这里只介绍glib库中hash表的基本用法。要使用一个hash表首先必......
  • 【glibc】glib库队列GQueue介绍
    队列是一种向最后添加条目,从最前删除条目的数据结构,这种数据结构在处理按顺序到达的数据是很有用。glib库提供的队列GQueue是一个双端队列,它的实现基础是双向链表,所以它支持在队列的两端进行添加和删除,也支持很多其它的操作,比如在队列中进行插入和删除,但是我不推荐使用这样的功能......
  • Go语言核心36讲 07 | 数组和切片
    从本篇文章开始,我们正式进入了模块2的学习。在这之前,我们已经聊了很多的Go语言和编程方面的基础知识,相信你已经对Go语言的开发环境配置、常用源码文件写法,以及程序实体(尤其是变量)及其相关的各种概念和编程技巧(比如类型推断、变量重声明、可重名变量、类型断言、类型转换、别名类......
  • 关于Java 数组
    了解Java数组Java中的数组是一种强大而灵活的数据结构,让我们一起深入探讨它的方方面面,从基础的概念到高级的应用。1.数组的创建与初始化首先,我们来看如何创建和初始化一个简单的整型数组:publicclassArrayExample{publicstaticvoidmain(String[]args){......
  • C++ ——vector数组笔记
     vector是C++标准库中的一个动态数组容器(SequenceContainer),它可以自动管理内存大小,可以在运行时根据需要动态增长或缩小。它是一个非常常用且强大的容器,用于存储一系列元素。可以简单的认为,vector是一个能够放任意类型的动态数组。下面详细介绍 vector 的使用方法......
  • 每日一题 2024-1-21 分割数组的最大值
    1.题目(困难)原题链接给定一个非负整数数组\(nums\)和一个整数\(k\),你需要将这个数组分成\(k\)个非空的连续子数组。设计一个算法使得这\(k\)个子数组各自和的最大值最小。示例1:输入:nums=[7,2,5,10,8],k=2输出:18解释:一共有四种方法将nums分割为2个子数组......
  • Java将字符串转为list数组
    将字符串转为list数组的实现方法概述在Java开发中,有时候我们需要将一个字符串转换为一个列表数组,以便对其中的元素进行操作和处理。本文将介绍一种常见的实现方法,并提供详细的步骤和示例代码来帮助你完成这个任务。实现步骤下面是实现将字符串转为list数组的一般步骤,你可以按照......
  • Java将json字符串转换为数组的方法
    Java将json字符串转换为数组的方法在Java开发中,经常会遇到将json字符串转换为数组的需求。JSON(JavaScriptObjectNotation)是一种轻量级的数据交换格式,常用于前后端数据传输和存储。而Java中的JSONArray类可以用来处理json数组。下面将介绍一种常用的方法,用于将json字符串转换为......