ruan-virus's Blog

Happy coding

GLIB库 Arrays代码示例

ruan-virus posted @ 2009年8月31日 23:06 in C程序设计 with tags GArray Arrars , 2711 阅读

今天学习了glib库中的Arrays,对它的用法有了粗略了解,用代码记之备忘。

/*!
 *\file: g_array.c
 *\brief: 这个文件用于演示glib库中数组GArray的用法
 * compile:"gcc -o test test*.c `pkg-config --cflags --libs glib-2.0`"
 */


#include <glib.h>
#include<stdio.h>

/*!定义输出函数*/
void display_array(GArray *array, int len, const char *prompt)
{
        int i = 0;
        printf("%s: \n", prompt);
        for (i = 0; i < len; i++) {
                printf("%d ", g_array_index(array, int, i));
        }
         printf("\n");
}
        /*!
         *g_array_index()函数删除数组中指定位置的元素,后面的元素按顺序前移。
         *i指元素的位置,函数返回指定的类型。
         */

int main(int argc, char *argv[])
{       
        GArray *array = NULL;/*!<定义数组*/
        int i = 0;
        int cur_arr_len = 0;
        int len = 0;
        array = g_array_new(FALSE, TRUE, sizeof(int));
        /*!
         *函数g_array_new()创建一个数组,它有三个参数:
         *第一个表示是否在数组的尾部添加'\0';
         *第二个表示当分配空间给数组元素时是否要把它初始化为‘0’;
         *第三个表示数组里每个元素的长度。
         */

        len = 0;
        for (i = 10; i < 15; i++) {
                g_array_append_val(array, i);
                /*!向数组尾部加入一个元素,第二个参数必须是变量*/
               
                len++;
        }
        cur_arr_len += len;
        display_array(array, cur_arr_len, "Create array");

        int app_data[] = {30, 40, 50, 60};
        int app_len = sizeof(app_data) / sizeof(int);

        g_array_append_vals(array, app_data, app_len);
        /*!可以加入多个元素,后面的数据依次后延**/
        cur_arr_len += app_len;
        display_array(array, cur_arr_len, "After append values 30 40 50 60");

        len = 0;
        for (i = 1; i < 4; i++) {
                g_array_prepend_val(array, i);
                /*!在数组的开头加入数据,后面的数据依次后延**/
          len++;
        }
        cur_arr_len += len;
        display_array(array, cur_arr_len, "After prepend value 1 2 3");

        int prepend_data[] = {-10, -20, -30, -40};
        int prepend_len = sizeof(prepend_data) / sizeof(int);

        g_array_prepend_vals(array, prepend_data, prepend_len);
        /*!加入多个数据,后面的数据依次后延**/

        cur_arr_len += prepend_len;
        display_array(array, cur_arr_len, "After prepend values -10 -20 -30 -40");
        int data = 100;
        g_array_insert_val(array, 5, data);
        /*!在指定位置插入数据,后面的数据依次后延*/
        cur_arr_len += 1;
        display_array(array, cur_arr_len, "After insert 100 at index 5");

        g_array_remove_index(array, 4);
        /*!在指定位置删除数据,后面的依次补上*/
        cur_arr_len -= 1;
        display_array(array, cur_arr_len, "After remove value at index 4");

        g_array_remove_index_fast(array, 10);
        /*!在指定位置删除数据,数组最后一个元素补到被删的位置*/
        cur_arr_len -= 1;
        display_array(array, cur_arr_len, "After remove value at index 10 fast");

        g_array_free(array, TRUE);
}

 

 

 

 

Avatar_small
celeb networth 说:
2022年9月23日 17:38

Checking out the information of any celebrity on celeb networth post to know how much their name and fame worth and their family members.

Avatar_small
powder auger filler 说:
2023年11月09日 05:38

I've perused some well done here. Certainly worth bookmarking for returning to. I astound the amount of exertion you put to make such an extraordinary useful site.

Avatar_small
darth vader lingerie 说:
2024年1月10日 19:00

Sometimes, blogging is a bit tiresome specially if you need to update more topics.

Avatar_small
read more 说:
2024年1月17日 21:56

This is because most of the home owners left out with their untreated doors that is not only a gateway to your home but tell a lot about your lifestyle and personality


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter