【C】——回调函数实现泛型算法

  回调函数的一个典型应用就是实现类似C++的泛型算法(Generics Algorithm)。下面实现的max函数可以在任意一组对象中找出最大值,可以是一组int、一组char或者一组结构体,但是实现者并不知道怎样去比较两个对象的大小,调用者需要提供一个做比较操作的回调函数。

 1 /* generics.h */
 2 #ifndef GENERICS_H
 3 #define GENERICS_H
 4 
 5 typedef int (*cmp_t)(void *, void *);
 6 extern void *max(void *data[], int num, cmp_t cmp);
 7 
 8 #endif/* generics.c */
 9 #include "generics.h"
10 
11 void *max(void *data[], int num, cmp_t cmp)
12 {
13      int i;
14      void *temp = data[0];
15      for(i=1; i<num; i++) {
16       if(cmp(temp, data[i])<0)
17            temp = data[i];
18      }
19      return temp;
20 }/* main.c */
21 #include <stdio.h>
22 #include "generics.h"
23 
24 typedef struct {
25      const char *name;
26      int score;
27 } student_t;
28 
29 int cmp_student(void *a, void *b)
30 {
31      if(((student_t *)a)->score > ((student_t *)b)->score)
32       return 1;
33      else if(((student_t *)a)->score == ((student_t *)b)->score)
34       return 0;
35      else
36       return -1;
37 }
38 
39 int main(void)
40 {
41      student_t list[4] = {{"Tom", 68}, {"Jerry", 72},
42                {"Moby", 60}, {"Kirby", 89}};
43      student_t *plist[4] = {&list[0], &list[1], &list[2], &list[3]};
44      student_t *pmax = max((void **)plist, 4, cmp_student);
45      printf("%s gets the highest score %d
", pmax->name, pmax->score);
46 
47      return 0;
48 }

  max函数之所以能对一组任意类型的对象进行操作,关键在于传给max的是指向对象的指针所构成的数组,而不是对象本身所构成的数组,这样max不必关心对象到底是什么类型,只需转给比较函数cmp,然后根据比较结果做相应操作即可,cmp是调用者提供的回调函数,调用者当然知道对象是什么类型以及如何比较。

原文地址:https://www.cnblogs.com/ngnetboy/p/3144669.html