1. #ifndef _FUNCTION_LIB_H_
  2. #define _FUNCTION_LIB_H_
  3.  
  4. #include <functional>
  5.  
  6. namespace function_lib
  7. {
  8. /*
  9. *仿函数功能:将二元仿函数的第一个参数绑定,使之成为一元仿函数;
  10. */
  11. template<class _Fn2>
  12. class binder1st
  13. : public std::binary_function<typename _Fn2::first_argument_type,
  14. typename _Fn2::second_argument_type,
  15. typename _Fn2::result_type>
  16. {
  17. public:
  18. typedef std::binary_function<typename _Fn2::first_argument_type,
  19. typename _Fn2::second_argument_type,
  20. typename _Fn2::result_type> _Base;
  21. typedef typename _Base::first_argument_type first_argument_type;
  22. typedef typename _Base::second_argument_type second_argument_type;
  23. typedef typename _Base::result_type result_type;
  24.  
  25. binder1st(_Fn2& _Func,
  26. const first_argument_type &_Left)
  27. : op(_Func), value(_Left)
  28. {
  29. }
  30.  
  31. result_type operator()(const second_argument_type &_Right)
  32. {
  33. return (op(value, _Right));
  34. }
  35.  
  36. result_type operator()(second_argument_type &_Right)
  37. {
  38. return (op(value, _Right));
  39. }
  40.  
  41. protected:
  42. _Fn2 op;
  43. typename _Fn2::first_argument_type value;
  44. };
  45.  
  46. template<class _Fn2,
  47. class _Ty>
  48. function_lib::binder1st<_Fn2> bind1st(_Fn2& _Func, const _Ty& _Left)
  49. {
  50. typename _Fn2::first_argument_type _Val(_Left);
  51. return (function_lib::binder1st<_Fn2>(_Func, _Val));
  52. }
  53.  
  54. /*
  55. *仿函数功能:将二元仿函数的第一个参数绑定,使之成为一元仿函数;
  56. */
  57. template<class _Fn2>
  58. class const_binder1st
  59. : public std::binary_function<typename _Fn2::first_argument_type,
  60. typename _Fn2::second_argument_type,
  61. typename _Fn2::result_type>
  62. {
  63. public:
  64. typedef std::binary_function<typename _Fn2::first_argument_type,
  65. typename _Fn2::second_argument_type,
  66. typename _Fn2::result_type> _Base;
  67. typedef typename _Base::first_argument_type first_argument_type;
  68. typedef typename _Base::second_argument_type second_argument_type;
  69. typedef typename _Base::result_type result_type;
  70.  
  71. const_binder1st(_Fn2 &_Func,
  72. const first_argument_type &_Left)
  73. : op(_Func), value(_Left)
  74. {
  75. }
  76.  
  77. result_type operator()(second_argument_type &_Right)
  78. {
  79. return (op(value, _Right));
  80. }
  81.  
  82. protected:
  83. _Fn2 op;
  84. typename _Fn2::first_argument_type value;
  85. };
  86.  
  87. template<class _Fn2,
  88. class _Ty>
  89. function_lib::const_binder1st<_Fn2> const_bind1st(_Fn2 &_Func, const _Ty& _Left)
  90. {
  91. typename _Fn2::first_argument_type _Val(_Left);
  92. return (function_lib::const_binder1st<_Fn2>(_Func, _Val));
  93. }
  94.  
  95. /*
  96. *仿函数功能:通过指针,调用一个对象的成员函数,并传递一个参数;
  97. 如果成员函数声明中该参数为引用,则传递为引用;
  98. */
  99. template<class _Result,
  100. class _Ty,
  101. class _Arg>
  102. class obj_mem_fun_t : public std::unary_function<_Arg, void>
  103. {
  104. public:
  105. typedef _Result (_Ty::*_MemFunPtr)(_Arg);
  106.  
  107. obj_mem_fun_t(_Ty *_pObj, _MemFunPtr _pMemFunPtr)
  108. : m_pObj(_pObj),
  109. m_pMemFunPtr(_pMemFunPtr)
  110. {
  111. }
  112.  
  113. void operator ()(const _Arg &_arg)
  114. {
  115. (m_pObj->*m_pMemFunPtr)(_arg);
  116. }
  117.  
  118. void operator ()(_Arg &_arg)
  119. {
  120. (m_pObj->*m_pMemFunPtr)(_arg);
  121. }
  122.  
  123. private:
  124. _Ty *m_pObj;
  125. _MemFunPtr m_pMemFunPtr;
  126. };
  127.  
  128. template<class _Result,
  129. class _Ty,
  130. class _Arg>
  131. obj_mem_fun_t<_Result, _Ty, _Arg>
  132. obj_mem_fun(_Ty *_pObj, _Result (_Ty::*_pm)(_Arg))
  133. {
  134. return obj_mem_fun_t<_Result, _Ty, _Arg>(_pObj, _pm);
  135. }
  136.  
  137. /*
  138. *仿函数功能:通过指针,调用一个对象的成员函数,并传递两个参数;
  139. 如果成员函数声明中该参数为引用,则传递为引用;
  140. */
  141. template<class _Result,
  142. class _Ty,
  143. class _LeftArg,
  144. class _RightArg>
  145. class obj_mem_fun1_t : public std::binary_function<_LeftArg, _RightArg, void>
  146. {
  147. public:
  148. typedef _Result (_Ty::*_MemFunPtr)(_LeftArg, _RightArg);
  149.  
  150. obj_mem_fun1_t(_Ty *_pObj, _MemFunPtr _pMemFunPtr)
  151. : m_pObj(_pObj),
  152. m_pMemFunPtr(_pMemFunPtr)
  153. {
  154. }
  155.  
  156. void operator ()(const _LeftArg &_left, const _RightArg &_right)
  157. {
  158. (m_pObj->*m_pMemFunPtr)(_left, _right);
  159. }
  160. void operator ()(_LeftArg &_left, _RightArg &_right)
  161. {
  162. (m_pObj->*m_pMemFunPtr)(_left, _right);
  163. }
  164.  
  165. private:
  166. _Ty *m_pObj;
  167. _MemFunPtr m_pMemFunPtr;
  168. };
  169.  
  170. template<class _Result,
  171. class _Ty,
  172. class _LeftArg,
  173. class _RightArg>
  174. obj_mem_fun1_t<_Result, _Ty, _LeftArg, _RightArg>
  175. obj_mem_fun(_Ty *_pObj, _Result (_Ty::*_pm)(_LeftArg, _RightArg))
  176. {
  177. return obj_mem_fun1_t<_Result, _Ty, _LeftArg, _RightArg>(_pObj, _pm);
  178. }
  179.  
  180. /*
  181. *仿函数功能:通过指针,调用一个对象的const成员函数,并传递一个参数;
  182. 如果成员函数声明中该参数为引用,则传递为引用;
  183. */
  184. template<class _Result,
  185. class _Ty,
  186. class _Arg>
  187. class const_obj_mem_fun_t : public std::unary_function<_Arg, void>
  188. {
  189. public:
  190. typedef _Result (_Ty::*_MemFunPtr)(_Arg) const;
  191.  
  192. const_obj_mem_fun_t(const _Ty *_pObj, _MemFunPtr _pMemFunPtr)
  193. : m_pObj(_pObj),
  194. m_pMemFunPtr(_pMemFunPtr)
  195. {
  196. }
  197.  
  198. void operator ()(_Arg _arg)
  199. {
  200. (m_pObj->*m_pMemFunPtr)(_arg);
  201. }
  202.  
  203. private:
  204. const _Ty *m_pObj;
  205. _MemFunPtr m_pMemFunPtr;
  206. };
  207.  
  208. template<class _Result,
  209. class _Ty,
  210. class _Arg>
  211. const_obj_mem_fun_t<_Result, _Ty, _Arg>
  212. const_obj_mem_fun(const _Ty *_pObj, _Result (_Ty::*_pm)(_Arg) const)
  213. {
  214. return const_obj_mem_fun_t<_Result, _Ty, _Arg>(_pObj, _pm);
  215. }
  216.  
  217. /*
  218. *仿函数功能:通过指针,调用一个对象的const成员函数,并传递两个参数;
  219. 如果成员函数声明中该参数为引用,则传递为引用;
  220. */
  221. template<class _Result,
  222. class _Ty,
  223. class _LeftArg,
  224. class _RightArg>
  225. class const_obj_mem_fun1_t : public std::binary_function<_LeftArg, _RightArg, void>
  226. {
  227. public:
  228. typedef _Result (_Ty::*_MemFunPtr)(_LeftArg, _RightArg) const;
  229.  
  230. const_obj_mem_fun1_t(const _Ty *_pObj, _MemFunPtr _pMemFunPtr)
  231. : m_pObj(_pObj),
  232. m_pMemFunPtr(_pMemFunPtr)
  233. {
  234. }
  235.  
  236. void operator ()(_LeftArg &_left, _RightArg &_right)
  237. {
  238. (m_pObj->*m_pMemFunPtr)(_left, _right);
  239. }
  240.  
  241. private:
  242. const _Ty *m_pObj;
  243. _MemFunPtr m_pMemFunPtr;
  244. };
  245.  
  246. template<class _Result,
  247. class _Ty,
  248. class _LeftArg,
  249. class _RightArg>
  250. const_obj_mem_fun1_t<_Result, _Ty, _LeftArg, _RightArg>
  251. const_obj_mem_fun(const _Ty *_pObj, _Result (_Ty::*_pm)(_LeftArg, _RightArg) const)
  252. {
  253. return const_obj_mem_fun1_t<_Result, _Ty, _LeftArg, _RightArg>(_pObj, _pm);
  254. }
  255.  
  256. /*
  257. *仿函数功能:通过对象引用,调用对象的成员函数,并传递一个参数;
  258. 如果成员函数声明中该参数为引用,则传递为引用;
  259. */
  260. template<class _Result,
  261. class _Ty,
  262. class _Arg>
  263. class obj_mem_fun_ref_t : public std::unary_function<_Arg, void>
  264. {
  265. public:
  266. typedef _Result (_Ty::*_MemFunPtr)(_Arg);
  267.  
  268. obj_mem_fun_ref_t(_Ty &_obj, _MemFunPtr _pMemFunPtr)
  269. : m_obj(_obj),
  270. m_pMemFunPtr(_pMemFunPtr)
  271. {
  272. }
  273.  
  274. void operator ()(_Arg _arg)
  275. {
  276. (m_obj.*m_pMemFunPtr)(_arg);
  277. }
  278.  
  279. private:
  280. _Ty &m_obj;
  281. _MemFunPtr m_pMemFunPtr;
  282. };
  283.  
  284. template<class _Result,
  285. class _Ty,
  286. class _Arg>
  287. obj_mem_fun_ref_t<_Result,
  288. _Ty,
  289. _Arg>
  290. obj_mem_fun_ref(_Ty &_obj, _Result (_Ty::*_pm)(_Arg))
  291. {
  292. return obj_mem_fun_ref_t<_Result, _Ty, _Arg>(_obj, _pm);
  293. }
  294.  
  295. /*
  296. *仿函数功能:通过对象引用,调用对象的const成员函数,并传递一个参数;
  297. 如果成员函数声明中该参数为引用,则传递为引用;
  298. */
  299. template<class _Result,
  300. class _Ty,
  301. class _Arg>
  302. class const_obj_mem_fun_ref_t : public std::unary_function<_Arg, void>
  303. {
  304. public:
  305. typedef _Result (_Ty::*_MemFunPtr)(_Arg) const;
  306.  
  307. const_obj_mem_fun_ref_t(const _Ty &_obj, _MemFunPtr _pMemFunPtr)
  308. : m_obj(_obj),
  309. m_pMemFunPtr(_pMemFunPtr)
  310. {
  311. }
  312.  
  313. void operator ()(_Arg _arg)
  314. {
  315. (m_obj.*m_pMemFunPtr)(_arg);
  316. }
  317.  
  318. private:
  319. const _Ty &m_obj;
  320. _MemFunPtr m_pMemFunPtr;
  321. };
  322.  
  323. template<class _Result,
  324. class _Ty,
  325. class _Arg>
  326. const_obj_mem_fun_ref_t<_Result,
  327. _Ty,
  328. _Arg>
  329. const_obj_mem_fun_ref(_Ty &_obj, _Result (_Ty::*_pm)(_Arg) const)
  330. {
  331. return const_obj_mem_fun_ref_t<_Result, _Ty, _Arg>(_obj, _pm);
  332. }
  333.  
  334. /*
  335. *仿函数功能:通过对象的引用,调用对象的成员函数,并传递两个参数;
  336. 如果成员函数声明中该参数为引用,则传递为引用;
  337. */
  338. template<class _Result,
  339. class _Ty,
  340. class _LeftArg,
  341. class _RightArg>
  342. class obj_mem_fun1_ref_t : public std::binary_function<_LeftArg, _RightArg, void>
  343. {
  344. public:
  345. typedef _Result (_Ty::*_MemFunPtr)(_LeftArg, _RightArg);
  346.  
  347. obj_mem_fun1_ref_t(_Ty &_obj, _MemFunPtr _pMemFunPtr)
  348. : m_obj(_obj),
  349. m_pMemFunPtr(_pMemFunPtr)
  350. {
  351. }
  352.  
  353. void operator ()(_LeftArg _left, _RightArg _right)
  354. {
  355. (m_obj.*m_pMemFunPtr)(_left, _right);
  356. }
  357.  
  358. private:
  359. _Ty &m_obj;
  360. _MemFunPtr m_pMemFunPtr;
  361. };
  362.  
  363. template<class _Result,
  364. class _Ty,
  365. class _LeftArg,
  366. class _RightArg>
  367. obj_mem_fun1_ref_t<_Result,
  368. _Ty,
  369. _LeftArg,
  370. _RightArg>
  371. obj_mem_fun_ref(_Ty &_obj, _Result (_Ty::*_pm)(_LeftArg, _RightArg))
  372. {
  373. return obj_mem_fun1_ref_t<_Result, _Ty, _LeftArg, _RightArg>(_obj, _pm);
  374. }
  375.  
  376. /*
  377. *仿函数功能:通过对象的引用,调用对象的const成员函数,并传递两个参数;
  378. 如果成员函数声明中该参数为引用,则传递为引用;
  379. */
  380. template<class _Result,
  381. class _Ty,
  382. class _LeftArg,
  383. class _RightArg>
  384. class const_obj_mem_fun1_ref_t : public std::binary_function<_LeftArg, _RightArg, void>
  385. {
  386. public:
  387. typedef _Result (_Ty::*_MemFunPtr)(_LeftArg, _RightArg) const;
  388.  
  389. const_obj_mem_fun1_ref_t(const _Ty &_obj, _MemFunPtr _pMemFunPtr)
  390. : m_obj(_obj),
  391. m_pMemFunPtr(_pMemFunPtr)
  392. {
  393. }
  394.  
  395. void operator ()(_LeftArg _left, _RightArg _right)
  396. {
  397. (m_obj.*m_pMemFunPtr)(_left, _right);
  398. }
  399.  
  400. private:
  401. const _Ty &m_obj;
  402. _MemFunPtr m_pMemFunPtr;
  403. };
  404.  
  405. template<class _Result,
  406. class _Ty,
  407. class _LeftArg,
  408. class _RightArg>
  409. const_obj_mem_fun1_ref_t<_Result,
  410. _Ty,
  411. _LeftArg,
  412. _RightArg>
  413. const_obj_mem_fun_ref(const _Ty &_obj, _Result (_Ty::*_pm)(_LeftArg, _RightArg) const)
  414. {
  415. return const_obj_mem_fun1_ref_t<_Result, _Ty, _LeftArg, _RightArg>(_obj, _pm);
  416. }
  417. }
  418.  
  419. #endif

调用示例:

定义

  1. //这里省略组合框窗口创建过程...
  2. CComboBox m_combo;
  3. CString arString[] = {
  4. _T("1"),
  5. _T("2"),
  6. _T("3"),
  7. _T("4"),
  8. _T("5"),
  9. _T("6")
  10. };
  11. int nCount = sizeof(arString) / sizeof(CString);

1、

  1. auto combo1 = function_lib::obj_mem_fun(&m_combo, &CComboBox::AddString);
  2. std::for_each(arString, arString + nCount, combo1);
  3. m_combo.ResetContent();

2、

  1. auto combo2 = function_lib::bind1st(function_lib::obj_mem_fun(&m_combo, &CComboBox::InsertString), -1);
  2. std::for_each(arString, arString + nCount, combo2);

3、

  1. auto func = function_lib::const_obj_mem_fun1_t<void, CComboBox, int, CString&>(&m_combo, &CComboBox::GetLBText);
  2. auto combo3 = function_lib::const_bind1st(func, 0);
  3. std::for_each(arString, arString + nCount, combo3);
  4. //失败,无法将CString转化为LPTSTR
  5. //auto func = function_lib::const_obj_mem_fun1_t<int, CComboBox, int, LPTSTR >(&m_combo, &CComboBox::GetLBText);
  6. //auto combo3 = function_lib::const_bind1st(func, 0);
  7. //std::for_each(arString, arString + nCount, combo3);

4、

  1. m_combo.ResetContent();
  2. auto combo4 = function_lib::obj_mem_fun_ref(m_combo, &CComboBox::AddString);
  3. std::for_each(arString, arString + nCount, combo4);

5、

  1. m_combo.ResetContent();
  2. auto combo5 = function_lib::bind1st(function_lib::obj_mem_fun_ref(m_combo, &CComboBox::InsertString), 0);
  3. std::for_each(arString, arString + nCount, combo5);

6、

  1. auto func6 = function_lib::const_obj_mem_fun1_ref_t<void, CComboBox, int, CString&>(m_combo, &CComboBox::GetLBText);
  2. auto combo6 = function_lib::const_bind1st(func6, 0);
  3. std::for_each(arString, arString+ nCount, combo6);

stl function扩展(一)的更多相关文章

  1. 建议20:建议通过Function扩展类型

    JavaScript允许为语言的基本数据类型定义方法.通过Object.prototype添加原型方法,该方法可被所有的对象,.这样的方法对函数,数组,字符串,数字,正则表达式和布尔值都适用.例如,通 ...

  2. Object、Function、String、Array原生对象扩展方法

    JavaScript原生对象的api有些情况下使用并不方便,考虑扩展基于Object.Function.String.Array扩展,参考了prototype.js的部分实现,做了提取和修改,分享下: ...

  3. 标准非STL容器 : bitset

    1. 概念 什么是"标准非STL容器"?标准非STL容器是指"可以认为它们是容器,但是他们并不满足STL容器的所有要求".前文提到的容器适配器stack.que ...

  4. jQuery $.fn 方法扩展~

    //以下代码紧跟在引进的jquery.js代码后面 <script type="Text/JavaScript"> $(function (){ //扩展myName方 ...

  5. 【JavaScript】JS_Object跟Function的区别

    JS_Object和Function的区别 我们本次的解释,主要通过下图 粗看该图,估计你不一定能看明白.不过接下来让我逐行向你解释. 最左侧:意思是,有两个对象f1和f2,他们是通过new Foo( ...

  6. jquery serialize()方法的扩展

    Jquery提供的序列化表单方法serialize方法确实方便,但是我在使用的时候发现了一个弊端:当我使用type:“post”进行ajax请求的时候, 这个时候参数data:$("#myf ...

  7. Javascript中Function,Object,Prototypes,__proto__等概念详解

    http://anykoro.sinaapp.com/2012/01/31/javascript%E4%B8%ADfunctionobjectprototypes__proto__%E7%AD%89% ...

  8. jQuery扩展两类函数(对象调用,静态调用)

    作者:zccst 先看小例子: $(function(){ //扩展方式1-通过对新调用 $.fn.each1=function(){ console.log("hehehehe$.fn.f ...

  9. SGI STL内存配置器存在内存泄漏吗?

    阅读了SGI的源码后对STL很是膜拜,很高质量的源码,从中学到了很多.温故而知新!下文中所有STL如无特殊说明均指SGI版本实现. STL 内存配置器 STL对内存管理最核心部分我觉得是其将C++对象 ...

随机推荐

  1. digitalocean注册验证账户、激活账号教程

    注册digitalocean账号很简单,使用优惠链接注册digitalocean还能赠送10美元余额,digitalocean vps是优秀的SSD VPS,最便宜的套餐只要5美元/月. 由于中国大陆 ...

  2. sharepoint2010配置个人网站的offical方法 来自Jetluning的专栏

    Configuring My Site in SharePoint 201   SharePoint My Sites are commonly referred to as “Facebook fo ...

  3. erlang dets

    1.dets表包含set.bag.和duplicate bag 2.dets:open_file(TableName,Options)创建或打开表 3.Options 1){auto_save,Int ...

  4. 【Machine Learning in Action --4】朴素贝叶斯分类

    1.概述 朴素贝叶斯分类是贝叶斯分类器的一种,贝叶斯分类算法是统计学的一种分类方法,利用概率统计知识进行分类,其分类原理就是利用贝叶斯公式根据某对象的先验 概率计算出其后验概率(即该对象属于某一类的概 ...

  5. 第一题 (Map)利用Map,完成下面的功能:

    从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队.如果该 年没有举办世界杯,则输出:没有举办世界杯.  附:世界杯冠军以及对应的夺冠年份,请参考本章附录. 附录  1.历届世界杯冠 ...

  6. mysql之TIMESTAMP(时间戳)用法详解 [http://www.jb51.net/article/51794.htm]

    一.TIMESTAMP的变体 TIMESTAMP时间戳在创建的时候可以有多重不同的特性,如: 1.在创建新记录和修改现有记录的时候都对这个数据列刷新: TIMESTAMP DEFAULT CURREN ...

  7. MapReduce初级案例

    1.数据去重  "数据去重"主要是为了掌握和利用并行化思想来对数据进行有意义的筛选.统计大数据集上的数据种类个数.从网站日志中计算访问地等这些看似庞杂的任务都会涉及数据去重.下面就 ...

  8. nmon命令用法

    用途 以交互方式显示本地系统统计信息并以记录方式记录系统统计信息. 语法 交互方式: nmon [ -h ] nmon [ -s < seconds > ] [ -c < count ...

  9. MFC中修改默认启动对话框方法

    // CMyAppEApp 初始化 BOOL CMyAppEApp::InitInstance(){// 如果一个运行在 Windows XP 上的应用程序清单指定要// 使用 ComCtl32.dl ...

  10. 第三十三节,sys解释器相关模块

    首先要引入import sys模块 sys.argv 功能:获取向脚本文件传入的参数,返回的列表,列表里的第一个元素是脚本文件路径和名称,后面的元素是传入的向脚本传入的参数 使用方法:sys.argv ...