gpt4 book ai didi

c - 在 GLib 中搜索 g_slist_find_custom() 函数

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:23:14 24 4
gpt4 key购买 nike

当我使用单个列表时,如何使用 g_slist_find_custom()。列表的每个节点都是一个结构。

typedef struct {
int number;
int price;
char* title;
}Books;

GSList *list =NULL,
g_slist_find_custom(list, /*?*/, (GCompareFunc)compp/*?*/);

最佳答案

您可以使用比较函数在 GSList 中查找项目:

gint comp(gpointer pa, gpointer pb)
{
const Books *a = pa, *b = pb;

/* Compared by title */
return strcmp(a->title, b->title);
}

GSList *list = NULL;
Books temp, *item, *abook = g_malloc(sizeof(Books));

strcpy(abook->title, "Don Quijote");
list = g_slist_append(list, abook);
/* more code */
temp.title = "Don Quijote";
item = g_slist_find_custom(list, &temp, (GCompareFunc)comp);
/* now item contains abook */

并且您可以使用 NULL 作为第二个参数来比较常量:

gint comp(gpointer p)
{
const Books *x = p;

return strcmp(x->title, "Don Quijote");
}

item = g_slist_find_custom(list, NULL, (GCompareFunc)comp);

关于c - 在 GLib 中搜索 g_slist_find_custom() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17858247/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com