C++笔记-仿函数(functor)
STL6个组建:
1. 仿函数;
2. 算法;
3. 迭代器;
4. 空间配置器;
5. 容器;
6. 适配器;
仿函数一般不会单独使用,主要是为了搭配STL算法。
函数指针不能满足STL对抽象性的要求,不能满足软件积木的要求,无法和STL其他组建搭配;
本质就是类重载了一个operator(),创建一个行为类似函数的对象。
如下C++容器排序的过程。
程序运行截图都这样:
C++排序过程
#include <iostream>
#include <algorithm>using namespace std;bool mySort(int a, int b) {return a > b;
}void display(int a) {cout << a << " ";
}int main() {int arr1[] = { 5, 4, 2, 1, 7, 99 };sort(arr1, arr1 + 6, mySort);for_each(arr1, arr1 + 6, display);getchar();return 0;
}
C++模板
#include <iostream>
#include <algorithm>using namespace std;template<class T>
inline bool mySort(T const &a, T const &b) {return a > b;
}template<class T>
inline void display(T const &a) {cout << a << " ";
}int main() {int arr1[] = { 5, 4, 2, 1, 7, 99 };sort(arr1, arr1 + 6, mySort<int>);for_each(arr1, arr1 + 6, display<int>);getchar();return 0;
}
使用C++仿函数来做:
#include <iostream>
#include <algorithm>using namespace std;struct Sort {bool operator()(int a, int b) {return a < b;}
};struct Display {void operator()(int a) {cout << a << " ";}
};int main() {int arr1[] = { 5, 4, 2, 1, 7, 99 };sort(arr1, arr1 + 6, Sort());for_each(arr1, arr1 + 6, Display());getchar();return 0;
}
使用C++仿函数模版来做:
#include <iostream>
#include <algorithm>using namespace std;template<class T>
struct Sort {bool operator()(T const &a, T const &b) {return a < b;}
};template<class T>
struct Display {void operator()(T const &a) {cout << a << " ";}
};int main() {int arr1[] = { 5, 4, 2, 1, 7, 99 };sort(arr1, arr1 + 6, Sort<int>());for_each(arr1, arr1 + 6, Display<int>());getchar();return 0;
}
C++笔记-仿函数(functor)相关推荐
- c语言 谓词,C++ 谓词(predicate) 与 仿函数 ( functor (function object))
#谓词与函数对象 谓词 predicate C++ 标准定义谓词如下: The Predicate parameter is used whenever an algorithm expects a ...
- C++中的仿函数functor
引子 先考虑一个简单的例子:假设有一个vector<string>,你的任务是统计长度小于5的string的个数,如果使用count_if函数的话,你的代码: bool LessThan5 ...
- c++仿函数 functor
https://www.cnblogs.com/decade-dnbc66/p/5347088.html 内容整理自国外C++教材 先考虑一个简单的例子:假设有一个vector<string&g ...
- 2022-1-19 C++STL—— 仿函数 functor
仿函数是为了算法而服务的. 我的理解是,相较于函数,仿函数能够实现泛型参数. 如果一个 class 里面重载小括号,这样的函数创建出来的对象就叫做函数对象. 如果想要自己写出的 functor 和 S ...
- STL学习笔记(仿函数)
仿函数(Functors) 仿函数(functor),就是使一个类的使用看上去象一个函数.其实现就是类中实现一个operator(),这个类就有了类似函数的行为,就是一个仿函数类了. 例如我们定义一个 ...
- C++新特性探究(9.1):functor仿函数探究
相关博文:C++新特性探究(九):functor仿函数 Functor 对象模拟函数 把类对象,像函数名一样使用. 仿函数(functor),就是使一个类的使用看上去像一个函数.其实现就是类中 ...
- ceres快速教材及学习笔记(四)bundl adjustment,《视觉slam十四讲》第十讲ceres例子
0. 前言 本文是根据ceres官方教程内容ceres-solver官方教程链接,再结合自己理解的一个ceres快速学习笔记. 在博文ceres快速教材及学习笔记(一)hello,world!中,我们 ...
- C++11_学习笔记
Vector(std::initializer_list<double>) //{1.2,12.3,111.2} 关键字virtual的意思是可能随后在其派生类中重新定义:含有纯虚函数的 ...
- C++学习笔记【封装、继承、多态】
C++学习笔记 [封装.继承.多态] 个人通过学习C++,手打出了一份27000字C++笔记,包括封装继承多态等面向对象的思想:笔记中包含了大量的代码案例对每个重要的知识点进行了代码演示,通过理论和实 ...
最新文章
- [转帖]SAP初级学习者一句话入门06—PS
- SQLmap工具常用命令
- 赠书:Kotlin在Spring Boot中的应用
- java中怎么找类的路径_Java中获取类加载路径和项目根路径的5种方法
- pdf保存如何带批注_带有批注的SpringSelenium测试
- PHP获取一段时间内的每个周几, 每月几号, 遇到特殊日子就往后延
- ArcGIS:ArcToolBox工具使用——提取DEM/DSM中的高程点
- FreeSwitch SIP基本原理和流程
- linux共用home分区,安装UOS和Deepin双系统并且共用/home分区的测试报告
- 测试用例编写八大要素
- word插入分隔符的方法
- Qt 控件添加右键菜单
- android app报告,知乎APP用户体验报告
- 163邮箱怎么开通注册?公司邮箱格式选择,可以邮箱注册微信吗?
- 胶质瘤新发现丨神经胶质瘤标志物研究
- 一篇文章构建你的 NodeJS 知识体系(W字长文)
- 《娱乐至死》读书笔记
- BDB (Berkeley DB)数据库简介(转载)
- 计算机粘贴复制快捷键,详细教你使用电脑复制粘贴快捷键
- 传统企业培训与线上直播培训大PK
热门文章
- ipsec_profile
- DataTable,DataView和DataGrid中一些容易混淆的概念
- DevExpress 组件ToolBar、PopupMenu 使用有感
- 连续加班2周做出来的功能,老板说先不做了
- 除了要求app随手机壳颜色变之外,产品还要求....
- 【母亲节快乐】程序员的表达方式,了解一下?
- ios 高德挪动地图获取经纬度_高德地图获取地理位置经纬度并将经维度转化为详细地址信息...
- infor wms 项目启动_派诺科技数字工厂MES/WMS系统项目正式启动
- 对于 AttributeError: 'Series' object has no attribute 'sort'的解决办法
- Advanced Science|北京大学第一医院肾脏内科杨莉团队与白凡研究组揭示急性肾损伤中始动和放大炎症巨噬细胞新亚群...