//---------------------------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源码剖析 详细学习笔记 仿函数的更多相关文章

  1. stl源码剖析 详细学习笔记 hashtable

    //---------------------------15/03/24---------------------------- //hashtable { /* 概述: sgi采用的是开链法完成h ...

  2. stl源码剖析 详细学习笔记 set map

    // //  set map.cpp //  笔记 // //  Created by fam on 15/3/23. // // //---------------------------15/03 ...

  3. stl源码剖析 详细学习笔记 算法(1)

    //---------------------------15/03/27---------------------------- //算法 { /* 质变算法:会改变操作对象之值 所有的stl算法都 ...

  4. stl源码剖析 详细学习笔记 RB_tree (2)

    //---------------------------15/03/22---------------------------- //一直好奇KeyOfValue是什么,查了下就是一个和仿函数差不多 ...

  5. stl源码剖析 详细学习笔记 RB_tree (1)

    // //  RB_tree_STL.cpp //  笔记 // //  Created by fam on 15/3/21. // // #include "RB_tree_STL.h&q ...

  6. stl源码剖析 详细学习笔记heap

    // //  heap.cpp //  笔记 // //  Created by fam on 15/3/15. // // //---------------------------15/03/15 ...

  7. stl源码剖析 详细学习笔记 空间配置器

    //---------------------------15/04/05---------------------------- /* 空间配置器概述: 1:new操作包含两个阶段操作 1>调 ...

  8. stl源码剖析 详细学习笔记 配接器

    //---------------------------15/04/03---------------------------- /* 配接器概述: 1:adapter是一种设计模式:将一个clas ...

  9. stl源码剖析 详细学习笔记 算法(2)

    //---------------------------15/03/29---------------------------- //****************************set相 ...

随机推荐

  1. python之demo1----改编自turtle.py文件中的demo

    """ 改编自turtle.py自带demo 执行 python -m turtledemo 命令查看系统内置demo的源码 绘制:需要通过import turtle引入 ...

  2. windows使用

    将桌面.我的文档.收藏夹等转移到其他盘 方法很多,介绍如下: 一.新装的系统,桌面.我的文档.收藏夹等都是默认在C盘的,并且这些数据都是用户经常用到的一些数据.为了避免以后系统崩溃所带来的危险,最好的 ...

  3. 第四次作业 重写equals方法

    使用上几次用到得User实体类,在其中重写equals方法. @Override public boolean equals(Object obj) { if(obj==null)return fal ...

  4. HashSet集合的add()方法的源码

    interface Collection { ... } interface Set extends Collection { ... } class HashSet implements Set { ...

  5. JDK5的新特性之 增强for

      package cn.itcast.day19.foreach; import java.util.ArrayList; import java.util.Collection; import j ...

  6. NOIP2018考前抱佛脚——数据结构基础及STL实现

    目录 动态数组 栈 队列 优先队列 动态数组 srand(time(0)); std::vector<int> qwq; for(int i = 1;i <= 10;++i) qwq ...

  7. 关于react的一点工作总结

    首先,react是Facebook开发的一套前端框架,仅仅是MVC中的V.核心思想是“封装组件”,组件封装后可以作为一个独立的实体被引入到新的组件中,这样新的组件就又是一个实体了,由于组件的实现了可复 ...

  8. chrome主页被篡改为360导航之解决方式

    昨天,安装某款游戏之后,发现chrome的主页被篡改为360导航. 进入chrome设置改动主页,又一次启动chrome还是360导航,后来发如今chrome快捷方式的属性中目标后面加了一串360导航 ...

  9. Scala学习之路 (六)Scala的类、对象、继承、特质

    一.类 1.类的定义 scala语言中没有static成员存在,但是scala允许以某种方式去使用static成员这个就是伴生机制,所谓伴生,就是在语言层面上,把static成员和非static成员用 ...

  10. maven项目中,lib目录下有自己私有的包,则需要配置一下代码,然后进行打包

    <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compi ...