ruan-virus's Blog

Happy coding

GLIB库 Arrays代码示例

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

今天学习了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

Avatar_small
tree trimming 说:
2024年6月12日 18:36

For people who are experts at using the hedge trimmer, the double-sided one can make trimming faster because it cuts either side at once.

Avatar_small
View more details 说:
2024年7月28日 04:25

My husband and i arrived here since this post had been tweeted by a individual I was following and am extremely pleased I made it here.

Avatar_small
best casino bonuses 说:
2024年9月14日 22:05

we both have those traditional picture frames and digital picture frames at home. both are great for displaying family pictures,,

Avatar_small
jasa pbn 说:
2025年5月14日 15:50
Loving the info on this web site , you have done great job on the blog posts.
 
Avatar_small
jasa pbn 说:
2025年5月14日 15:51
Loving the info on this web site , you have done great job on the blog posts.

 

 
Avatar_small
Visit our official w 说:
2025年5月14日 17:50

For fraud updates and scam alerts, I strongly advise visiting Visit our official website if you are serious about staying away from online betting scams. Additionally, I check the Toto platforms I use using antifraudcentre-centreantifraude. My betting experience has been safe and secure thanks to these two products working together.

Avatar_small
Find contact informa 说:
2025年5月14日 18:43

How can you be safe when placing bets online? I utilize Find contact information here to confirm Toto platforms and europeangaming for scam updates. Using this technique has protected me from phony websites. What other resources or methods people employ for secure online betting would be greatly appreciated.


登录 *


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