#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扩展(一)的更多相关文章

  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. CF #368 div2

    题目链接:http://codeforces.com/contest/707/problem/A A. Brain's Photos time limit per test 2 seconds mem ...

  2. Java 编码 字符集

    Java 编码 字符集 @author ixenos 1.   字符集 a)    字符集建立了两字节Unicode码元序列与使用本地字符编码方式的字节序列之间的映射. b)    为了兼容其它命名, ...

  3. C语言实现用户输入

    用户输入一个字符串然后回车表示结束.因为用户在输入的过程中长度是不确定的,所以要求自己使用的循环写的更好.在这里自己写了一个代码,效率不高,相对来说如果能模拟出C++中的vector向量可能会好一些. ...

  4. Visual Studio 2010 安装帮助文档问题

    今天重装系统,装完VS2010后,如往常一样安装文档,却弹出如下错误"Could not create the local store in the specified folder.... ...

  5. CEdit实现文本换行

    CEdit控件若要在字符串中插入换行字符("\r\n")实现换行效果,必须指定两个风格 ES_MULTILINE和ES_WANTRETURN. 1: DWORD dwStyle = ...

  6. redis高级实用特性(2)

    事务处理: redis对事务的支持目前还是比较简单,redis只能保证一个 client发起事务中的命令可以连续执行,而中间不会插入其他 client的命令,当一个client 在一个连接中发出mul ...

  7. 【python问题系列--4】ValueError: operands could not be broadcast together with shapes (100,3) (3,1)

    背景:dataMatrix是(100,3)的列表,labelMat是(1,100)的列表,weights是(3,1)的数组,属性如下代码所示: >>> import types> ...

  8. 29.编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数 loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个 类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功 能。

    //Vehicle类 package d922A; public class Vehicle { private int wheels; private double weight; Vehicle( ...

  9. sql标识符和格式

    数据库名是一个标识符,表名也是一个标识符,在SQL SERVER中标识符分为两类: (1).常规标识符;(2).分隔标识符两者重要的区别:常规标识符必须严格遵守命名的规定,而分隔标识符则可以不遵守命名 ...

  10. placeholder改变颜色

    ::-webkit-input-placeholder { /* WebKit browsers */ color: #cfcfcf; } :-moz-placeholder { /* Mozilla ...