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相 ...
随机推荐
- UIAutomator环境搭建
目录 下载.安装JDK&配置Java环境变量 下载.安装SDK.ADT&配置Android环境变量 下载.安装ANT&配置ANT环境变量 创建UIAutomator工程 UIA ...
- MyISAM和InnoDB的主要区别和应用场景
主要区别: 1).MyISAM是非事务安全型的,而InnoDB是事务安全型的. 2).MyISAM锁的粒度是表级,而InnoDB支持行级锁定. 3).MyISAM支持全文类型索引,而InnoDB不支持 ...
- ssh中文手册
ssh-keygen 中文手册 sshd_config 中文手册 sshd 中文手册
- JSP中文乱码问题终极解决方案
在介绍方法之前我们首先应该清楚具体的问题有哪些,笔者在本博客当中论述的JSP中文乱码问题有如下几个方面:页面乱码.参数乱码.表单乱码.源文件乱码.下面来逐一解决其中的乱码问题. 一.JSP页面中文乱码 ...
- 【第一次玩Travis CI】终于弄好了我的马鸭
真是不容易,我都要哭了.熬了半天终于弄完了!! 终于可以坐这儿挺会小曲,写写感受了. 作为一个程序写的不咋滴的程序员,倒是特别喜欢写博客,也是绝了. 高三的时候,用OneNote,后来转到Lofter ...
- X-Pack权限控制之给Kibana加上登录控制以及index_not_found_exception问题解决
无法查看索引下的日志问题解决 好事多磨,我们还是无法在Kibana下看到数据,究竟是怎么一回事呢? 笔者再次查看了logstash的控制台,又发现了如下错误: logstash outputs ela ...
- November 15th, 2017 Week 46th Wednesday
Of all the tribulations in this world, boredom is the one most hard to bear. 所有的苦难中,无聊是最难以忍受的. When ...
- Lr原理初识-hc课堂笔记
showslow web服务器-apache.ngix devops 需求调研-占1/3的时间. 架构拓扑图 APP端测试工具:JT.Vtest 进程是管理单元.线程是执行单元. 虚拟用户和真实用户是 ...
- Centos7常见问题及解决方法
1,在Centos7中用MariaDB代替了mysql数据库 2,mysql报错:/usr/sbin/mysqld:unknown variable 'default-character-set=ut ...
- java.lang.NoSuchMethodError: No static method getFont
最近在Android Studio升级3.0后,在AlertDialog弹窗时报出了如下问题: java.lang.NoSuchMethodError: No static method getFon ...