深入解析神秘的 --- 仿函数

来源:互联网 发布:win10声音增强软件 编辑:程序博客网 时间:2024/05/17 06:10

一,概述
        仿函数(functor),就是使一个类的使用看上去象一个函数。其实现就是类中实现一个operator(),这个类就有了类似函数的行为,就是一个仿函数类了。
  有些功能的的代码,会在不同的成员函数中用到,想复用这些代码。

                            1)公共的函数,可以,这是一个解决方法,不过函数用到的一些变量,就可能成为公共的全局变量,再说为了复用这么一片代码,就要单立出一个函数,也不是很好维护。

                            2)仿函数,写一个简单类,除了那些维护一个类的成员函数外,就只是实现一个operator(),在类实例化时,就将要用的,非参数的元素传入类中。


二,仿函数(functor)在各编程语言中的应用

  1)C语言使用函数指针回调函数来实现仿函数,例如一个用来排序的函数可以这样使用仿函数
  

[html] view plain copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. //int sort_function( const void *a, const void *b);  
  4. int sort_function( const void *a, const void *b)  
  5. {     
  6.     return *(int*)a-*(int*)b;  
  7. }  
  8.   
  9. int main()  
  10. {  
  11.      
  12.    int list[5] = { 54, 21, 11, 67, 22 };  
  13.    qsort((void *)list, 5, sizeof(list[0]), sort_function);//起始地址,个数,元素大小,回调函数   
  14.    int  x;  
  15.    for (x = 0; x < 5; x++)  
  16.           printf("%i\n", list[x]);  
  17.                     
  18.    return 0;  
  19. }  

        2)在C++里,我们通过在一个类中重载括号运算符的方法使用一个函数对象而不是一个普通函数。

[html] view plain copy
  1. #include <iostream>  
  2. #include <algorithm>  
  3.   
  4. using namespace std;  
  5. template<typename T>  
  6. class display  
  7. {  
  8. public:  
  9.     void operator()(const T &x)  
  10.     {  
  11.         cout<<x<<" ";   
  12.     }   
  13. };   
  14.   
  15.   
  16. int main()  
  17. {  
  18.     int ia[]={1,2,3,4,5};  
  19.     for_each(ia,ia+5,display<int>());   
  20.       
  21.     return 0;   
  22. }   


三,仿函数在STL中的定义

        要使用STL内建的仿函数,必须包含<functional>头文件。而头文件中包含的仿函数分类包括

         1)算术类仿函数

               加:plus<T>

               减:minus<T>

               乘:multiplies<T>

               除:divides<T>

               模取:modulus<T>

               否定:negate<T>

例子:

[html] view plain copy
  1. #include <iostream>  
  2. #include <numeric>  
  3. #include <vector>   
  4. #include <functional>   
  5. using namespace std;  
  6.   
  7. int main()  
  8. {  
  9.     int ia[]={1,2,3,4,5};  
  10.     vector<int> iv(ia,ia+5);  
  11.     cout<<accumulate(iv.begin(),iv.end(),1,multiplies<int>())<<endl;   
  12.       
  13.     cout<<multiplies<int>()(3,5)<<endl;  
  14.       
  15.     modulus<int>  modulusObj;  
  16.     cout<<modulusObj(3,5)<<endl; // 3   
  17.     return 0;   
  18. }   


         2)关系运算类仿函数

               等于:equal_to<T>

               不等于:not_equal_to<T>

               大于:greater<T>

               大于等于:greater_equal<T>

               小于:less<T>

               小于等于:less_equal<T>

              从大到小排序:

[html] view plain copy
  1. #include <iostream>  
  2. #include <algorithm>  
  3. #include <vector>   
  4.   
  5. using namespace std;  
  6.   
  7. template <class T>   
  8. class display  
  9. {  
  10. public:  
  11.     void operator()(const T &x)  
  12.     {  
  13.         cout<<x<<" ";   
  14.     }   
  15. };  
  16.   
  17. int main()  
  18. {  
  19.     int ia[]={1,5,4,3,2};  
  20.     vector<int> iv(ia,ia+5);  
  21.     sort(iv.begin(),iv.end(),greater<int>());  
  22.     for_each(iv.begin(),iv.end(),display<int>());   
  23.     return 0;   
  24. }   

            3)逻辑运算仿函数

                 逻辑与:logical_and<T>

                 逻辑或:logical_or<T>

                 逻辑否:logical_no<T>

原创粉丝点击