stl function扩展(一)
- #ifndef _FUNCTION_LIB_H_
- #define _FUNCTION_LIB_H_
- #include <functional>
- namespace function_lib
- {
- /*
- *仿函数功能:将二元仿函数的第一个参数绑定,使之成为一元仿函数;
- */
- template<class _Fn2>
- class binder1st
- : public std::binary_function<typename _Fn2::first_argument_type,
- typename _Fn2::second_argument_type,
- typename _Fn2::result_type>
- {
- public:
- typedef std::binary_function<typename _Fn2::first_argument_type,
- typename _Fn2::second_argument_type,
- typename _Fn2::result_type> _Base;
- typedef typename _Base::first_argument_type first_argument_type;
- typedef typename _Base::second_argument_type second_argument_type;
- typedef typename _Base::result_type result_type;
- binder1st(_Fn2& _Func,
- const first_argument_type &_Left)
- : op(_Func), value(_Left)
- {
- }
- result_type operator()(const second_argument_type &_Right)
- {
- return (op(value, _Right));
- }
- result_type operator()(second_argument_type &_Right)
- {
- return (op(value, _Right));
- }
- protected:
- _Fn2 op;
- typename _Fn2::first_argument_type value;
- };
- template<class _Fn2,
- class _Ty>
- function_lib::binder1st<_Fn2> bind1st(_Fn2& _Func, const _Ty& _Left)
- {
- typename _Fn2::first_argument_type _Val(_Left);
- return (function_lib::binder1st<_Fn2>(_Func, _Val));
- }
- /*
- *仿函数功能:将二元仿函数的第一个参数绑定,使之成为一元仿函数;
- */
- template<class _Fn2>
- class const_binder1st
- : public std::binary_function<typename _Fn2::first_argument_type,
- typename _Fn2::second_argument_type,
- typename _Fn2::result_type>
- {
- public:
- typedef std::binary_function<typename _Fn2::first_argument_type,
- typename _Fn2::second_argument_type,
- typename _Fn2::result_type> _Base;
- typedef typename _Base::first_argument_type first_argument_type;
- typedef typename _Base::second_argument_type second_argument_type;
- typedef typename _Base::result_type result_type;
- const_binder1st(_Fn2 &_Func,
- const first_argument_type &_Left)
- : op(_Func), value(_Left)
- {
- }
- result_type operator()(second_argument_type &_Right)
- {
- return (op(value, _Right));
- }
- protected:
- _Fn2 op;
- typename _Fn2::first_argument_type value;
- };
- template<class _Fn2,
- class _Ty>
- function_lib::const_binder1st<_Fn2> const_bind1st(_Fn2 &_Func, const _Ty& _Left)
- {
- typename _Fn2::first_argument_type _Val(_Left);
- return (function_lib::const_binder1st<_Fn2>(_Func, _Val));
- }
- /*
- *仿函数功能:通过指针,调用一个对象的成员函数,并传递一个参数;
- 如果成员函数声明中该参数为引用,则传递为引用;
- */
- template<class _Result,
- class _Ty,
- class _Arg>
- class obj_mem_fun_t : public std::unary_function<_Arg, void>
- {
- public:
- typedef _Result (_Ty::*_MemFunPtr)(_Arg);
- obj_mem_fun_t(_Ty *_pObj, _MemFunPtr _pMemFunPtr)
- : m_pObj(_pObj),
- m_pMemFunPtr(_pMemFunPtr)
- {
- }
- void operator ()(const _Arg &_arg)
- {
- (m_pObj->*m_pMemFunPtr)(_arg);
- }
- void operator ()(_Arg &_arg)
- {
- (m_pObj->*m_pMemFunPtr)(_arg);
- }
- private:
- _Ty *m_pObj;
- _MemFunPtr m_pMemFunPtr;
- };
- template<class _Result,
- class _Ty,
- class _Arg>
- obj_mem_fun_t<_Result, _Ty, _Arg>
- obj_mem_fun(_Ty *_pObj, _Result (_Ty::*_pm)(_Arg))
- {
- return obj_mem_fun_t<_Result, _Ty, _Arg>(_pObj, _pm);
- }
- /*
- *仿函数功能:通过指针,调用一个对象的成员函数,并传递两个参数;
- 如果成员函数声明中该参数为引用,则传递为引用;
- */
- template<class _Result,
- class _Ty,
- class _LeftArg,
- class _RightArg>
- class obj_mem_fun1_t : public std::binary_function<_LeftArg, _RightArg, void>
- {
- public:
- typedef _Result (_Ty::*_MemFunPtr)(_LeftArg, _RightArg);
- obj_mem_fun1_t(_Ty *_pObj, _MemFunPtr _pMemFunPtr)
- : m_pObj(_pObj),
- m_pMemFunPtr(_pMemFunPtr)
- {
- }
- void operator ()(const _LeftArg &_left, const _RightArg &_right)
- {
- (m_pObj->*m_pMemFunPtr)(_left, _right);
- }
- void operator ()(_LeftArg &_left, _RightArg &_right)
- {
- (m_pObj->*m_pMemFunPtr)(_left, _right);
- }
- private:
- _Ty *m_pObj;
- _MemFunPtr m_pMemFunPtr;
- };
- template<class _Result,
- class _Ty,
- class _LeftArg,
- class _RightArg>
- obj_mem_fun1_t<_Result, _Ty, _LeftArg, _RightArg>
- obj_mem_fun(_Ty *_pObj, _Result (_Ty::*_pm)(_LeftArg, _RightArg))
- {
- return obj_mem_fun1_t<_Result, _Ty, _LeftArg, _RightArg>(_pObj, _pm);
- }
- /*
- *仿函数功能:通过指针,调用一个对象的const成员函数,并传递一个参数;
- 如果成员函数声明中该参数为引用,则传递为引用;
- */
- template<class _Result,
- class _Ty,
- class _Arg>
- class const_obj_mem_fun_t : public std::unary_function<_Arg, void>
- {
- public:
- typedef _Result (_Ty::*_MemFunPtr)(_Arg) const;
- const_obj_mem_fun_t(const _Ty *_pObj, _MemFunPtr _pMemFunPtr)
- : m_pObj(_pObj),
- m_pMemFunPtr(_pMemFunPtr)
- {
- }
- void operator ()(_Arg _arg)
- {
- (m_pObj->*m_pMemFunPtr)(_arg);
- }
- private:
- const _Ty *m_pObj;
- _MemFunPtr m_pMemFunPtr;
- };
- template<class _Result,
- class _Ty,
- class _Arg>
- const_obj_mem_fun_t<_Result, _Ty, _Arg>
- const_obj_mem_fun(const _Ty *_pObj, _Result (_Ty::*_pm)(_Arg) const)
- {
- return const_obj_mem_fun_t<_Result, _Ty, _Arg>(_pObj, _pm);
- }
- /*
- *仿函数功能:通过指针,调用一个对象的const成员函数,并传递两个参数;
- 如果成员函数声明中该参数为引用,则传递为引用;
- */
- template<class _Result,
- class _Ty,
- class _LeftArg,
- class _RightArg>
- class const_obj_mem_fun1_t : public std::binary_function<_LeftArg, _RightArg, void>
- {
- public:
- typedef _Result (_Ty::*_MemFunPtr)(_LeftArg, _RightArg) const;
- const_obj_mem_fun1_t(const _Ty *_pObj, _MemFunPtr _pMemFunPtr)
- : m_pObj(_pObj),
- m_pMemFunPtr(_pMemFunPtr)
- {
- }
- void operator ()(_LeftArg &_left, _RightArg &_right)
- {
- (m_pObj->*m_pMemFunPtr)(_left, _right);
- }
- private:
- const _Ty *m_pObj;
- _MemFunPtr m_pMemFunPtr;
- };
- template<class _Result,
- class _Ty,
- class _LeftArg,
- class _RightArg>
- const_obj_mem_fun1_t<_Result, _Ty, _LeftArg, _RightArg>
- const_obj_mem_fun(const _Ty *_pObj, _Result (_Ty::*_pm)(_LeftArg, _RightArg) const)
- {
- return const_obj_mem_fun1_t<_Result, _Ty, _LeftArg, _RightArg>(_pObj, _pm);
- }
- /*
- *仿函数功能:通过对象引用,调用对象的成员函数,并传递一个参数;
- 如果成员函数声明中该参数为引用,则传递为引用;
- */
- template<class _Result,
- class _Ty,
- class _Arg>
- class obj_mem_fun_ref_t : public std::unary_function<_Arg, void>
- {
- public:
- typedef _Result (_Ty::*_MemFunPtr)(_Arg);
- obj_mem_fun_ref_t(_Ty &_obj, _MemFunPtr _pMemFunPtr)
- : m_obj(_obj),
- m_pMemFunPtr(_pMemFunPtr)
- {
- }
- void operator ()(_Arg _arg)
- {
- (m_obj.*m_pMemFunPtr)(_arg);
- }
- private:
- _Ty &m_obj;
- _MemFunPtr m_pMemFunPtr;
- };
- template<class _Result,
- class _Ty,
- class _Arg>
- obj_mem_fun_ref_t<_Result,
- _Ty,
- _Arg>
- obj_mem_fun_ref(_Ty &_obj, _Result (_Ty::*_pm)(_Arg))
- {
- return obj_mem_fun_ref_t<_Result, _Ty, _Arg>(_obj, _pm);
- }
- /*
- *仿函数功能:通过对象引用,调用对象的const成员函数,并传递一个参数;
- 如果成员函数声明中该参数为引用,则传递为引用;
- */
- template<class _Result,
- class _Ty,
- class _Arg>
- class const_obj_mem_fun_ref_t : public std::unary_function<_Arg, void>
- {
- public:
- typedef _Result (_Ty::*_MemFunPtr)(_Arg) const;
- const_obj_mem_fun_ref_t(const _Ty &_obj, _MemFunPtr _pMemFunPtr)
- : m_obj(_obj),
- m_pMemFunPtr(_pMemFunPtr)
- {
- }
- void operator ()(_Arg _arg)
- {
- (m_obj.*m_pMemFunPtr)(_arg);
- }
- private:
- const _Ty &m_obj;
- _MemFunPtr m_pMemFunPtr;
- };
- template<class _Result,
- class _Ty,
- class _Arg>
- const_obj_mem_fun_ref_t<_Result,
- _Ty,
- _Arg>
- const_obj_mem_fun_ref(_Ty &_obj, _Result (_Ty::*_pm)(_Arg) const)
- {
- return const_obj_mem_fun_ref_t<_Result, _Ty, _Arg>(_obj, _pm);
- }
- /*
- *仿函数功能:通过对象的引用,调用对象的成员函数,并传递两个参数;
- 如果成员函数声明中该参数为引用,则传递为引用;
- */
- template<class _Result,
- class _Ty,
- class _LeftArg,
- class _RightArg>
- class obj_mem_fun1_ref_t : public std::binary_function<_LeftArg, _RightArg, void>
- {
- public:
- typedef _Result (_Ty::*_MemFunPtr)(_LeftArg, _RightArg);
- obj_mem_fun1_ref_t(_Ty &_obj, _MemFunPtr _pMemFunPtr)
- : m_obj(_obj),
- m_pMemFunPtr(_pMemFunPtr)
- {
- }
- void operator ()(_LeftArg _left, _RightArg _right)
- {
- (m_obj.*m_pMemFunPtr)(_left, _right);
- }
- private:
- _Ty &m_obj;
- _MemFunPtr m_pMemFunPtr;
- };
- template<class _Result,
- class _Ty,
- class _LeftArg,
- class _RightArg>
- obj_mem_fun1_ref_t<_Result,
- _Ty,
- _LeftArg,
- _RightArg>
- obj_mem_fun_ref(_Ty &_obj, _Result (_Ty::*_pm)(_LeftArg, _RightArg))
- {
- return obj_mem_fun1_ref_t<_Result, _Ty, _LeftArg, _RightArg>(_obj, _pm);
- }
- /*
- *仿函数功能:通过对象的引用,调用对象的const成员函数,并传递两个参数;
- 如果成员函数声明中该参数为引用,则传递为引用;
- */
- template<class _Result,
- class _Ty,
- class _LeftArg,
- class _RightArg>
- class const_obj_mem_fun1_ref_t : public std::binary_function<_LeftArg, _RightArg, void>
- {
- public:
- typedef _Result (_Ty::*_MemFunPtr)(_LeftArg, _RightArg) const;
- const_obj_mem_fun1_ref_t(const _Ty &_obj, _MemFunPtr _pMemFunPtr)
- : m_obj(_obj),
- m_pMemFunPtr(_pMemFunPtr)
- {
- }
- void operator ()(_LeftArg _left, _RightArg _right)
- {
- (m_obj.*m_pMemFunPtr)(_left, _right);
- }
- private:
- const _Ty &m_obj;
- _MemFunPtr m_pMemFunPtr;
- };
- template<class _Result,
- class _Ty,
- class _LeftArg,
- class _RightArg>
- const_obj_mem_fun1_ref_t<_Result,
- _Ty,
- _LeftArg,
- _RightArg>
- const_obj_mem_fun_ref(const _Ty &_obj, _Result (_Ty::*_pm)(_LeftArg, _RightArg) const)
- {
- return const_obj_mem_fun1_ref_t<_Result, _Ty, _LeftArg, _RightArg>(_obj, _pm);
- }
- }
- #endif
调用示例:
定义
- //这里省略组合框窗口创建过程...
- CComboBox m_combo;
- CString arString[] = {
- _T("1"),
- _T("2"),
- _T("3"),
- _T("4"),
- _T("5"),
- _T("6")
- };
- int nCount = sizeof(arString) / sizeof(CString);
1、
- auto combo1 = function_lib::obj_mem_fun(&m_combo, &CComboBox::AddString);
- std::for_each(arString, arString + nCount, combo1);
- m_combo.ResetContent();
2、
- auto combo2 = function_lib::bind1st(function_lib::obj_mem_fun(&m_combo, &CComboBox::InsertString), -1);
- std::for_each(arString, arString + nCount, combo2);
3、
- auto func = function_lib::const_obj_mem_fun1_t<void, CComboBox, int, CString&>(&m_combo, &CComboBox::GetLBText);
- auto combo3 = function_lib::const_bind1st(func, 0);
- std::for_each(arString, arString + nCount, combo3);
- //失败,无法将CString转化为LPTSTR
- //auto func = function_lib::const_obj_mem_fun1_t<int, CComboBox, int, LPTSTR >(&m_combo, &CComboBox::GetLBText);
- //auto combo3 = function_lib::const_bind1st(func, 0);
- //std::for_each(arString, arString + nCount, combo3);
4、
- m_combo.ResetContent();
- auto combo4 = function_lib::obj_mem_fun_ref(m_combo, &CComboBox::AddString);
- std::for_each(arString, arString + nCount, combo4);
5、
- m_combo.ResetContent();
- auto combo5 = function_lib::bind1st(function_lib::obj_mem_fun_ref(m_combo, &CComboBox::InsertString), 0);
- std::for_each(arString, arString + nCount, combo5);
6、
- auto func6 = function_lib::const_obj_mem_fun1_ref_t<void, CComboBox, int, CString&>(m_combo, &CComboBox::GetLBText);
- auto combo6 = function_lib::const_bind1st(func6, 0);
- std::for_each(arString, arString+ nCount, combo6);
stl function扩展(一)的更多相关文章
- 建议20:建议通过Function扩展类型
JavaScript允许为语言的基本数据类型定义方法.通过Object.prototype添加原型方法,该方法可被所有的对象,.这样的方法对函数,数组,字符串,数字,正则表达式和布尔值都适用.例如,通 ...
- Object、Function、String、Array原生对象扩展方法
JavaScript原生对象的api有些情况下使用并不方便,考虑扩展基于Object.Function.String.Array扩展,参考了prototype.js的部分实现,做了提取和修改,分享下: ...
- 标准非STL容器 : bitset
1. 概念 什么是"标准非STL容器"?标准非STL容器是指"可以认为它们是容器,但是他们并不满足STL容器的所有要求".前文提到的容器适配器stack.que ...
- jQuery $.fn 方法扩展~
//以下代码紧跟在引进的jquery.js代码后面 <script type="Text/JavaScript"> $(function (){ //扩展myName方 ...
- 【JavaScript】JS_Object跟Function的区别
JS_Object和Function的区别 我们本次的解释,主要通过下图 粗看该图,估计你不一定能看明白.不过接下来让我逐行向你解释. 最左侧:意思是,有两个对象f1和f2,他们是通过new Foo( ...
- jquery serialize()方法的扩展
Jquery提供的序列化表单方法serialize方法确实方便,但是我在使用的时候发现了一个弊端:当我使用type:“post”进行ajax请求的时候, 这个时候参数data:$("#myf ...
- Javascript中Function,Object,Prototypes,__proto__等概念详解
http://anykoro.sinaapp.com/2012/01/31/javascript%E4%B8%ADfunctionobjectprototypes__proto__%E7%AD%89% ...
- jQuery扩展两类函数(对象调用,静态调用)
作者:zccst 先看小例子: $(function(){ //扩展方式1-通过对新调用 $.fn.each1=function(){ console.log("hehehehe$.fn.f ...
- SGI STL内存配置器存在内存泄漏吗?
阅读了SGI的源码后对STL很是膜拜,很高质量的源码,从中学到了很多.温故而知新!下文中所有STL如无特殊说明均指SGI版本实现. STL 内存配置器 STL对内存管理最核心部分我觉得是其将C++对象 ...
随机推荐
- digitalocean注册验证账户、激活账号教程
注册digitalocean账号很简单,使用优惠链接注册digitalocean还能赠送10美元余额,digitalocean vps是优秀的SSD VPS,最便宜的套餐只要5美元/月. 由于中国大陆 ...
- sharepoint2010配置个人网站的offical方法 来自Jetluning的专栏
Configuring My Site in SharePoint 201 SharePoint My Sites are commonly referred to as “Facebook fo ...
- erlang dets
1.dets表包含set.bag.和duplicate bag 2.dets:open_file(TableName,Options)创建或打开表 3.Options 1){auto_save,Int ...
- 【Machine Learning in Action --4】朴素贝叶斯分类
1.概述 朴素贝叶斯分类是贝叶斯分类器的一种,贝叶斯分类算法是统计学的一种分类方法,利用概率统计知识进行分类,其分类原理就是利用贝叶斯公式根据某对象的先验 概率计算出其后验概率(即该对象属于某一类的概 ...
- 第一题 (Map)利用Map,完成下面的功能:
从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队.如果该 年没有举办世界杯,则输出:没有举办世界杯. 附:世界杯冠军以及对应的夺冠年份,请参考本章附录. 附录 1.历届世界杯冠 ...
- mysql之TIMESTAMP(时间戳)用法详解 [http://www.jb51.net/article/51794.htm]
一.TIMESTAMP的变体 TIMESTAMP时间戳在创建的时候可以有多重不同的特性,如: 1.在创建新记录和修改现有记录的时候都对这个数据列刷新: TIMESTAMP DEFAULT CURREN ...
- MapReduce初级案例
1.数据去重 "数据去重"主要是为了掌握和利用并行化思想来对数据进行有意义的筛选.统计大数据集上的数据种类个数.从网站日志中计算访问地等这些看似庞杂的任务都会涉及数据去重.下面就 ...
- nmon命令用法
用途 以交互方式显示本地系统统计信息并以记录方式记录系统统计信息. 语法 交互方式: nmon [ -h ] nmon [ -s < seconds > ] [ -c < count ...
- MFC中修改默认启动对话框方法
// CMyAppEApp 初始化 BOOL CMyAppEApp::InitInstance(){// 如果一个运行在 Windows XP 上的应用程序清单指定要// 使用 ComCtl32.dl ...
- 第三十三节,sys解释器相关模块
首先要引入import sys模块 sys.argv 功能:获取向脚本文件传入的参数,返回的列表,列表里的第一个元素是脚本文件路径和名称,后面的元素是传入的向脚本传入的参数 使用方法:sys.argv ...