zoukankan      html  css  js  c++  java
  • ptr_fun

     

    ptr_fun

    分类: C/C++
     
     

    转自: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:
    [cpp] view plaincopy
     
    1. template <class Arg, class Result>  
    2.   pointer_to_unary_function<Arg,Result> ptr_fun (Result (*f)(Arg))  
    3. {  
    4.   return pointer_to_unary_function<Arg,Result>(f);  
    5. }  
    6.   
    7. template <class Arg1, class Arg2, class Result>  
    8.   pointer_to_binary_function<Arg1,Arg2,Result> ptr_fun (Result (*f)(Arg1,Arg2))  
    9. {  
    10.   return pointer_to_binary_function<Arg1,Arg2,Result>(f);  
    11. }  


    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 fromunary_function and binary_function.

    Example

     
    [cpp] view plaincopy
     
    1. // ptr_fun example  
    2. #include <iostream>  
    3. #include <functional>  
    4. #include <algorithm>  
    5. #include <cstdlib>  
    6. #include <numeric>  
    7. using namespace std;  
    8.   
    9. int main () {  
    10.   char* foo[] = {"10","20","30","40","50"};  
    11.   int bar[5];  
    12.   int sum;  
    13.   transform (foo, foo+5, bar, ptr_fun(atoi) );  
    14.   sum = accumulate (bar, bar+5, 0);  
    15.   cout << "sum = " << sum << endl;  
    16.   return 0;  
    17. }  


    Possible output:
    [cpp] view plaincopy
     
    1. sum = 150  

    ptr_fun使用
    转自:http://topic.csdn.net/u/20100112/16/e1973c74-d81c-4e49-bef8-128ab836800b.html
    [cpp] view plaincopy
     
    1. // ceshi1.cpp : 定义控制台应用程序的入口点。  
    2. //  
    3.   
    4. #include "stdafx.h"  
    5.   
    6.   
    7. #include <iostream>  
    8. #include <functional>  
    9. using std::cout;  
    10. using std::endl;  
    11. /* 
    12.     binary_function 
    13. */  
    14. template<typename Arg1,typename Arg2,typename Ret>  
    15. class binary_function{  
    16. public:  
    17.     typedef Arg1 first_argument_type;  
    18.     typedef Arg2 second_argument_type;  
    19.     typedef Ret return_type;  
    20. };  
    21. /* 
    22.     pointer_to_binary_function 
    23. */  
    24. template<typename Arg1,typename Arg2,typename Ret>  
    25. class pointer_to_binary_function:public binary_function<Arg1,Arg2,Ret>{  
    26. private:  
    27.     Ret (*pmf)(Arg1,Arg2);  
    28. public:  
    29.     explicit pointer_to_binary_function(Ret (*pmf_)(Arg1,Arg2))  
    30.         :pmf(pmf_){  
    31.     }  
    32.     Ret operator()(Arg1 left,Arg2 right){  
    33.         return pmf(left,right);  
    34.     }  
    35. };  
    36. /* 
    37.     ptr_fun 
    38. */  
    39. template<typename Arg1,typename Arg2,typename Ret>  
    40. pointer_to_binary_function<Arg1,Arg2,Ret> ptr_fun(Ret (*pf)(Arg1,Arg2)){  
    41.     return pointer_to_binary_function<Arg1,Arg2,Ret>(pf);  
    42. }  
    43. /* 
    44.     test_function 
    45. */  
    46. void test_function(int a,int b){  
    47.     cout<<"Arg1 is "<<a<<endl;  
    48.     cout<<"Arg2 is "<<b<<endl;  
    49. }  
    50. /* 
    51.     main 
    52. */  
    53. int main(int argc,char* argv[]){  
    54.     int a=3;  
    55.     int b=30;  
    56.     (pointer_to_binary_function<int,int,void>(test_function))(a,b);  
    57.     return 0;  
    58. }  
  • 相关阅读:
    Mono 4.0 Mac上运行asp.net mvc 5.2.3
    ASP.NET SignalR 高可用设计
    .NET Fringe 定义未来
    微软“.Net社区虚拟大会”dotnetConf2015 第二天 无处不在的Xamarin
    微软“.Net社区虚拟大会”dotnetConf2015:关键词:.NET 创新、开源、跨平台
    Mono产品生命周期
    Paket 介绍
    谷歌发布的首款基于HTTP/2和protobuf的RPC框架:GRPC
    Visual Studio 2015 CTP6 发布
    皮裤原理和运营微信公众号dotNET跨平台
  • 原文地址:https://www.cnblogs.com/OooO-CN/p/3994850.html
Copyright © 2011-2022 走看看