Simon 's Blog
» 做笔记做笔记
Toggle navigation
Simon 's Blog
HOME
总裁介绍
coper
zongcai
what
ARCH
TAGS
navigation
!!! STL仿函数
? STL源码 ?
? STL仿函数 ?
2019-07-24 14:55:51
527
0
0
simon88
? STL源码 ?
? STL仿函数 ?
[TOC] # 一元仿函数(unary_function) ## 内嵌型别(参数和返回型别,STL中迭代器的`型别萃取机`类似实现) ``` template<typename Arg,typename Result> struct unary_function { typedef Arg argument_type; typedef Result result_type; }; ``` ## 仿函数 ``` template<typename T> struct display:public unary_function<T,void> { void operator()(const T &x) const { std::cout << " " << x;}; }; ``` ## 仿函数适配器 ``` template<typename T> struct display_adapter { typename T::result_type operator()(const typename T::argument_type &x) const { std::cout << ": " << x; } }; ``` # 二元仿函数(binary_function) ## 内置型别(参数和返回值型别) ``` template<typename Arg1,typename Arg2,typename Result> struct binary_function { typedef Arg1 argment_type1; typedef Arg2 argment_type2; typedef Result result_type; }; ``` ## 仿函数 ``` template<typename T> struct plus { T operator()(const T &x, T &y) const { return x + y; } }; ``` ## 仿函数适配器 ``` template<typename T> struct plus_adapter { T t; typename T::argument_type1 v1; typename T::Result_type operator()(const typename T::argument_type2 &v2) const { return v2 + 1; } }; ``` # 算术运算仿函数 ``` template<typename T> struct plus :public binary_function<T,T,T> { T operator()(const T &x, const T &y) const { return x + y; } }; template<typename T> struct minus :public binary_function<T,T,T> { T operator()(const T &x, const T &y) const { return x - y; } }; template<typename T> struct multiplies :public binary_function<T,T,T> { T operator()(const T &x, const T &y) const { return x*y; } }; template<typename T> struct divides :public binary_function<T,T,T> { T operator()(const T &x, const T &y) const { return x / y; } }; template<typename T> struct modulus :public binary_function<T,T,T> { T operator()(const T &x, const T &y) const { return x%y; } }; template<typename T> struct negate :public unary_function<T,T> { T operator()(const T &x) const { return -x; } }; ``` # 关系运算仿函数 ``` template<typename T> struct equal_to :public binary_function<T,T,bool> { bool operator()(const T &x, const T &y)const { return x == y; } }; template<typename T> struct not_equal_to :public binary_function<T, T, bool> { bool operator()(const T &x, const T &y)const { return x != y; } }; template<typename T> struct greater :public binary_function<T,T,bool> { bool operator()(const T &x, const T &y)const { return x > y; } }; template<typename T> struct greater_equal :public binary_function<T,T,bool> { bool operator()(const T &x, const T &y) const { return x >= y; } }; template<typename T> struct less :public binary_function<T, T, bool> { bool operator()(const T &x, const T &y) const { return x < y; } }; template<typename T> struct less_equal :public binary_function<T, T, bool> { bool operator()(const T &x, const T &y) const { return x <= y; } }; ``` # 逻辑运算仿函数 ``` template<typename T> struct logical_and :public binary_function<T,T,bool> { bool operator()(const T &x, const T &y) const { return x && y; } }; template<typename T> struct logical_or :public binary_function<T,T,bool> { bool operator()(const T &x, const T &y) const { return x || y; } } template<typename T> struct logical_not :public unary_function<T, bool> { bool operator()(const T &x) const { return !x; } }; ``` # 证同(identity) ``` template<typename T> struct identity:public unary_function<T,T> { const T& operator()(const T &x, const T &y)const { return x; } }; ``` # 选择(select) ``` template<typename Pair> struct select1st :public unary_function<Pair,typename Pair::first_type> { const typename Pair::first_type& select1st()(const Pair &p) const { return p.first(); } }; template<typename Pair> struct select2nd :public unary_function<Pair, typename Pair::second_type> { const typename Pair::second_type& select2nd()(const Pair &p) const { return p.second(); } }; ``` # 投影(poject) ``` template<typename T1,typename T2> struct poject1st :public binary_function<T1, T2, T1> { const T1& operator()(const T1 &x, const T2 &y) const { return x; } }; template<typename T1,typename T2> struct poject2nd :public binary_function<T1, T2, T2> { const T2& operator()(const T1 &x, const T2 &y) const { return y; } }; ``` # 测试demo ``` void testFunctor() { functor::minus<int> m; cout << "minux:" << m(1, 2) << endl; vector<int> v = { 1,2,3,4,2,8,4,6,5,0,1,44,22,88}; int x = 0; x = std::inner_product(v.begin(), v.end(),v.begin(),10, functor::plus<int>(),functor::multiplies<int>()); for_each(v.begin(), v.end(), functor::display<int>()); cout << endl << "x:" << x << endl; cout << "sort:" << endl; std::vector<int> v2 = v; sort(v2.begin(), v2.end(), functor::greater<int>()); for_each(v2.begin(), v2.end(), functor::display<int>()); cout << endl; } ```
上一篇:
STL适配器的实现(iterator adapter)
下一篇:
stl::set算法
0
赞
527 人读过
新浪微博
微信
腾讯微博
QQ空间
人人网
提交评论
0
条评论
More...
<>