stl源码剖析 详细学习笔记 仿函数
//---------------------------15/04/01----------------------------
//仿函数是为了算法而诞生的,可以作为算法的一个参数,来自定义各种操作,比如比大小,返回bool值,对元素进行操作等
//虽然这些函数也能实现,但是如果配合配接器(adapter)可以产生更灵活的变化。
//为了使对象像函数一样,就必须重载operator()
//unary_function
template<class Arg,
class Result>
struct unary_function
{
//参数类型
typedef Arg argument_type;
//返回值类型
typedef Result result_type;
};
//binary_functione
//二元仿函数
template<class Arg1,
class Arg2, class Result>
struct binary_functione
{
typedef Arg1 first_argument_type;
typedef arg2 second_argument_type;
typedef Result result_type;
};
//算术类仿函数
template<class T>
struct plus :
public binary_functione<T, T, T>
{
T
operator()(const T& x,
const T& y) const
{
return x + y;
}
};
template<class T>
struct minus :
public binary_functione<T, T, T>
{
T
operator()(const T& x,
const T& y) const
{
return x - y;
}
};
template<class T>
struct multiplies :
public binary_functione<T, T, T>
{
T
operator()(const T& x,
const T& y) const
{
return x * y;
}
};
template<class T>
struct divides :
public binary_functione<T, T, T>
{
T
operator()(const T& x,
const T& y) const
{
return x / y;
}
};
template<class T>
struct modulus :
public binary_functione<T, T, T>
{
T
operator()(const T& x,
const T& y) const
{
return x & y;
}
};
template<class T>
struct negate:
public unary_function<T, T>
{
T operator()(const T& x)
const
{
return -x;
}
};
//证同元素,数值a与该元素做op操作会得到自己。
加法的证同元素为0 乘法为1
template<class T>
inline T identity_element(plus<T>)
{
);
};
template<class T>
inline T identity_element(multiplies<T>)
{
);
};
//关系运算类仿函数
template<class T>
struct equal_to :
public binary_functione<T, T,
bool>
{
bool operator()(const T& x,
const T& y) const
{
return x == y;
}
};
template<class T>
struct not_equal_to :
public binary_functione<T, T,
bool>
{
bool operator()(const T& x,
const T& y) const
{
return x != y;
}
};
template<class T>
struct greater :
public binary_functione<T, T,
bool>
{
bool operator()(const T& x,
const T& y) const
{
return x > y;
}
};
template<class T>
struct less :
public binary_functione<T, T,
bool>
{
bool operator()(const T& x,
const T& y) const
{
return x < y;
}
};
template<class T>
struct greater_equal :
public binary_functione<T, T,
bool>
{
bool operator()(const T& x,
const T& y) const
{
return x >= y;
}
};
template<class T>
struct less_equal :
public binary_functione<T, T,
bool>
{
bool operator()(const T& x,
const T& y) const
{
return x <= y;
}
};
//逻辑类仿函数
template<class T>
struct logical_and :
public binary_functione<T, T,
bool>
{
bool operator()(const T& x,
const T& y) const
{
return x && y;
}
};
template<class T>
struct logical_or :
public binary_functione<T, T,
bool>
{
bool operator()(const T& x,
const T& y) const
{
return x || y;
}
};
template<class T>
struct logical_not :
public unary_function<T,
bool>
{
bool operator()(const T& x)
const
{
return !x;
}
};
//证同函数,任何数通过此函数调用运算后返回原值。
template<class T>
struct identity :
public unary_function<T, T>
{
const T&
operator()(const T& x)
const
{
return x;
}
};
//选择函数
接受pair,传回第一个元素
template<class Pair>
struct select1st :
public unary_function<Pair,
typename Pair::first_type>
{
const typename Pair::first_type&
operator()(const Pair& x)
const
{
return x.first;
}
};
template<class Pair>
struct select2nd :
public unary_function<Pair,
typename Pair::second_type>
{
const typename Pair::second_type&
operator()(const Pair& x)
const
{
return x.second;
}
};
//投射函数:传回第一参数,忽略第二参数
template<class Arg1,
class Arg2>
struct project1st :
public binary_functione<Arg1, Arg2, Arg1>
{
Arg1
operator()(const Arg1& x,
const Arg2& y) const
{
return x;
}
}
template<class Arg1,
class Arg2>
struct project2nd :
public binary_functione<Arg1, Arg2, Arg2>
{
Arg2
operator()(const Arg1& x,
const Arg2& y) const
{
return y;
}
}
stl源码剖析 详细学习笔记 仿函数的更多相关文章
- stl源码剖析 详细学习笔记 hashtable
//---------------------------15/03/24---------------------------- //hashtable { /* 概述: sgi采用的是开链法完成h ...
- stl源码剖析 详细学习笔记 set map
// // set map.cpp // 笔记 // // Created by fam on 15/3/23. // // //---------------------------15/03 ...
- stl源码剖析 详细学习笔记 算法(1)
//---------------------------15/03/27---------------------------- //算法 { /* 质变算法:会改变操作对象之值 所有的stl算法都 ...
- stl源码剖析 详细学习笔记 RB_tree (2)
//---------------------------15/03/22---------------------------- //一直好奇KeyOfValue是什么,查了下就是一个和仿函数差不多 ...
- stl源码剖析 详细学习笔记 RB_tree (1)
// // RB_tree_STL.cpp // 笔记 // // Created by fam on 15/3/21. // // #include "RB_tree_STL.h&q ...
- stl源码剖析 详细学习笔记heap
// // heap.cpp // 笔记 // // Created by fam on 15/3/15. // // //---------------------------15/03/15 ...
- stl源码剖析 详细学习笔记 空间配置器
//---------------------------15/04/05---------------------------- /* 空间配置器概述: 1:new操作包含两个阶段操作 1>调 ...
- stl源码剖析 详细学习笔记 配接器
//---------------------------15/04/03---------------------------- /* 配接器概述: 1:adapter是一种设计模式:将一个clas ...
- stl源码剖析 详细学习笔记 算法(2)
//---------------------------15/03/29---------------------------- //****************************set相 ...
随机推荐
- 【SPL标准库专题(7)】 Datastructures:SplHeap & SplMaxHeap & SplMinHeap
堆(Heap)就是为了实现优先队列而设计的一种数据结构,它是通过构造二叉堆(二叉树的一种)实现.根节点最大的堆叫做最大堆或大根堆,根节点最小的堆叫做最小堆或小根堆.二叉堆还常用于排序(堆排序). 类摘 ...
- Prometheus Node_exporter 之 Network Netstat ICMP
Network Netstat ICMP /proc/net/snmp 1. ICMP Errors 1 type: GraphUnit: shortLabel: Messages out (-) / ...
- Saltstack安装配置过程
一.安装配置 1.服务器配置情况 三台服务器,均需要关闭iptables和selinux(否则salt执行指令无效) master: 192.168.60.139 centos slave: 192. ...
- abp框架里使用Redis
首先引用 nuget Abp.RedisCache 在 appsettings.json加上Redis服务器配置 "RedisCache": { "ConnectionS ...
- VC 调试版(Debug Version)和发行版(Release Version)
调试是纠正或修改代码,使之可以顺利地编译.运行的过程.为此,VC IDE提供了功能强大的调试和跟踪工具. 1.1.1 调试版(Debug Version)和发行版(Release Version) 开 ...
- Shell学习---Shell脚本的静态检查工具shellcheck
Shell脚本的静态检查工具shellcheck ubuntu下 apt install shellcheck ,即可安装shellcheck.写完shell脚本,记得用它检查一下,能给你点建议的.要 ...
- 【史上最全】申请配置阿里云服务器,并部署IIS和开发环境,项目上线经验
最近一年在实验室做web后端开发,涉及到一些和服务器搭建及部署上线项目的相关经验,写个帖子和小伙伴们分享,一同进步! 首先谈一下,为什么越来越多中小型公司/实验室,部署项目的趋势都是在云服务器而不是普 ...
- git bash安装和基本设置
Part1:安装步骤 1.下载地址: https://git-for-windows.github.io/ 2.下载完成后安装,安装路径自己选择,其他的选项参照下图: 其他的一步一步往下即可,最后Fi ...
- XtraEditors七、ProgressBarControl、MarqueeProgressBarControl、ProgressPanel控件
一.ProgressBarControl 进度条控件 效果如下: 示例代码: using System; using System.Collections.Generic; using System. ...
- Django商城项目笔记No.9用户部分-注册接口签发JWTtoken
Django商城项目笔记No.9用户部分-注册接口签发JWTtoken 我们在验证完用户的身份后(检验用户名和密码),需要向用户签发JWT,在需要用到用户身份信息的时候,还需核验用户的JWT. 关于签 ...