分类: C/C++2012-05-05 20:21 593人阅读 评论(0) 收藏 举报
 
 

转自: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:

  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. template <class Arg1, class Arg2, class Result>
  7. pointer_to_binary_function<Arg1,Arg2,Result> ptr_fun (Result (*f)(Arg1,Arg2))
  8. {
  9. return pointer_to_binary_function<Arg1,Arg2,Result>(f);
  10. }

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

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

Possible output:

  1. sum = 150

ptr_fun使用
转自:http://topic.csdn.net/u/20100112/16/e1973c74-d81c-4e49-bef8-128ab836800b.html

  1. // ceshi1.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <functional>
  6. using std::cout;
  7. using std::endl;
  8. /*
  9. binary_function
  10. */
  11. template<typename Arg1,typename Arg2,typename Ret>
  12. class binary_function{
  13. public:
  14. typedef Arg1 first_argument_type;
  15. typedef Arg2 second_argument_type;
  16. typedef Ret return_type;
  17. };
  18. /*
  19. pointer_to_binary_function
  20. */
  21. template<typename Arg1,typename Arg2,typename Ret>
  22. class pointer_to_binary_function:public binary_function<Arg1,Arg2,Ret>{
  23. private:
  24. Ret (*pmf)(Arg1,Arg2);
  25. public:
  26. explicit pointer_to_binary_function(Ret (*pmf_)(Arg1,Arg2))
  27. :pmf(pmf_){
  28. }
  29. Ret operator()(Arg1 left,Arg2 right){
  30. return pmf(left,right);
  31. }
  32. };
  33. /*
  34. ptr_fun
  35. */
  36. template<typename Arg1,typename Arg2,typename Ret>
  37. pointer_to_binary_function<Arg1,Arg2,Ret> ptr_fun(Ret (*pf)(Arg1,Arg2)){
  38. return pointer_to_binary_function<Arg1,Arg2,Ret>(pf);
  39. }
  40. /*
  41. test_function
  42. */
  43. void test_function(int a,int b){
  44. cout<<"Arg1 is "<<a<<endl;
  45. cout<<"Arg2 is "<<b<<endl;
  46. }
  47. /*
  48. main
  49. */
  50. int main(int argc,char* argv[]){
  51. int a=3;
  52. int b=30;
  53. (pointer_to_binary_function<int,int,void>(test_function))(a,b);
  54. return 0;
  55. }

ptr_fun的更多相关文章

  1. 函数对象适配器之ptr_fun的使用示例

    //============================================================================ // Name : CopyInts4.c ...

  2. 关于标准库中的ptr_fun/binary_function/bind1st/bind2nd

    http://www.cnblogs.com/shootingstars/archive/2008/11/14/860042.html 以前使用bind1st以及bind2nd很少,后来发现这两个函数 ...

  3. ptr_fun学习笔记

    ptr_fun是将一个普通的函数适配成一个functor,添加上argument type和result type等类型, 其实现如下(例子里面是binary_function,unary_funct ...

  4. 【转】三十分钟掌握STL

    转自http://net.pku.edu.cn/~yhf/UsingSTL.htm 三十分钟掌握STL 这是本小人书.原名是<using stl>,不知道是谁写的.不过我倒觉得很有趣,所以 ...

  5. Bjarne Stroustrup对C++程序员的忠告

    转自:http://blog.csdn.net/adm_qxx/archive/2007/05/20/1617488.aspx  第1章 致读者  [1] 在编写程序时,你是在为你针对某个问题的解决方 ...

  6. for_each使用方法详解[转]

    for_each使用方法详解[转] Abstract之前在(原創) 如何使用for_each() algorithm? (C/C++) (STL)曾經討論過for_each(),不過當時功力尚淺,只談 ...

  7. c++模板库(简介)

    目 录 STL 简介 ......................................................................................... ...

  8. 三十分钟掌握STL

    这是本小人书.原名是<using stl>,不知道是谁写的.不过我倒觉得很有趣,所以化了两个晚上把它翻译出来.我没有对翻译出来的内容校验过.如果你没法在三十分钟内觉得有所收获,那么赶紧扔了 ...

  9. Effective STL

    第9条:慎重选择删除元素的方法 删除特定值元素,vector.string.deque用erase-remove:c.erase(remove(c.begin(),c.end(),1963),c.en ...

随机推荐

  1. Faster-rcnn 配置方法

    Faster-rcnn 在Linux下的配置方法 感谢@邓学长 建立过程: (下载库的时候要按照库readme 进行操作) opencv 的包下载安装,安装教程 用git命令将这个库下载到本地 fas ...

  2. from: can't read /var/mail/xxx 解决方法

    在执行一个发包脚本的时候,遇到了如下问题: from: can't read /var/mail/scapy.all 原因:脚本是没有问题的,但它并不是可以被python执行的可执行文件. 解决方法: ...

  3. oracle 12c 报错 ora-03137 来自客户机的格式错误的TTC包被拒绝

    昨天下午,实施报了一个oracle的报错ora-03137 说是数据库在11g时没有问题,升级到12c 时,就报错了. 本地调试,看到执行完sql后,报异常,如下: 把SqL在12c的数据库执行一下, ...

  4. CALL_AND_RETRY_LAST Allocation failed node打包报错

    全局安装increase-memory-limit: npm install -g increase-memory-limit 进入工程目录,执行: increase-memory-limit

  5. nginx 开启rewrite thinkcmf

    server{ ... 省略 location / { index index.php index.html index.htm; #如果请求既不是一个文件,也不是一个目录,则执行一下重写规则 if ...

  6. 实用SQL语句

    sp_depends t_im_flow 获取到与这个表有关系的存储过程.触发器.函数.视图等.

  7. .Net Core 修改默认的启动端口

    今天无意中发现一个变化,因为很久没看.net core的项目了,发现项目启动的默认端口已经不是5000了,记得很清楚,最早那还是.net core 1.x版本的时候,每次启动都会默认是5000端口号, ...

  8. Java获取未知类型对象的属性

    获取未知类型对象的属性通常有两种方式: 一是通过自定义注解的方式,通过获取被注解的属性从而获取属性的值,这种方式也是Spring参数注入的重要实现手段 二是通过反射获取属性的名称,通过属性名从而获取属 ...

  9. [MyEclipse]转:设置注释格式

    Window --> Java --> Code Style --> Code Templates --> Comments --> types --> Edit ...

  10. 24.Java中atomic包中的原子操作类总结

    1. 原子操作类介绍 在并发编程中很容易出现并发安全的问题,有一个很简单的例子就是多线程更新变量i=1,比如多个线程执行i++操作,就有可能获取不到正确的值,而这个问题,最常用的方法是通过Synchr ...