1

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

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

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

1 回答 1

2

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

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);
于 2013-07-25T12:54:17.023 回答