回调的原理、实现与应用



什么是回调
 
    上一篇文章中讲了“函数的指针”,那么函数指针有什么用呢?一个最常用的地方就是回调。
 
    什么回调?维基百科是这样解释的:回调是一段可执行的代码通过参数传递给别一段代码,以期望在一个合适的时间调用这个参数(可执行的代码)。


参考:In computer programming, a callback is a piece of executable code that is passed as an argument to other code, which is expected to call back(execute) the argument at some convenient time.
 
如果你已明白回调或理解上面这一句话,可以漂过,感觉头昏的请往下看。
 
从一个需求开始
 
先不扯淡,直接拿事实说话。假设有这么一个需求:
有一Person类定义如下:
struct Person
{
    int age;
    float weight;
    float height;
};
    现要对Person的一组对象进行排序,但并没有确定根据什么规则来排序,有时需要根据年龄进行排序,有时需要根据身高进行排序,有时可能是根据身高和体重的综合情况来排序,还有可能……
 
    你可能会想到这样写,定义三个函数分别根据年龄、体重、身高进行排序:
void SortByAge(Person* persons, int count);
void SortByWeight(Person* persons, int count);
void SortByHeight(Person* persons, int count);
    如果要根据身高和体重的综合情况来排序,那你还得再定义一个函数。这样是不是代码冗余且很繁琐?但如果你会用回调,这个问题就会很简单。
 
用回调实现对Person的排序:
 
typedef int (*Compare)(const Person&, const Person&);
 
//交换两个元素
void swap(Person* p1, Person *p2)
{
Person p = *p1;
*p1 = *p2;
*p2 = p;
}
//排序(本例中采用冒泡排序)
void PersonSort(Person* persons, int count, Compare pCompare)
{
for (int i = 0; i < count-1; i ++)
{
for (int j = 0; j < count - i -1; j++)
{
if (pCompare(persons[j], persons[j+1]) > 0)
{
swap(persons+j, persons+j+1);
}
}
}
}
 
    如果你要根据年龄来进行排序,只要实现一个Compare类型的函数,再调用上面的PersonSort函数就可以实现根据年龄排序的功能。如:
int CompareByAge(const Person& p1, const Person& p2)
{
return p1.age - p2.age;
}
 
void TestCallBack()
{
//创建Person的一组对象persons,对象中的各个值为0到100的随机数
srand((unsigned)time(NULL)); 
Person persons[10];
for(int i = 0; i < 10; i ++)
{
persons[i].age = rand()%100;
persons[i].weight = rand()%100;
persons[i].height = rand()%100;
}
//【todo】
//根据年龄进行排序。
PersonSort(persons, 10, CompareByAge);

for(int i = 0; i < 10; i ++)
{
std::cout << persons[i].age << "\t" << persons[i].weight << "\t" << persons[i].height << std::endl;
}
}


说明:以上调用TestCallBack函数时需要包含头文件<iostream>、<time.h>
 
    这样如果需求发生变更(如要根据每个Person身高和体重的总和来排序),只需求再定义一个Compare类型的函数,而不用再对PersonSort函数做任何改动。如下:
int CompareByHeightWeight(const Person& p1, const Person& p2)
{
return (p1.height + p1.weight) - (p2.height + p2.weight);
}
 
void TestCallBack()
{
//创建Person的一组对象persons,对象中的各个值为0到100的随机数
srand((unsigned)time(NULL)); 
Person persons[10];
for(int i = 0; i < 10; i ++)
{
persons[i].age = rand()%100;
persons[i].weight = rand()%100;
persons[i].height = rand()%100;
}
//【todo】
//根据年龄进行排序。
PersonSort(persons, 10, CompareByHeightWeight);

for(int i = 0; i < 10; i ++)
{
std::cout << persons[i].age << "\t" << persons[i].weight << "\t" << persons[i].height << "\t" << persons[i].weight + persons[i].height << std::endl;
}
}




     C++ STL中的Sort(在<algorithm>头文件中)用的就是这种技术:
template<class RandomAccessIterator>
     void sort(
        RandomAccessIterator first, 
        RandomAccessIterator last
     );
  template<class RandomAccessIterator, class Predicate>
     void sort(
        RandomAccessIterator first, 
        RandomAccessIterator last, 
        Predicate comp
     );
Parameters
first
A random-access iterator addressing the position of the first element in the range to be sorted.
last
A random-access iterator addressing the position one past the final element in the range to be sorted.
comp
User-defined predicate function object that defines the comparison criterion to be satisfied by successive elements in the ordering. This binary predicate takes two arguments and returns true if the two arguments are in order and false otherwise. This comparator function must impose a strict weak ordering on pairs of elements from the sequence. For more information, see Algorithms. 


 
    通过上面一个实例的分析,应该对回调有所了解和认识了吧!回调函数说白了就是定义一个函数,然后通过参数传递给另一个函数调用。回调不仅是一种技术,更是一种编程思想,上面是通过回调函数来实现的,但它不仅限于回调函数,也可以用其它的技术实现(如面向对象的实现)。如果想更深入的了解,请看后续的文章。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值