使用 g_array_sort 函数

发布于 2024-10-26 21:01:41 字数 125 浏览 3 评论 0原文

我需要使用函数 g_array_sort(GArray *array, GCompareFunc *func) 但我不理解第二个参数。

请告诉我应该如何调用它,如果可能的话请附上示例......

I need to use the function g_array_sort(GArray *array, GCompareFunc *func) but I do not understand the second parameter.

Please show me how it should be called and if possible please attach a sample example....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

她如夕阳 2024-11-02 21:01:41

g_array_sort()的第二个参数是一个指向函数的指针。如果您查看 GCompareFunc 的文档,您会发现将看到它是一个带有两个指针并返回一个 int 的函数:

gint (*GCompareFunc) (gconstpointer a, gconstpointer b);

文档还告诉您这个函数应该做什么:

如果第一个值出现在第二个值之前,则该函数应返回负整数;如果它们相等,则返回 0;如果第一个值出现在第二个值之后,则该函数应返回正整数。

由于您没有指定要在数组中存储的数据类型,因此我将仅使用字符串。您的排序函数看起来像这样:

int my_string_sort_function (gconstpointer a, gconstpointer b)
{
    char *str_a = (char *)a;
    char *str_b = (char *)b;

    return strcmp (str_a, str_b);
}

如果您在数组中存储数字,您可以执行类似的操作

int my_int_sort_function (gconstpointer a, gconstpointer b)
{
    int int_a = GPOINTER_TO_INT (a);
    int int_b = GPOINTER_TO_INT (b);

    return int_a - int_b;
}

将这些函数与g_array_sort一起使用

g_array_sort (my_array, my_int_sort_function);

The second argument of g_array_sort () is a pointer to a function. If you look at the documentation for GCompareFunc you will see that it is a function that takes two pointers and returns an int:

gint (*GCompareFunc) (gconstpointer a, gconstpointer b);

The documentation also tells you what this function should do:

The function should return a negative integer if the first value comes before the second, 0 if they are equal, or a positive integer if the first value comes after the second.

As you didn't specify what datatypes you're storing in your array, I'll just go with strings. Your sort function would look something like this:

int my_string_sort_function (gconstpointer a, gconstpointer b)
{
    char *str_a = (char *)a;
    char *str_b = (char *)b;

    return strcmp (str_a, str_b);
}

If you were storing numbers in your array you could do something like

int my_int_sort_function (gconstpointer a, gconstpointer b)
{
    int int_a = GPOINTER_TO_INT (a);
    int int_b = GPOINTER_TO_INT (b);

    return int_a - int_b;
}

To use these functions with g_array_sort

g_array_sort (my_array, my_int_sort_function);
伏妖词 2024-11-02 21:01:41

这是一个开源库,因此您可以查看 g_array_sort() 本身的代码。只需在 Google 代码搜索中输入 g_array_sort 即可获得代码。

在那里你可以看到这个函数实际上调用了 libc 的 qsort(3) 并将你感兴趣的函数原封不动地传递给 qsort。

现在,Linux 的 qsort 手册页 提供了一个很好的 qsort 使用示例。

This is an open source library, so you could take a look at the code of g_array_sort() itself. Just type in g_array_sort into Google's code search and you'll get the code.

There you can see that this function actually calls libc's qsort(3) and passes the function you are interested in to qsort unchanged.

Now, Linux's qsort man page has a good example of qsort use.

幸福还没到 2024-11-02 21:01:41

为 iain 的答案添加一个例子。

这是一个显示 GArray 函数(包括“g_array_sort()”)效果的示例。可以看到,排序后,int数组按照升序排列:

/*
 * file: garray_test.c
 * compile: gcc -o g_array garray_test.c `pkg-config --cflags --libs glib-2.0`
 */

#include <glib.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");
}

int my_int_sort_function (gconstpointer a, gconstpointer b)
{
    int * int_a = (int *) a;
    int * int_b = (int *) b;

    return (*int_a) - (*int_b);
}

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));
    printf("Current length of array is %d\n", array->len);

    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, 5);
    cur_arr_len -= 1;
    display_array(array, cur_arr_len, "After remove value at index 5");

    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_sort (array, my_int_sort_function);
    display_array(array, cur_arr_len, "Try to sort the array");

    g_array_free(array, TRUE);
}

上面代码的结果:

Current length of array is 0
Create array: 
10 11 12 13 14 
After append values 30 40 50 60: 
10 11 12 13 14 30 40 50 60 
After prepend value 1 2 3: 
3 2 1 10 11 12 13 14 30 40 50 60 
After prepend values -10 -20 -30 -40: 
-10 -20 -30 -40 3 2 1 10 11 12 13 14 30 40 50 60 
After insert 100 at index 5: 
-10 -20 -30 -40 3 100 2 1 10 11 12 13 14 30 40 50 60 
After remove value at index 5: 
-10 -20 -30 -40 3 2 1 10 11 12 13 14 30 40 50 60 
After remove value at index 10 fast: 
-10 -20 -30 -40 3 2 1 10 11 12 60 14 30 40 50 
Try to sort the array: 
-40 -30 -20 -10 1 2 3 10 11 12 14 30 40 50 60 

Add an example for iain's answer.

This is an example to show the effect of GArray functions including 'g_array_sort()'. You can see that after the sort, the int array is listed in an ascending order:

/*
 * file: garray_test.c
 * compile: gcc -o g_array garray_test.c `pkg-config --cflags --libs glib-2.0`
 */

#include <glib.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");
}

int my_int_sort_function (gconstpointer a, gconstpointer b)
{
    int * int_a = (int *) a;
    int * int_b = (int *) b;

    return (*int_a) - (*int_b);
}

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));
    printf("Current length of array is %d\n", array->len);

    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, 5);
    cur_arr_len -= 1;
    display_array(array, cur_arr_len, "After remove value at index 5");

    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_sort (array, my_int_sort_function);
    display_array(array, cur_arr_len, "Try to sort the array");

    g_array_free(array, TRUE);
}

Result of the code above:

Current length of array is 0
Create array: 
10 11 12 13 14 
After append values 30 40 50 60: 
10 11 12 13 14 30 40 50 60 
After prepend value 1 2 3: 
3 2 1 10 11 12 13 14 30 40 50 60 
After prepend values -10 -20 -30 -40: 
-10 -20 -30 -40 3 2 1 10 11 12 13 14 30 40 50 60 
After insert 100 at index 5: 
-10 -20 -30 -40 3 100 2 1 10 11 12 13 14 30 40 50 60 
After remove value at index 5: 
-10 -20 -30 -40 3 2 1 10 11 12 13 14 30 40 50 60 
After remove value at index 10 fast: 
-10 -20 -30 -40 3 2 1 10 11 12 60 14 30 40 50 
Try to sort the array: 
-40 -30 -20 -10 1 2 3 10 11 12 14 30 40 50 60 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文