//---------------------------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. EntityFramework 贪婪加载与延迟加载以及资源回收

    EntityFramework的资源回收 1) Using 内包含Entity的上下文关系,对俩表做Add操作,最好可以直接写一个 entity.SaveChanges(); 完成两张表的同时add操 ...

  2. Jenkins自动构建的几种方式

    1.远程URL构建 在任务配置处的构建触发器中选择远程触发,例如,在下图框中输入abc,则只需要在网页上输入地址:Jenkins_URL/job/工程名/build?token=abc 2.利用cur ...

  3. 缓存那些事-zz

    https://tech.meituan.com/cache_about.html 前言 一般而言,现在互联网应用(网站或App)的整体流程,可以概括如图1所示,用户请求从界面(浏览器或App界面)到 ...

  4. Web Api跨域访问配置及调用示例

    1.Web Api跨域访问配置. 在Web.config中的system.webServer内添加以下代码: <httpProtocol> <customHeaders> &l ...

  5. systemd-analyze – 在Linux中查找系统启动性能统计信息

    您是否在使用 systemd 系统和服务管理器,并且您的 Linux 系统需要较长时间才能启动,或者您希望查看系统启动性能的报告? 如果是的话,你已经登陆了正确的地方. 在本文中,我们将向您展示如何使 ...

  6. Qt在控件未显示时如何获取正确的控件尺寸

    因为打算全屏显示一个对话框,而对话框内有几个QLabel的尺寸要在确定QLabel可用的最大尺寸后,再根据内容调整一次,所以在对话框构造函数内就想确定QLabel的最大尺寸,但因为QWidget::u ...

  7. 模拟的confirm

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title> ...

  8. 【Python】新建自定义个数的自定义长度名字

    # -*- coding:utf-8 -*- import random def CreateRandomName(number,length): """ :param ...

  9. Angular2学习笔记(1)——Hello World

    1. 写在前面 之前基于Electron写过一个Markdown编辑器.就其功能而言,主要功能已经实现,一些小的不影响使用的功能由于时间关系还没有完成:但就代码而言,之前主要使用的是jQuery,由于 ...

  10. JavaScript中的单例模式

    单例模式 在JavaScript中,单例(Singleton)模式是最基本又最有用的模式之一.这种模式提供了一种将代码组织为一个逻辑单元的手段,这个逻辑单元中的代码可以通过单一的变量进行访问.确保单例 ...