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));
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 fromunary_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;
- }
ptr_fun的更多相关文章
- 函数对象适配器之ptr_fun的使用示例
//============================================================================ // Name : CopyInts4.c ...
- 关于标准库中的ptr_fun/binary_function/bind1st/bind2nd
http://www.cnblogs.com/shootingstars/archive/2008/11/14/860042.html 以前使用bind1st以及bind2nd很少,后来发现这两个函数 ...
- ptr_fun学习笔记
ptr_fun是将一个普通的函数适配成一个functor,添加上argument type和result type等类型, 其实现如下(例子里面是binary_function,unary_funct ...
- 【转】三十分钟掌握STL
转自http://net.pku.edu.cn/~yhf/UsingSTL.htm 三十分钟掌握STL 这是本小人书.原名是<using stl>,不知道是谁写的.不过我倒觉得很有趣,所以 ...
- Bjarne Stroustrup对C++程序员的忠告
转自:http://blog.csdn.net/adm_qxx/archive/2007/05/20/1617488.aspx 第1章 致读者 [1] 在编写程序时,你是在为你针对某个问题的解决方 ...
- for_each使用方法详解[转]
for_each使用方法详解[转] Abstract之前在(原創) 如何使用for_each() algorithm? (C/C++) (STL)曾經討論過for_each(),不過當時功力尚淺,只談 ...
- c++模板库(简介)
目 录 STL 简介 ......................................................................................... ...
- 三十分钟掌握STL
这是本小人书.原名是<using stl>,不知道是谁写的.不过我倒觉得很有趣,所以化了两个晚上把它翻译出来.我没有对翻译出来的内容校验过.如果你没法在三十分钟内觉得有所收获,那么赶紧扔了 ...
- Effective STL
第9条:慎重选择删除元素的方法 删除特定值元素,vector.string.deque用erase-remove:c.erase(remove(c.begin(),c.end(),1963),c.en ...
随机推荐
- ng2 quickstart
1.下载 git clone https://github.com/angular/quickstart.git quickstart-angular 2.安装模块 npm install 3.启动 ...
- [Network Architecture]Xception 论文笔记(转)
文章来源 论文:Xception: Deep Learning with Depthwise Separable Convolutions 论文链接:https://arxiv.org/abs/161 ...
- 北京联通光猫 F427 路由改桥接的方法
最近安装了一个联通的宽带,赠送的光猫是 中兴 F427,然后联通小哥给安装的时候,直接开启了光猫的路由功能. 不过联通这个光猫实在是太弱了,起码默认的帐号开启的功能实在是太弱了,没法完成以下几个功能: ...
- Vim练级攻略(转)
转自平凡的世界:http://www.ccvita.com/ 前言今天看到这篇文章,共鸣点非常多.它把Vim使用分为4个级别,目前我自己是熟练运用前面三级的命令,在培养习惯使用第四级.完全就是我这一年 ...
- mysql_fetch_assoc查询多行数据
每次从查询结果中返回一行数据,作为关联数组,类似于一个游标,第一次是返回第一行,第二次迭代就是第二行,以此类推 如果返回多行,使用如下方法就可以了 while($row = $db->fetch ...
- 读懂 ECMAScript 规格
概述 规格文件是计算机语言的官方标准,详细描述语法规则和实现方法. 一般来说,没有必要阅读规格,除非你要写编译器.因为规格写得非常抽象和精炼,又缺乏实例,不容易理解,而且对于解决实际的应用问题,帮助不 ...
- Class 的继承
简介 Class 可以通过extends关键字实现继承,这比 ES5 的通过修改原型链实现继承,要清晰和方便很多. class Point { } class ColorPoint extends P ...
- Session存储
session其实分为服务器端Session和客户端Session. 当用户首次与Web服务器建立连接的时候,服务器会给用户分发一个sessionid作为标识.用户每次提交页面,浏览器都会把这个ses ...
- maven笔记(2)
项目管理利器(Maven)——maven的生命周期和插件Maven的生命周期大概如下:clean compile test package install这几个命令对应了一个项目的完整的构建过程,这几 ...
- centos下搭建DNS
一.DNS名词介绍: ( Domain Name System )是“域名系统”的英文缩写 正向解析:通过域名查找IP 反向解析:通过IP查找域名 二.安装BIND: BIND即Berkeley In ...