回调函数就是一个被作为参数传递的函数。在C语言中,回调函数只能使用函数指针实现,在C++、Python等更现代的编程语言中还可以使用仿函数或匿名函数实现。
回调函数的使用可以大大提升编程的效率,这使得它在现代编程中被非常多地使用。同时,有一些需求必须要使用回调函数来实现。最著名的回调函数调用有C/C++标准库stdlib.h/cstdlib中的快速排序函数qsort和二分查找函数bsearch中都会要求的一个与strcmp类似的参数,用于设置数据的比较方法。
回调函数的作用是将代码逻辑分离出来,使得代码更加模块化和可维护。使用回调函数可以避免阻塞程序的运行,提高程序的性能和效率。另外,回调函数还可以实现代码的复用,因为它们可以被多个地方调用。
回调函数的使用场景包括:
事件处理:回调函数可以用于处理各种事件,例如鼠标点击、键盘输入、网络请求等。
异步操作:回调函数可以用于异步操作,例如读取文件、发送邮件、下载文件等。
数据处理:回调函数可以用于处理数据,例如对数组进行排序、过滤、映射等。
插件开发:回调函数可以用于开发插件,例如 WordPress 插件、jQuery 插件等
以冒泡排序为例:
- 冒泡排序
- void Bubble_qsort(int arr[10], int x)
- {
- for (int i = 0; i < x - 1; i++)
- {
- for (int j = 0; j < x - 1 - i; j++)
- {
- if (arr[j] > arr[j + 1])
- {
- int temp = arr[j];
- arr[j] = arr[j + 1];
- arr[j + 1] = temp;
- }
- }
- }
- }
- int main()
- {
- int arr[10] = { 1,2,3,4,5,6,7,8,9,0 };
- int size = sizeof(arr) / sizeof(arr[0]);
- Bubble_qsort(arr,size);
- int i = 0;
- while (i <= 9)
- {
- printf("%d",arr[i]);
- i++;
- }
- return 0;
- }
上述代码中的冒泡好像只能比较整型数据,换成字符串好像就没那么容易比较出大小了。此外,就算改进冒泡,让它可以比较字符串,但是浮点数、结构体等等是不是还比较不了?解决了一个问题,会导致原本的数据排不了。由此可见,冒泡排序的适用性非常小,我们需要新的排序来满足我们的要求。我们借用指针来指向数据类型,通过改变指针的指向来改变数据进而满足要求。
- #include
- #include
-
- //冒泡排序升级版
- void Swap(char* buf1, char* buf2, int size)//交换arr[j],arr[j+1]这两个元素
- {
- int i = 0;
- char tmp = 0;
- for (i = 0; i < size; i++)
- {
- tmp = *buf1;
- *buf1 = *buf2;
- *buf2 = tmp;
- buf1++;
- buf2++;
- }
- }
- void Bubble_sort(void*arr,int num,int size, int (*cmp)(const void*, const void*))
- {
- for (int i = 0; i < num - 1; i++)
- {
- for (int j = 0; j < num - 1-i;j++)
- {
- if (cmp((char*)arr + j * size, (char*)arr + (j + 1) * size) > 0)//两个元素比较,需要将arr[j],arr[j+1]的地址要传给cmp
- {
- Swap((char*)arr + j * size, (char*)arr + (j + 1) * size, size);
- }
- }
- }
- }
- struct Stu
- {
- char name[20];
- int age;
- };
- int cmp_int(const void* p1, const void* p2)
- {
- return (*(int*)p1 - *(int*)p2);
- }
- int cmp_stu_by_age(const void* p1, const void* p2)
- {
- return ((struct Stu*)p1)->age - ((struct Stu*)p2)->age;
- }
- int cmp_stu_by_name(const void* p1, const void* p2)
- {
- return strcmp(((struct Stu*)p1)->name, ((struct Stu*)p2)->name);
- }
- void test3()//测试结构体的字符串数据
- {
- struct Stu arr[] = { {"zhangsan", 20}, {"lisi", 50},{"wangwu", 15} };
- int sz = sizeof(arr) / sizeof(arr[0]);
- printf("%d\n", sizeof(struct Stu));
- bubble_sort(arr, sz, sizeof(arr[0]), cmp_stu_by_name);
- }
- void test2()//测试结构体的年龄数据
- {
- struct Stu arr[] = { {"zhangsan", 20}, {"lisi", 50},{"wangwu", 15} };
- int sz = sizeof(arr) / sizeof(arr[0]);
- printf("%d\n", sizeof(struct Stu));
- bubble_sort(arr, sz, sizeof(arr[0]), cmp_stu_by_age);
- }
- test1()//测试整型数据
- {
- int arr[10] = { 3,4,5,7,8,9,1,2,0,6 };
- int num = sizeof(arr) / sizeof(arr[0]);
- int size = sizeof(int);
- bubble_sort(arr, num, sizeof(arr[0]), cmp_int);
- print(arr,num);
- }
- int cmp_int(const void* p1, const void* p2)
- {
- return (*(int*)p1 - *(int*)p2);
- }
- int main()
- {
- //test1();
- //test2();
- test3();
-
- return 0;
- }
上面的例子说明,回调函数可以将一个函数作为参数传递给另一个函数,这能帮助我们简化函数设计。
void process_data(void *data, int len, void (*callback)(void *result));
其中,data
表示要处理的数据,len
表示数据的长度,callback
是一个函数指针,用于指定处理完数据后的回调函数。回调函数的形式如下:
void callback_func(void *result);
在 process_data
函数中,首先会对数据进行处理,然后将处理结果传递给回调函数进行处理。具体实现如下:
- void process_data(void *data, int len, void (*callback)(void *result)) {
- // 处理数据
- void *result = data; // 这里只是举个例子,实际上需要根据实际情况进行处理
-
- // 调用回调函数
- callback(result);
- }
使用示例:
- #include
-
- void callback_func(void *result) {
- printf("processing result: %s\n", (char *)result); // 这里只是举个例子,实际上需要根据实际情况进行处理
- }
-
- int main() {
- char data[] = "hello world";
- process_data(data, sizeof(data), callback_func);
- return 0;
- }
优点:
缺点: