mem_fun_t/ptr_fun等用法

来源:互联网 发布:linux工程师经验之谈 编辑:程序博客网 时间:2024/06/16 12:38

原文地址:http://weimingtom.iteye.com/blog/965824

mem_fun_t

1-用于遍历调用多态的虚函数,容器元素是指针  

2-const_mem_fun:用于const的成员函数,用法类似

std::vector<B*> V;

V.push_back(new D1);

V.push_back(new D2);

V.push_back(new D2);

V.push_back(new D1); 

std::for_each(V.begin(), V.end(),std::mem_fun(&B::print));

mem_fun_ref

用于遍历调用多态的虚函数,容器元素是引用

const_mem_fun_ref:用于const的成员函数,用法类似 

std::vector<D1> V;

V.push_back(D1());

V.push_back(D1()); 

std::for_each(V.begin(), V.end(),std::mem_fun_ref(&B::print));

pointer_to_unary_function/pointer_to_binary_function

//pointer_to_unary_function<Arg, Result>,用于把一个参数的C函数转为函数对象,

//pointer_to_binary_function<Arg1, Arg2, Result>,同上,把两个参数的C函数转为函数对象

注意:

如果没有compose1,可以直接使用C函数如fabs,则不需要std::ptr_fun()的辅助 

//直接使用fabs时不需要使用ptr_fun

//std::transform(V1.begin(), V1.end(), V1.begin(), fabs); 

std::transform(V1.begin(), V1.end(), V1.begin(),

compose1(std::negate<double>(), std::ptr_fun(fabs))); 

mem_fun和mem_fun_ref的区别?

  mem_fun_ref的作用和用法跟mem_fun一样,

唯一的不同就是:当容器中存放的是对象实体的时候用mem_fun_ref,当容器中存放的是对象的指针的时候用mem_fun。

class ClxECS
{
public:
    int DoSomething() 
    { 
        // 这里以输出一句话来代替具体的操作
        cout << "Output frommethod DoSomething!" << endl; 
        return 0; 
    };
};

vector<ClxECS*> vECS;

 

     for_each(vECS.begin(), vECS.end(), mem_fun(&ClxECS::DoSomething));

           

例子:

list<Widget *> lpw;
for_each(lpw.begin(), lpw.end(),mem_fun(&Widget::test)); // pw->test();

vector<Widget> vw;
for_each(vw.begin(), vw.end(),mem_fun_ref(&Widget::test)); // w.test();

成员函数有参数的情况:将值传入,再bind1st为this

std::for_each(m_erased.begin(), m_erased.end(),

                     std::bind1st(std::mem_fun(&SocketSet::_replace_with_last), this));
//相当于this->_replace_with_last(iter) //iter

总结篇:

………………………………………………………………………………………………………………………………

STL中包含许多预定义的函数对象,其中有:

算术操作:plus, minus, multiplies, divides, modulus, 和negate,例:
assert(V2.size() >= V1.size() && V3.size() >= V1.size());
transform(V1.begin(), V1.end(), V2.begin(), V3.begin(),modulus<int>());

把V1[i]%V2[i]存入V3[i]。
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

比较操作:equal_to, not_equal_to, greater, less, greater_equal, 和 less_equal,例:
list<int>::iterator first_nonnegative = find_if(L.begin(), L.end(),

                                               bind2nd(greater_equal<int>(), 0));

在L中找到第一个大于等于0的元素。
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

逻辑操作:logical_and, logical_or, 和 logical_not,例:
char str[MAXLEN];
const char* wptr = find_if(str, str + MAXLEN,compose2(logical_or<bool>(),

                                           bind2nd(equal_to<char>(), ' '),

                                            bind2nd(equal_to<char>(),'\n')));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

修改、组装已有函数对象生成新的函数对象:unary_compose, binary_compose, unary_negate,

 binary_negate,binder1st, binder2nd, pointer_to_unary_function,

pointer_to_binary_function,mem_fun_t, mem_fun_ref_t, mem_fun1_t 和mem_fun1_ref_t。

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
说明:
函数对象模板类名                          辅助函数名                             说明                                          生成函数对象

unary_compose                                compose1              unary_compose(f,g)                          f(g(x))
binary_compose                              compose2            binary_compose(f,g1,g2)               f(g1(x), g2(x))
unary_negate                                      not1                           unary_negate(pred)                     一元谓词取非
binary_negate                                     not2                      binary_negate(pred)                          二元谓词取非
binder1st                                          bind1st                      binder1st(F,c)                     二元函数对象的第一个参数绑定为c
binder2st                                          bind2st                       binder2st(F,c)                        二元函数对象的第二个参数绑定为c
pointer_to_unary_function
pointer_to_binary_function           ptr_fun                  只能有1个或2个参数

mem_fun_t
mem_fun1_t                                  mem_fun            成员函数(只能有0个或1个参数,

 
区别:

   struct T{
     print(){}
};

list<T*>lstT;
for_each(lstT.begin(),lstT.end(),mem_fun(&T::print));

list<T> lstT;
for_each(lstT.begin(),lstT.end(),mem_fun_ref(&T::print));
0 0