一.回调函数
1.回调函数定义
回调函数就是一个通过函数指针调用的函数。如果你把函数的指针(地址)作为函数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就说这个是回调函数。
回调函数不是由该函数的实现方直接调用,而是在特定的事件或者条件发生时,由另一方调用的,用于对该事件和条件进行响应。
2.回调函数简单举例
void test()
{
printf("hehe");
}
void fun(void(*p)())//用函数指针来接收test函数的地址
{
p();//在合适的场景下调用test函数
}
int main()
{
fun(test);//把test函数的地址传递给fun函数
return 0;
}
二.使用回调函数模拟实现qsort
1.首先了解qsort的使用方法
qsort叫做快速排序
参数介绍:
void qsort(void *base,size_t num,size_t width,int(*cmp)(const void *e1,const void *e2))
void *base—>目标数组的起始地址
size_t num—>数组有多少个元素(单位个)
size_t width—>每个元素的大小(单位字节)
int(*cmp)(const void *e1,const void *e2))—>比较函数(需要自己实现)
使用:
在使用qsort函数时,我们需要自己实现一个比较函数,因为自己使用的时候知道自己要比较两个值的方法。
如下程序,我们自己实现了一个整型数组的比较方法cmp_int,一个结构体中按年龄排序的方法cmp_stu_by_age,一个结构体中按名字排序的方法cmp_stu_by_name。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Stu//定义一个结构体
{
char name[20];
int age;
};
int cmp_int(const void *e1, const void *e2)//整型的比较方法
{
return *(int *)e1 - *(int *)e2;
}
int cmp_stu_by_age(const void *e1, const void *e2)//结构体按年龄的比较方法
{
return ((struct Stu *)e1)->age - ((struct Stu *)e2)->age;
}
int cmp_stu_by_name(const void *e1, const void *e2)//结构体按名字的比较方法
{
return strcmp(((struct Stu *)e1)->name , ((struct Stu *)e2)->name);
}
int main()
{
//排序整形数组
int arr[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
//排序结构体
//struct Stu arr[] = { { "zhangsan", 20 }, { "lisi", 15 }, { "wangwu", 8 } };
int sz = sizeof(arr) / sizeof(arr[0]);//求元素个数sz
int i = 0;
qsort(arr, sz, sizeof(arr[0]), cmp_int);//这里传参时第四个参数为比较函数,这个函数需要自己来实现
for (i = 0; i < sz; i++)//排序整型时打印整形数组
{
printf("%d ", arr[i]);
}
system("pause");
return 0;
}
2.模拟实现qsort(使用冒泡排序的方式)
#include <stdlib.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
struct Stu//定义一个结构体
{
char name[20];
int age;
};
int cmp_int(const void *e1, const void *e2)//整型的比较方法
{
return *(int *)e1 - *(int *)e2;
}
int cmp_stu_by_age(const void *e1, const void *e2)//结构体按年龄的比较方法
{
return ((struct Stu *)e1)->age - ((struct Stu *)e2)->age;
}
int cmp_stu_by_name(const void *e1, const void *e2)//结构体按名字的比较方法
{
return strcmp(((struct Stu *)e1)->name , ((struct Stu *)e2)->name);
}
//交换函数
//由于不知道要交换的数据类型,因此这里采用一字节一字节交换
//交换width个就可以完成两个数据的交换了
void _swap(char* buf1, char* buf2, size_t width)
{
unsigned int i = 0;
assert(buf1 && buf2);
for (i = 0; i < width; i++)
{
char tmp = *buf1;
*buf1 = *buf2;
*buf2 = tmp;
buf1++;
buf2++;
}
}
void bubble_sort(void *base, int num, int width, int (*cmp)(const void *e1, const void *e2))
{
assert(base && cmp);
int i = 0;
int j = 0;
for (i = 0; i < num - 1; i++)
{
for (j = 0; j < num - 1 - i; j++)
{
//相邻元素的比较
if (cmp((char*)base + j*width, (char*)base + (j + 1)*width)>0)
{
//交换
_swap((char*)base + j*width, (char*)base + (j + 1)*width,width);
}
}
}
}
int main()
{
int i = 0;
int arr[] = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 };
//struct Stu arr[] = { { "zhangsan", 20 }, { "lisi", 15 }, { "wangwu", 8 } };
int sz = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr, sz , sizeof(arr[0]), cmp_stu_by_name);
for (i = 0; i < sz; i++)//用于整型数组的打印
{
printf("%d ", arr[i]);
}
printf("\n");
system("pause");
return 0;
}
本文深入探讨了回调函数的概念及应用,通过具体实例讲解了如何使用回调函数进行自定义排序,包括整型数组和结构体数组的排序,并提供了一种使用冒泡排序模拟qsort函数的实现方式。
1827

被折叠的 条评论
为什么被折叠?



