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>)

{

return T(0);

};

template<class T>

inline T identity_element(multiplies<T>)

{

return T(1);

};

//关系运算类仿函数

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;

}

}

时间: 2024-08-28 07:50:09

stl源码剖析 详细学习笔记 仿函数的相关文章

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

//---------------------------15/03/27---------------------------- //算法 { /* 质变算法:会改变操作对象之值 所有的stl算法都作用在由迭代器[first,last)所标示出来的区间上.质变算法 就是 运算过程会更改区间内的元素内容 非质变算法:和质变算法相反 */ /* stl算法的一般形式 1>所有的泛型算法的前两个参数都是一对迭代器,通常称为first和last,用以标示算法的操作区间 2>stl习惯采用前闭后开区间

stl源码剖析 详细学习笔记 算法总览

//****************************基本算法***************************** /* stl算法总览,不在stl标准规格的sgi专属算法,都以 *加以标记 算法名称              算法用途         质变                   所在文件 accumulate          元素累计            否                   <stl_numeric.h> adjacent_differenc

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

//---------------------------15/04/03---------------------------- /* 配接器概述: 1:adapter是一种设计模式:将一个class的接口转换为另一个class的接口,使得原本因接口不兼容而 不能合作的classes可以一起工作. 2:改变仿函数接口的,称为function adapter,改变容器接口的,称为container adapter, 改变迭代器接口的,称为iterator adapter. 3:container

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

//---------------------------15/04/05---------------------------- /* 空间配置器概述: 1:new操作包含两个阶段操作 1>调用::operator new配置内存(底层使用malloc来申请内存). 2>调用函数的构造函数,构造对象内容. delte和new一样,先调用析构函数,再调用::operator delete释放内存. 2:为了效率,stl把两个阶段分开来. 1>内存配置操作: alloc::allocate

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

//---------------------------15/03/29---------------------------- //****************************set相关算法***************************** /* 1>set相关算法一共有4种:并集(union),交集(intersection),差集(difference), 对称差集(symmetric difference). 2>set相关算法只接受set/multiset. 3

stl源码剖析 详细学习笔记 算法(4)

//---------------------------15/03/31---------------------------- //lower_bound(要求有序) template<class ForwardIterator, class T> inline ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last, const T& value) { return __lower_bound

stl源码剖析 详细学习笔记 算法(5)

//---------------------------15/04/01---------------------------- //inplace_merge(要求有序) template<class BidirectionalIterator> inline void inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last) { if(first

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

//---------------------------15/03/30---------------------------- //min_element template<class ForwardIterator> ForwardIterator min_element(ForwardIterator first, ForwardIterator last) { if(first == last) return first; ForwardIterator result = first

《STL源码剖析》学习笔记系列之七、八——仿函数和配接器

1. 仿函数 仿函数又名函数对象,具有函数性质的对象,就是传入一些参数,然后对参数进行某些运算,然后返回一个值.为了能够使行为类似函数,需要在类别定义中必须自定义function call 运算子operator(). 仿函数有如下几类:算术类仿函数(plus<T>.minus<T>)关系运算类仿函数(equal_to<T>.less<T>)逻辑运算类仿函数(logical_and<T>.logical_or<T>.logical_n