ptr_fun

转自:http://www.cplusplus.com/reference/std/functional/ptr_fun/

template <class Arg, class Result>
  pointer_to_unary_function<Arg,Result> ptr_fun (Result (*f)(Arg));

template <class Arg1, class Arg2, class Result>
  pointer_to_binary_function<Arg1,Arg2,Result> ptr_fun (Result (*f)(Arg1,Arg2));
Convert function pointer to function object
Returns a function object that encapsulates function  f .

Function objects  are objects whose class defines member function  operator() . This member function allows the object to be used with the same syntax as a regular function call. Several standard  algorithms  and  adaptors  are designed to be used with function objects.

It is defined with the same behavior as:

template <class Arg, class Result>
  pointer_to_unary_function<Arg,Result> ptr_fun (Result (*f)(Arg))
{
  return pointer_to_unary_function<Arg,Result>(f);
}

template <class Arg1, class Arg2, class Result>
  pointer_to_binary_function<Arg1,Arg2,Result> ptr_fun (Result (*f)(Arg1,Arg2))
{
  return pointer_to_binary_function<Arg1,Arg2,Result>(f);
}



Template parameters

Arg, Arg1, Arg2
Types of the function's arguments.
Result
Function's return type.

Parameters

f
Pointer to a function, taking either one argument (of type  Arg) or two arguments (of types  Arg1 and  Arg2) and returning a value of type  Result.

Return value

A function object equivalent to  f .
pointer_to_unary_function  and  pointer_to_binary_function  are function object types, derived respectively from unary_function  and  binary_function .

Example


// ptr_fun example
#include <iostream>
#include <functional>
#include <algorithm>
#include <cstdlib>
#include <numeric>
using namespace std;

int main () {
  char* foo[] = {"10","20","30","40","50"};
  int bar[5];
  int sum;
  transform (foo, foo+5, bar, ptr_fun(atoi) );
  sum = accumulate (bar, bar+5, 0);
  cout << "sum = " << sum << endl;
  return 0;
}


Possible output:
sum = 150

ptr_fun使用
转自:http://topic.csdn.net/u/20100112/16/e1973c74-d81c-4e49-bef8-128ab836800b.html
// ceshi1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"


#include <iostream>
#include <functional>
using std::cout;
using std::endl;
/*
    binary_function
*/
template<typename Arg1,typename Arg2,typename Ret>
class binary_function{
public:
    typedef Arg1 first_argument_type;
    typedef Arg2 second_argument_type;
    typedef Ret return_type;
};
/*
    pointer_to_binary_function
*/
template<typename Arg1,typename Arg2,typename Ret>
class pointer_to_binary_function:public binary_function<Arg1,Arg2,Ret>{
private:
    Ret (*pmf)(Arg1,Arg2);
public:
    explicit pointer_to_binary_function(Ret (*pmf_)(Arg1,Arg2))
        :pmf(pmf_){
    }
    Ret operator()(Arg1 left,Arg2 right){
        return pmf(left,right);
    }
};
/*
    ptr_fun
*/
template<typename Arg1,typename Arg2,typename Ret>
pointer_to_binary_function<Arg1,Arg2,Ret> ptr_fun(Ret (*pf)(Arg1,Arg2)){
    return pointer_to_binary_function<Arg1,Arg2,Ret>(pf);
}
/*
    test_function
*/
void test_function(int a,int b){
    cout<<"Arg1 is "<<a<<endl;
    cout<<"Arg2 is "<<b<<endl;
}
/*
    main
*/
int main(int argc,char* argv[]){
    int a=3;
    int b=30;
    (pointer_to_binary_function<int,int,void>(test_function))(a,b);
    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值