C++仿函数(functor)详解
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)详解的更多相关文章
- [GeekBand] STL 仿函数入门详解
本文参考文献::GeekBand课堂内容,授课老师:张文杰 :C++ Primer 11 中文版(第五版) page 37 :网络资料: 叶卡同学的部落格 http://www.leavesite. ...
- C++ 谓词(predicate) 与 仿函数 ( functor (function object))
谓词与函数对象 谓词 predicate C++ 标准定义谓词如下: The Predicate parameter is used whenever an algorithm expects a f ...
- for_each的各种情况下的使用详解
原创作者:http://oomusou.cnblogs.com 配合<C++ Template>(简体中文)使用 http://download.csdn.net/detail/qq239 ...
- STL之heap与优先级队列Priority Queue详解
一.heap heap并不属于STL容器组件,它分为 max heap 和min heap,在缺省情况下,max-heap是优先队列(priority queue)的底层实现机制.而这个实现机制中的m ...
- C++中的STL中map用法详解(转)
原文地址: https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html C++中的STL中map用法详解 Map是STL的一个关联容器,它提供 ...
- Java反序列化漏洞详解
Java反序列化漏洞从爆出到现在快2个月了,已有白帽子实现了jenkins,weblogic,jboss等的代码执行利用工具.本文对于Java反序列化的漏洞简述后,并对于Java反序列化的Poc进 ...
- Linq之旅:Linq入门详解(Linq to Objects)
示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...
- 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)
一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...
- EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解
前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...
随机推荐
- linux c 打印彩色字符
#include <stdio.h> #include <string.h> int main(int argc, char **argv) { , j = , str_len ...
- ARP
视频教程 http://baidu.ku6.com/watch/08644463979695746698.html?page=videoMultiNeed arp代理 跨越路由 免费arp 检查i ...
- C#TCPClient应用-一个简单的消息发送和接收
TcpSend窗口用于发送消息,另外写一个用于接收消息的应用程序,消息接受到以后,必须要关闭接收消息的窗口,才能在接收新的消息,不知道怎么能解决这个问题. 源代码: 发送消息的窗口代码 using S ...
- range,shuffle,str_shuffle
print_r(range(1,20)); 输出,range产生 Array( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ...
- C#的winform拼数字游戏
C#的winform拼数字游戏 声明:阅读了别人的代码学习修改而来,增加了美观度和游戏乐趣.(作者出处忘了不好意思) 程序截图 关键代码 using System; using System.Coll ...
- php操作mysqli(示例代码)
<?php define("MYSQL_OPEN_LOGS",true); class mysqliHelp { private $db; public function _ ...
- libreoffice转换文档的方法(支持各平台各版本的libreoffice)
前段时间完成了一个利用libreoffice转换文档进行预览的资源管理系统,用的是jodconvert这个多年未更新的转换项目,由于版本不兼容等原因,导致最新版的libreoffice不能用,浪费了许 ...
- [搜片神器]服务器SQL2005查询分页语句你理解了么
在sosobt.com网站准备采用Lucence.net来进行索引处理搜索慢问题的时候,突然发现常用的分页获取数据的row_number也支持不住了,后期查到200多万的时候非常慢(总数据有500万) ...
- 剑指offer--面试题17
题目:合并两个排序的单向链表 自己所写代码如下: ListNode* MergeSortedLists(ListNode* pHead1, ListNode* pHead2) { if(pHead1 ...
- [转]GLES 3.0 新特性
转自: http://www.ifanr.com/131333 OpenGL ES 3.0 带来很多新特性,根据 AnandTech 的解释: 支持更多缓冲区对象.在 OpenGL ES 2.0 时中 ...