STL源码剖析笔记——仿函数(函数对象)

系列文章目录

STL源码剖析笔记——迭代器
STL源码剖析笔记——vector
STL源码剖析笔记——list
STL源码剖析笔记——deque、stack,queue
STL源码剖析笔记——Binary Heap、priority_queue
STL源码剖析笔记——AVL-tree、RB-tree、set、map、mutiset、mutimap
STL源码剖析笔记——哈希表、unordered_set、unordered_map、unordered_mutiset、unordered_mutimap
STL源码剖析笔记——仿函数(函数对象)
STL源码剖析笔记——适配器(adapters)


1. 定义

  仿函数(functors)在C++标准规格定案后所采用的新名称是函数对象(function objects),是一种具有函数特质的对象。仿函数通过重载operator( )实现函数行为,实际上就是一个“行为类似函数”的对象。

在这里插入图片描述

2. 型别

  仿函数的相应型别主要用来表现函数参数型别和传回值型别。为了方便起见, <stl_function.h>定义了两个classes,分别代表一元仿函数和二元仿函数(STL不支持三元仿函数),其中没有任何data members或member functions,唯有一些型别定义。任何仿函数,只要依个人需求选择继承其中一个class,便自动拥有 了那些相应型别,也就自动拥有了配接能力。

(1) 一元函数(unary_function)

unary_function的定义为:

/ / STL规定,每一个Adaptable Unary Function都应该继承此类别
template <class Arg, class Result>
struct unary_function {
   
	typedef Arg argument_type;
	typedef Result result_type;
};

一元函数拥有一个参数型别和一个返回值型别,可以通过继承这个类来获取型别。

//以下仿函数继承了 unary__function.
template <class T>
struct negate:public unary_function<T, T> {
   
	T operator()(const T& x) const {
    return -x;} 
};

(2) 二元函数(binary_function)

binary_function的定义为:

/ / STL规定,每一个Adaptable Binary Function都应该继承此类别 
template <class Argl, class Arg2 , class Result> 
struct binary_function {
   
	typedef Argl first_argument_type;
	typedef Arg2 second_argument_type;
	typedef Result result_type; 
};

二元函数拥有两个参数型别和一个返回值型别,可以通过继承这个类来获取型别。

/ /以下仿函数继承了 binary-f unction.
template <class T> 
struct plus:public binar_functior<T, T, T> {
   
	T operator()(const T& x, const T& y) const {
    return x + y; } 
};

3. 算术类 (Arithmetic)仿函数

STL内建的“算术类仿函数”,支持加法、减法、乘法、除法、模数(余数, modulus)和否定(negation)运算。除了 “否定”运算为一元运算,其它都是二元运算。
  •加法:plus< T >
  •减法: minus< T >
  •乘法:multiplies< T >
  •除法:divides< T >
  •模取(modulus) :modulus< T >
  • 否定(negation) :negate< T >

//以下6个为算术类(Arithmetic)仿函数
template <class T>
struct plus:public binary_function<T, T, T> {
   
	T operator{
   )(const T& x, const T& y) const {
    return x + y;) 
};

template <class T>
struct minus:public binary_function<T, T, T> {
   
	T operator()(const T& x, const T& y) const {
    return x - y; } 
};

template <class T>
struct multiplies:public binary_function<T, T, T> {
   
	T operator()(const T& x, const T& y) const {
    return x * y;} 
};

template <class T>
struct divides:public binary_function<T, T, T> {
   
	T operator()(const T& xz const T& y) const {
    return x / y; } 
};

template <class T>
struct modulus:public binary_function<T, T, T> {
   
	T operator()(const T& x, const T& y) const {
    return x % y; } 
};

template <class T>
struct negate:public unary_functio<T, T> {
   
	T operator()(const T& x) const {
    return -x; }
};

一个仿函数有两种用法:第一种是实例化对象,并且运用函数功能

plus<int> plusobj;//创建对象

cout << plusobj(3,5) << endl;调用()重载,输出为8

第二种是创建一个临时对象,并且运用函数功能

//以下直接以仿函数的临时对象履行函数功能
//语法分析:functor<T> ()是一个临时对象,后面再接一对小括号 
//意指调用operator()
cout << plus<int>()(3,5) << endl; // 8

4. 关系运算类 (Relational ) 仿函数

STL内建的“关系运算类仿函数”支持了等于、不等于、大于、大于等于、 小于、小于等于六种运算。每一个都是二元运算。
  •等于(equality) :equal_to< T >
  •不等于(inequality) : not_equal_to< T >
  •大于(greater than) : greater< T >
  •大于或等于(greater than or equal) : greater_equal< T >
  •小于(lessthan) : less< T >
  •小于或等于(less than or equal) : less_equal< T >

//以下6个为关系运算类(Relational)仿函数
template <class T>
struct equal_to:public binar_function<T, T, bool> {
   
	bool operator()(const T& x, const T& y) const {
    return x == y; } 
};

template <class T>
struct not_equal_to:public binar_function<T, T, bool> {
   
	bool operator()(const T& xf const T& y) const {
    return x 1= y;}
};

template <class T>
struct greater:public binar_function<T, T, bool> {
   
	bool operator()(const T& x, const T& y) const {
    return x > y;}
};

template <class T>
struct less:public binar_function<T, T, bool> {
   
	bool operator()(const T& x, const T& y) const {
    return x < y;}
};

template <class T>
struct greater_equal:public binar_function<T, T, bool> {
   
	bool operatort)(const T& xz const T& y) const {
    return x >= y;}
};

template <class T>
struct less_equal:public binar_function<T, T, bool> {
   
	bool operator()(const T& x, const T& y) const {
    return x <= y;}
};

5. 逻辑运算类(Logical)仿函数

STL内建的“逻辑运算类仿函数”支持了逻辑运算中的And、Or、Not三 种运算,其中And和Or为二元运算,Not为一元运算。
  •逻辑运算 And: logical_and< T >
  •逻辑运算 Or: logical_or< T >
  •逻辑运算 Not: logical_not< T >

//以下3个为逻辑运算类(Logical)仿函数
template <class T>
struct logical_and:public binary_function<T, T, bool> {
   
	bool operator()(const T& x, const T& y) const {
    return x && y; } 
};
template <class T>
struct logical_or:public binary_function<T, T, bool> {
   
	bool operator() (const T& x, const T& y) const {
    return x || y; } 
};
template <class T>
	struct logical_not:public binary_function<T, T, bool> {
    
	bool operator()(const T& x) const {
    return !x; } 
};

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2023-12-12 08:46:07       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-12 08:46:07       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-12 08:46:07       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-12 08:46:07       20 阅读

热门阅读

  1. Spark

    2023-12-12 08:46:07       30 阅读
  2. 09 # 函数相关知识点梳理

    2023-12-12 08:46:07       35 阅读
  3. HTTP常见错误码原因以及解决办法

    2023-12-12 08:46:07       38 阅读
  4. MySQL常用基础命令

    2023-12-12 08:46:07       37 阅读
  5. react相关hooks(二)

    2023-12-12 08:46:07       36 阅读
  6. 浏览器面试

    2023-12-12 08:46:07       32 阅读
  7. Elasticsearch

    2023-12-12 08:46:07       31 阅读
  8. 什么是Scss

    2023-12-12 08:46:07       30 阅读
  9. Matlab矩阵中元素交换

    2023-12-12 08:46:07       39 阅读
  10. Linux中的几个重要指令

    2023-12-12 08:46:07       27 阅读
  11. 剑指 Offer(第2版)面试题 24:反转链表

    2023-12-12 08:46:07       45 阅读
  12. Network Repo Installation for Ubuntu(gds)

    2023-12-12 08:46:07       39 阅读