Glib 인 스 턴 스 학습 (4) 동적 배열
1: GArray 는 임의의 유형의 요 소 를 저장 할 수 있 고 크기 는 요소 가 증가 함 에 따라 자동 으로 증가 할 수 있 습 니 다.2: GArray 구조
- typedef struct {
- gchar *data;
- guint len;
- } GArray;
3:GArray
- GArray* g_array_new (gboolean zero_terminated,
- gboolean clear_,
- guint element_size);
- GArray* g_array_sized_new (gboolean zero_terminated,
- gboolean clear_,
- guint element_size,
- guint reserved_size);
- #define g_array_append_val (a,v)
- GArray* g_array_append_vals (GArray *array,
- gconstpointer data,
- guint len);
- #define g_array_prepend_val (a,v)
- GArray* g_array_prepend_vals (GArray *array,
- gconstpointer data,
- guint len);
- #define g_array_insert_val (a,i,v)
- GArray* g_array_insert_vals (GArray *array,
- guint index_,
- gconstpointer data,
- guint len);
- GArray* g_array_remove_index (GArray *array,
- guint index_);
- GArray* g_array_remove_index_fast (GArray *array,
- guint index_);
- GArray* g_array_remove_range (GArray *array,
- guint index_,
- guint length);
- void g_array_sort (GArray *array,
- GCompareFunc compare_func);
- void g_array_sort_with_data (GArray *array,
- GCompareDataFunc compare_func,
- gpointer user_data);
- #define g_array_index (a,t,i)
- GArray* g_array_set_size (GArray *array,
- guint length);
- gchar* g_array_free (GArray *array,
- gboolean free_segment);
4:GArray
- #include <stdio.h>
-
- #include <glib.h>
- #include <glib/gprintf.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)
- {
- gint32 a, b;
-
- a = *(gint*)(p1);
- b = *(gint*)(p2);
-
- return (a > b ? +1 : a == b ? 0 : -1);
- }
-
- static gint
- sort_r(gconstpointer p1, gconstpointer p2, gpointer user_data)
- {
- gint32 a, b;
-
- a = *(gint*)(p1);
- b = *(gint*)(p2);
-
- return (a < b ? +1 : a == b ? 0 : -1);
- }
-
- static void
- print(GArray *array)
- {
- gint i;
- for (i = 0; i < array->len; i++)
- g_printf("%d,", g_array_index(array, int, i));
- g_printf("
");
- }
-
- static void
- test_array(void)
- {
- GArray *array = NULL;
- gint i;
-
- // GArray* g_array_new(gboolean zero_terminated, gboolean clear_, guint element_size);
- array = g_array_new(FALSE, FALSE, sizeof(int));
- // #define g_array_append_val(a,v)
- for (i = 0; i < sizeof(m)/sizeof(m[0]); i++)
- g_array_append_val(array, m[i].key);
-
- g_printf("There should be '%d' items now.\t\tResult: %d.
", 10, array->len);
- g_printf("All of items:
");
- // #define g_array_index(a, t, i)
- for (i = 0; i < sizeof(m)/sizeof(m[0]); i++)
- g_printf("%d,", g_array_index(array, int, i));
- g_printf("
");
-
- // GArray* g_array_remove_index(GArray *array, guint index_);
- array = g_array_remove_index(array, 1);
- g_printf("All of items[exclude the second item]:
");
- for (i = 0; i < sizeof(m)/sizeof(m[0])-1; i++)
- g_printf("%d,", g_array_index(array, int, i));
- g_printf("
");
-
- // GArray* g_array_remove_index_fast(GArray *array, guint index_);
- array = g_array_remove_index_fast(array, 1);
- g_printf("All of items[exclude the second item]:
");
- for (i = 0; i < sizeof(m)/sizeof(m[0])-1-1; i++)
- g_printf("%d,", g_array_index(array, int, i));
- g_printf("
");
-
- // GArray* g_array_remove_range(GArray *array, guint index_, guint length);
- array = g_array_remove_range(array, 2, 2);
- g_printf("All of items[after remove 2 items from the third item]:
");
- for (i = 0; i < sizeof(m)/sizeof(m[0])-1-1-2; i++)
- g_printf("%d,", g_array_index(array, int, i));
- g_printf("
");
-
- // void g_array_sort(GArray *array, GCompareFunc compare_func);
- g_array_sort(array, sort);
- g_printf("All of items[sorted]:
");
- for (i = 0; i < sizeof(m)/sizeof(m[0])-1-1-2; i++)
- g_printf("%d,", g_array_index(array, int, i));
- g_printf("
");
-
- // void g_array_sort_with_data(GArray *array, GCompareDataFunc compare_func, gpointer user_data);
- g_array_sort_with_data(array, sort_r, NULL);
- g_printf("All of items[sorted reversed]:
");
- for (i = 0; i < sizeof(m)/sizeof(m[0])-1-1-2; i++)
- g_printf("%d,", g_array_index(array, int, i));
- g_printf("
");
-
- int x[4] = {2,3,4,5};
- // GArray* g_array_append_vals(GArray *array, gconstpointer data, guint len);
- array = g_array_append_vals(array, x, 4);
- g_printf("All of items[after append all 2,3,4,5]:
");
- for (i = 0; i < array->len; i++)
- g_printf("%d,", g_array_index(array, int, i));
- g_printf("
");
-
- g_printf("All of items[after prepend one by one 2,3,4,5]:
");
- // #define g_array_prepend_val(a,v)
- for (i = 0; i < 4; i++)
- g_array_prepend_val(array, x[i]);
- print(array);
-
- g_printf("All of items[after prepend all 2,3,4,5]:
");
- // GArray* g_array_prepend_vals(GArray *array, gconstpointer data, guint len);
- array = g_array_prepend_vals(array, x, 4);
- print(array);
-
- int t = 0;
- // #define g_array_insert_val(a, i, v)
- g_array_insert_val(array, 0, t);
- g_printf("All of items[after insert 0 at the first index]:
");
- print(array);
-
- g_array_sort(array, sort);
- g_printf("All of items[sorted]:
");
- print(array);
-
- // gchar* g_array_free(GArray *array, gboolean free_segment);
- g_array_free(array, TRUE);
- }
-
- int
- main(void)
- {
- printf("BEGIN:
************************************************************
");
- test_array();
- printf("
************************************************************
DONE
");
-
- return 0;
- }
5:
- BEGIN:
- ************************************************************
- There should be '10' items now. Result: 10.
- All of items:
- 1,2,3,4,5,6,7,8,9,10,
- All of items[exclude the second item]:
- 1,3,4,5,6,7,8,9,10,
- All of items[exclude the second item]:
- 1,10,4,5,6,7,8,9,
- All of items[after remove 2 items from the third item]:
- 1,10,6,7,8,9,
- All of items[sorted]:
- 1,6,7,8,9,10,
- All of items[sorted reversed]:
- 10,9,8,7,6,1,
- All of items[after append all 2,3,4,5]:
- 10,9,8,7,6,1,2,3,4,5,
- All of items[after prepend one by one 2,3,4,5]:
- 5,4,3,2,10,9,8,7,6,1,2,3,4,5,
- All of items[after prepend all 2,3,4,5]:
- 2,3,4,5,5,4,3,2,10,9,8,7,6,1,2,3,4,5,
- All of items[after insert 0 at the first index]:
- 0,2,3,4,5,5,4,3,2,10,9,8,7,6,1,2,3,4,5,
- All of items[sorted]:
- 0,1,2,2,2,3,3,3,4,4,4,5,5,5,6,7,8,9,10,
-
- ************************************************************
- DONE
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
양식 제출 후 제출 버튼 비활성화텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.