当前位置: 首页 > news >正文

C++ 中的仿函数 functor

一   仿函数的概念

1. 定义

  仿函数(functor)是一种使用上像函数的类,其本质是一个实现了 operato() 函数的类,这种类就有了类似于函数一样的使用行为,这就是仿函数的类。

仿函数在 C++ STL标准库中被大量使用。

2. 特点

1. 仿函数是一个类,不是一个函数

2. 仿函数的类需要重载 operator() 函数,以此拥有函数的行为

二  STL 中常见的 仿函数介绍

1. 基类

  1.1  unary_function

 /***  This is one of the @link functors functor base classes@endlink.*/template<typename _Arg, typename _Result>struct unary_function{/// @c argument_type is the type of the argumenttypedef _Arg 	argument_type;   /// @c result_type is the return typetypedef _Result 	result_type;  };

   作为拥有一个输入参数的仿函数常用基类,该类主要回答了两个问题:

     1. 该仿函数类的输入参数是什么类型:argument_type

     2. 该仿函数类的返回参数是什么类型: result_type

  1.2  binary_function

 /***  This is one of the @link functors functor base classes@endlink.*/template<typename _Arg1, typename _Arg2, typename _Result>struct binary_function{/// @c first_argument_type is the type of the first argumenttypedef _Arg1 	first_argument_type; /// @c second_argument_type is the type of the second argumenttypedef _Arg2 	second_argument_type;/// @c result_type is the return typetypedef _Result 	result_type;};

作为拥有两个输入参数的仿函数常用基类,该类主要回答了两个问题:

     1. 该仿函数类的输入参数是什么类型:first_argument_type 与 second_argument_type

     2. 该仿函数类的返回参数是什么类型: result_type

2.  STL 中常见仿函数

2.1  plus

 /// One of the @link arithmetic_functors math functors@endlink.template<typename _Tp>struct plus : public binary_function<_Tp, _Tp, _Tp>{_Tpoperator()(const _Tp& __x, const _Tp& __y) const{ return __x + __y; }};

2.2 minus

/// One of the @link arithmetic_functors math functors@endlink.template<typename _Tp>struct minus : public binary_function<_Tp, _Tp, _Tp>{_Tpoperator()(const _Tp& __x, const _Tp& __y) const{ return __x - __y; }};

2.3  multiplies

  /// One of the @link arithmetic_functors math functors@endlink.template<typename _Tp>struct multiplies : public binary_function<_Tp, _Tp, _Tp>{_Tpoperator()(const _Tp& __x, const _Tp& __y) const{ return __x * __y; }};

2.4  divides

template<typename _Tp>struct divides : public binary_function<_Tp, _Tp, _Tp>{_Tpoperator()(const _Tp& __x, const _Tp& __y) const{ return __x / __y; }};

2.5  equal_to

 /// One of the @link comparison_functors comparison functors@endlink.template<typename _Tp>struct equal_to : public binary_function<_Tp, _Tp, bool>{booloperator()(const _Tp& __x, const _Tp& __y) const{ return __x == __y; }};

2.6  less

 /// One of the @link comparison_functors comparison functors@endlink.template<typename _Tp>struct less : public binary_function<_Tp, _Tp, bool>{booloperator()(const _Tp& __x, const _Tp& __y) const{ return __x < __y; }};

2.7  greater

/// One of the @link comparison_functors comparison functors@endlink.template<typename _Tp>struct greater : public binary_function<_Tp, _Tp, bool>{booloperator()(const _Tp& __x, const _Tp& __y) const{ return __x > __y; }};

2.8   _Select1st

template<typename _Pair>struct _Select1st: public unary_function<_Pair, typename _Pair::first_type>{typename _Pair::first_type&operator()(_Pair& __x) const{ return __x.first; }const typename _Pair::first_type&operator()(const _Pair& __x) const{ return __x.first; }};

2.9  _Select2nd

template<typename _Pair>struct _Select2nd: public unary_function<_Pair, typename _Pair::second_type>{typename _Pair::second_type&operator()(_Pair& __x) const{ return __x.second; }const typename _Pair::second_type&operator()(const _Pair& __x) const{ return __x.second; }};

3. 使用例子

#include<algorithm>
#include<iostream>int main()
{// 1. plusstd::cout << "------ plus ------" << std::endl;std::vector<int>  vec = {1, 3, 5};std::plus<int> pl;int init = 0;int res1 = std::accumulate(vec.begin(), vec.end(), init, pl);std::cout << res1 << std::endl;std::cout << "------ plus ------" << std::endl; // 9// 2. minusstd::cout << "------ minus ------" << std::endl;init = 10;std::minus<int> mis;int res2 =  std::accumulate(vec.begin(), vec.end(), init, mis);std::cout << res2 << std::endl; // 1std::cout << "------ minus ------" << std::endl;// 3. multiesstd::cout << "------ multies ------" << std::endl;init = 1;std::multiplies<int> multiply;int res3 = std::accumulate(vec.begin(), vec.end(), init, multiply);std::cout << res3 << std::endl; // 15std::cout << "------ multies ------" << std::endl;// 4. dividesstd::cout << "------ divides ------" << std::endl;init = 90;std::divides<int> divid;int res4 = std::accumulate(vec.begin(), vec.end(), init, divid);std::cout << res4 << std::endl; // 6std::cout << "------ divides ------" << std::endl;// 5. equal_tostd::cout << "------ equal_to ------" << std::endl;std::pair<int, std::string> pair1 = std::make_pair(1, "abc");std::pair<int, std::string> pair2 = std::make_pair(2, "abc");std::equal_to<std::string> equal;std::_Select2nd<std::pair<int, std::string>> second_argu;std::cout << equal(second_argu(pair1), second_argu(pair2)) << std::endl;// 1std::cout << "------ equal_to ------" << std::endl;// 6. lessstd::cout << "------ less ------" << std::endl;std::less<int> less;std::cout << less(3, 6) << std::endl; // 1std::cout << "------ less ------" << std::endl;// 7. greaterstd::cout << "------ greater ------" << std::endl;std::greater<int> greater;std::cout << greater(3, 6) << std::endl; // 0std::cout << "------ greater ------" << std::endl;// 8. _Select1ststd::cout << "------ _Select1st ------" << std::endl;std::pair<int, std::string> pair3 = std::make_pair(1, "abc");std::_Select1st<std::pair<int, std::string>> select1st;std::cout << select1st(pair3) << std::endl; // 1std::cout << "------ _Select1st ------" << std::endl;// 9. _Select2ndstd::cout << "------ _Select2nd ------" << std::endl;std::pair<int, std::string> pair4 = std::make_pair(1, "abc");std::_Select2nd<std::pair<int, std::string>> select2nd;std::cout << select2nd(pair3) << std::endl; // abcstd::cout << "------ _Select2nd ------" << std::endl;return 0;
}

输出:

相关文章:

  • PHP | php入门知识(if、switch、数组、数组排序、超级全局变量)
  • 6G关键新兴技术- 智能超表面(RIS)技术演进
  • DbVisualizer和DBeaver启动不来,启动报错
  • nodejs+vue食力派网上订餐系统-计算机毕业设计
  • AVL树、红黑树的介绍和实现[C++]
  • Windows 和 Linux 这2个系统在进行编程实现的时候的一些区别:
  • Flutter FittedBox
  • 【Python入门教程】基于OpenCV视频分解成图片+图片组合成视频(视频抽帧组帧)
  • HarmonyOS SDK,赋能开发者实现更具象、个性化开发诉求
  • java try throw exception finally 遇上 return break continue造成异常丢失
  • jenkins如何安装?
  • php框架路由实现
  • 【Unity PlasticSCM】记录:从介绍 下载 到拉取项目
  • MySQL数据库干货_08—— MySQL中的主键约束(Primary Key)
  • IDE的组成
  • [译]Python中的类属性与实例属性的区别
  • CoolViewPager:即刻刷新,自定义边缘效果颜色,双向自动循环,内置垂直切换效果,想要的都在这里...
  • eclipse的离线汉化
  • Flex布局到底解决了什么问题
  • golang中接口赋值与方法集
  • javascript数组去重/查找/插入/删除
  • Java反射-动态类加载和重新加载
  • js ES6 求数组的交集,并集,还有差集
  • Mysql优化
  • SegmentFault 社区上线小程序开发频道,助力小程序开发者生态
  • Spring框架之我见(三)——IOC、AOP
  • ViewService——一种保证客户端与服务端同步的方法
  • vue和cordova项目整合打包,并实现vue调用android的相机的demo
  • 阿里云前端周刊 - 第 26 期
  • 技术:超级实用的电脑小技巧
  • 利用阿里云 OSS 搭建私有 Docker 仓库
  • 我感觉这是史上最牛的防sql注入方法类
  • ​ssh-keyscan命令--Linux命令应用大词典729个命令解读
  • ​决定德拉瓦州地区版图的关键历史事件
  • !$boo在php中什么意思,php前戏
  • #pragma预处理命令
  • #周末课堂# 【Linux + JVM + Mysql高级性能优化班】(火热报名中~~~)
  • (3)Dubbo启动时qos-server can not bind localhost22222错误解决
  • (6)【Python/机器学习/深度学习】Machine-Learning模型与算法应用—使用Adaboost建模及工作环境下的数据分析整理
  • (k8s中)docker netty OOM问题记录
  • (笔试题)分解质因式
  • (附源码)springboot优课在线教学系统 毕业设计 081251
  • (机器学习的矩阵)(向量、矩阵与多元线性回归)
  • (九)信息融合方式简介
  • (力扣记录)235. 二叉搜索树的最近公共祖先
  • (四)JPA - JQPL 实现增删改查
  • (四)linux文件内容查看
  • (转)甲方乙方——赵民谈找工作
  • ***详解账号泄露:全球约1亿用户已泄露
  • .dwp和.webpart的区别
  • .htaccess配置重写url引擎
  • .Net Web窗口页属性
  • .Net(C#)常用转换byte转uint32、byte转float等
  • .NET性能优化(文摘)
  • @ComponentScan比较