C++ 仿函数

来源:互联网 发布:修改网络手游数据 编辑:程序博客网 时间:2024/04/28 22:54
//**
// * 所谓的仿函数(functor),是通过重载()运算符模拟函数行为的类。
// * 因此,这里需要明确两点:
// * 1 仿函数不是函数,它是个类;
// * 2 仿函数重载了()运算符,使得它的对你可以像函数那样子调用(代码的形式好像是在调用函数)。
// **/  
#include <iostream>  //标准输入输出流
using namespace std;//标准命名空间
const int CMP_LES = -1;//定义的常量
const int CMP_EQU = 0;
const int CMP_BIG = 1;
class Comparer  //仿函数的类 函数的主要功能是重载运算符()这样调用的时候就直接用类引用加上()后面的参数就可以 
{
public:
 Comparer(int cmpType)
    {
  m_cmpType = cmpType;//初始化变量
    }
    bool operator ()(int num1, int num2) const
    {
  bool res;
        switch(m_cmpType)
        {
   case CMP_LES:
    res = num1 < num2;
                break;
            case CMP_EQU:
                res = num1 == num2;
                break;
            case CMP_BIG:
                res = num1 > num2;
                break;
            default:
                res = false;
                break;
        }
  return res;
    }
private:
 int m_cmpType;
};
//互相交换数值
void Swap(int &num1, int &num2)
{
 int temp = num1;
    num1 = num2;
    num2 = temp;
}
//这个是个排序函数 双层循环来排列顺序 
void SortArray(int array[], int size, const Comparer &cmp)
{
 for (int i = 0; i < size - 1; ++i)
 {
  int indx = i;
        for (int j = i + 1; j < size; ++j)
        {
   if (cmp(array[indx], array[j]))//这里是对仿函数类的引用 看看很像函数 
            {
    indx = j;
            }
        }
        if (indx != i)
        {
        Swap(array[i], array[indx]);
        }
 }
}
//这是输出显示函数
void ListArray(int array[], int size) 
{
 for (int i = 0; i < size; ++i)
    {
  cout << array[i] << " ";
    }
}

#define ARY_SIZE 10
int main()
{
 int array[ARY_SIZE] = {10, 12, 9, 31, 93, 34, 98, 9, 1, 20};//定义一个数组 有十个数据
 cout << "The initial array is : ";//首先输出现在的数组数据
 ListArray(array, ARY_SIZE);
 cout << endl;
 SortArray(array, ARY_SIZE, Comparer(CMP_BIG));//然后在这里用到了SortArray排序函数 其中第三个参数是个
//对象的引用 所以要初始化这个对象(他是先初始化Comparer对象然后再动作的)
 cout << "The ascending sorted array is :";
 ListArray(array, ARY_SIZE);
 cout << endl;
 SortArray(array, ARY_SIZE, Comparer(CMP_LES));//用这个方法来排序
 cout << "The descending sorted array is : ";//输出排序以后的方法
 ListArray(array, ARY_SIZE);
 cout << endl;
  return 0;
}
//****************************************************************************************
程序中定义了一个仿函数Comparer,它重重载了()运算符:
  Comparer::bool operator ()(int num1, int num2) const;
  这里温习一下运算符重载的方式:
  ret_type operator opt(array_list);
  其中,ret_type为运算符重载后返回值的类型,operator为c++运算符重载专用关健字,opt为所要重载的运算符,如+, -, *, /, [], ()...
  于是我们可以解读Comparer::bool operator ()(int num1, int num2) const的意义:
  bool限定了()的返回值为布尔类型,(int num1, int num2)指定了运算符()的参数形式,const使得应该运算符可被它的const对象调用。()运算符中根据m_cmpType值返回不同方式下两整数的比较值。

  函数void SortArray(int array[], int size, const Comparer &cmp)用于给数组排序。其中,array[]指定所要排序的数组对象,size限定数组元素个数,cmp为Comparer对象的引用,用作对元素的比较使用,前面使用const修饰是向函数调用都声明,在函数内不会有修改该对象任何数据的形为。注意SortArray中的代码:
            if (cmp(array[indx], array[j]))
            {
                indx = j;
            }
  其中,cmp为Comparer类的一个对象,但这里的用法好像它是某个函数的样子。这就是仿函数的真谛。

  别外,void Swap(int &num1, int &num2)完成交换num1与num2值的功能。int &num1表示函数参数使用的引用,用久了c的朋友也许更习惯了void Swap(int *num1, int *num2),但在c++中这个习惯要改了,引用和指针一样高效,但引用要比指针更直观。下面是指针版的Swap函数:
            void Swap(int *num1, int *num2)
            {
                int temp = *num1;
                *num1 = *num2;
                *num2 = temp;
            }
  实现的功能与程序中使用的一模一样,替换掉程序照样正常工作

0 0
原创粉丝点击