C++仿函数(functor)详解

  所谓的仿函数(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, array[indx]);

}

}

}

void ListArray(int array[], int size)

{

for (int i = 0; i < size; ++i)

{

cout << array << " ";

}

}

#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));

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;

}

运行结果:

The initial array is : 10 12 9 31 93 34 98 9 1 20

The ascending sorted array is :1 9 9 10 12 20 31 34 93 98

The descending sorted array is : 98 93 34 31 20 12 10 9 9 1

  程序中定义了一个仿函数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;

}

  实现的功能与程序中使用的一模一样,替换掉程序照样正常工作。仔细比较引用版与指针版的Swap()函数,我相信大多数人会爱上C++的引用版。

C++仿函数(functor)详解的更多相关文章

  1. [GeekBand] STL 仿函数入门详解

    本文参考文献::GeekBand课堂内容,授课老师:张文杰 :C++ Primer 11 中文版(第五版) page 37 :网络资料: 叶卡同学的部落格  http://www.leavesite. ...

  2. C++ 谓词(predicate) 与 仿函数 ( functor (function object))

    谓词与函数对象 谓词 predicate C++ 标准定义谓词如下: The Predicate parameter is used whenever an algorithm expects a f ...

  3. for_each的各种情况下的使用详解

    原创作者:http://oomusou.cnblogs.com 配合<C++ Template>(简体中文)使用 http://download.csdn.net/detail/qq239 ...

  4. STL之heap与优先级队列Priority Queue详解

    一.heap heap并不属于STL容器组件,它分为 max heap 和min heap,在缺省情况下,max-heap是优先队列(priority queue)的底层实现机制.而这个实现机制中的m ...

  5. C++中的STL中map用法详解(转)

    原文地址: https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html C++中的STL中map用法详解   Map是STL的一个关联容器,它提供 ...

  6. Java反序列化漏洞详解

      Java反序列化漏洞从爆出到现在快2个月了,已有白帽子实现了jenkins,weblogic,jboss等的代码执行利用工具.本文对于Java反序列化的漏洞简述后,并对于Java反序列化的Poc进 ...

  7. Linq之旅:Linq入门详解(Linq to Objects)

    示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...

  8. 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)

    一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...

  9. EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解

    前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...

随机推荐

  1. linux 压缩文件 及压缩选项详解

    本文介绍linux下的压缩程序tar.gzip.gunzip.bzip2.bunzip2.compress.uncompress. zip. unzip.rar.unrar等程式,以及如何使用它们对. ...

  2. Barracuda VS antelope

    version : 5.6.16-64.0-56-log Innodb 行存储:Innodb Plugin新引入Barracuda梭子鱼,包含Compressed和Dynamic两种行格式.而Comp ...

  3. WebDev.WebServer40.exe已停止工作

    今天写程序的遇到这个错误 错误的原因是代码中有死循环

  4. android聊天,存储聊天记录sqlite

    项目中有聊天模块,需要用到打开activity的时候初始化聊天记录的情况.大致情况如下: 辅助类:ChatSQLiteHelper   在第一次时会调用oncreate方法(判断的标准是schedul ...

  5. DevExpress GridControl使用(转)

    DevExpress GridControl使用 (一)原汁原味的表格展示 Dev控件中的表格控件GridControl控件非常强大.不过,一些细枝末节的地方有时候用起来不好找挺讨厌的.使用过程中,多 ...

  6. Flex显示麦克风当前音量

    Flex动态显示麦克风当前音量 效果: 代码: <?xml version="1.0" encoding="utf-8"?> <s:Appli ...

  7. javascript 获取父页面中元素对象方法

    父页面中: <input type="hidden" id="areaID" value="test1"> <iframe ...

  8. EXT--当defaultType与items的子组件默认xtype冲突时items的子组件的xtype为panel

    示例图 直接看下面示例代码: /** * 获取导入表单 * @returns {Ext.FormPanel} */ function getImportForm() { return new Ext. ...

  9. jquery层居中,点击小图查看大图,弹出层居中代码

    1.层居中 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...

  10. 转:[gevent源码分析] 深度分析gevent运行流程

    [gevent源码分析] 深度分析gevent运行流程 http://blog.csdn.net/yueguanghaidao/article/details/24281751 一直对gevent运行 ...