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)相关推荐

  1. c语言 谓词,C++ 谓词(predicate) 与 仿函数 ( functor (function object))

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

  2. C++中的仿函数functor

    引子 先考虑一个简单的例子:假设有一个vector<string>,你的任务是统计长度小于5的string的个数,如果使用count_if函数的话,你的代码: bool LessThan5 ...

  3. c++仿函数 functor

    https://www.cnblogs.com/decade-dnbc66/p/5347088.html 内容整理自国外C++教材 先考虑一个简单的例子:假设有一个vector<string&g ...

  4. 2022-1-19 C++STL—— 仿函数 functor

    仿函数是为了算法而服务的. 我的理解是,相较于函数,仿函数能够实现泛型参数. 如果一个 class 里面重载小括号,这样的函数创建出来的对象就叫做函数对象. 如果想要自己写出的 functor 和 S ...

  5. STL学习笔记(仿函数)

    仿函数(Functors) 仿函数(functor),就是使一个类的使用看上去象一个函数.其实现就是类中实现一个operator(),这个类就有了类似函数的行为,就是一个仿函数类了. 例如我们定义一个 ...

  6. C++新特性探究(9.1):functor仿函数探究

    相关博文:C++新特性探究(九):functor仿函数 Functor 对象模拟函数   把类对象,像函数名一样使用.   仿函数(functor),就是使一个类的使用看上去像一个函数.其实现就是类中 ...

  7. ceres快速教材及学习笔记(四)bundl adjustment,《视觉slam十四讲》第十讲ceres例子

    0. 前言 本文是根据ceres官方教程内容ceres-solver官方教程链接,再结合自己理解的一个ceres快速学习笔记. 在博文ceres快速教材及学习笔记(一)hello,world!中,我们 ...

  8. C++11_学习笔记

    Vector(std::initializer_list<double>)  //{1.2,12.3,111.2} 关键字virtual的意思是可能随后在其派生类中重新定义:含有纯虚函数的 ...

  9. C++学习笔记【封装、继承、多态】

    C++学习笔记 [封装.继承.多态] 个人通过学习C++,手打出了一份27000字C++笔记,包括封装继承多态等面向对象的思想:笔记中包含了大量的代码案例对每个重要的知识点进行了代码演示,通过理论和实 ...

最新文章

  1. [转帖]SAP初级学习者一句话入门06—PS
  2. SQLmap工具常用命令
  3. 赠书:Kotlin在Spring Boot中的应用
  4. java中怎么找类的路径_Java中获取类加载路径和项目根路径的5种方法
  5. pdf保存如何带批注_带有批注的SpringSelenium测试
  6. PHP获取一段时间内的每个周几, 每月几号, 遇到特殊日子就往后延
  7. ArcGIS:ArcToolBox工具使用——提取DEM/DSM中的高程点
  8. FreeSwitch SIP基本原理和流程
  9. linux共用home分区,安装UOS和Deepin双系统并且共用/home分区的测试报告
  10. 测试用例编写八大要素
  11. word插入分隔符的方法
  12. Qt 控件添加右键菜单
  13. android app报告,知乎APP用户体验报告
  14. 163邮箱怎么开通注册?公司邮箱格式选择,可以邮箱注册微信吗?
  15. 胶质瘤新发现丨神经胶质瘤标志物研究
  16. 一篇文章构建你的 NodeJS 知识体系(W字长文)
  17. 《娱乐至死》读书笔记
  18. BDB (Berkeley DB)数据库简介(转载)
  19. 计算机粘贴复制快捷键,详细教你使用电脑复制粘贴快捷键
  20. 传统企业培训与线上直播培训大PK

热门文章

  1. ipsec_profile
  2. DataTable,DataView和DataGrid中一些容易混淆的概念
  3. DevExpress 组件ToolBar、PopupMenu 使用有感
  4. 连续加班2周做出来的功能,老板说先不做了
  5. 除了要求app随手机壳颜色变之外,产品还要求....
  6. 【母亲节快乐】程序员的表达方式,了解一下?
  7. ios 高德挪动地图获取经纬度_高德地图获取地理位置经纬度并将经维度转化为详细地址信息...
  8. infor wms 项目启动_派诺科技数字工厂MES/WMS系统项目正式启动
  9. 对于 AttributeError: 'Series' object has no attribute 'sort'的解决办法
  10. Advanced Science|北京大学第一医院肾脏内科杨莉团队与白凡研究组揭示急性肾损伤中始动和放大炎症巨噬细胞新亚群...