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 ...
随机推荐
- java 普通内部类和静态内部类
区别1: 普通内部类实例化后的对象持有外部类的引用,在非静态类内部可以访问外部类的成员:静态内部类实例化不持有外部对象引用,不能访问外面的方法和成员: 从耦合度上来讲,普通内部类跟外部类耦合程度很高, ...
- LeetCode——Integer Break
Question Given a positive integer n, break it into the sum of at least two positive integers and max ...
- 51Nod 1433 0和5(9的倍数理论)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1433 思路: 数论中关于9的倍数的理论:若是一个数能被9整除,则各位数之 ...
- ORM框架greenDao 2 (用于了解旧版本的使用方法,目前最新版本为3.2.2,使用注释的方式来生成)
摘要: Android中对SQLite数据库使用,是一件非常频繁的事情.现今,也有非常多的SQLite处理的开源框架,其中最著名的greenDao,它以占用资源少,处理效率高等特点,成为优秀的ORM框 ...
- GTID主从 与 传统主从复制
一.主从复制 1.)普通主从复制: 普通主从复制主要是基于二进制日志文件位置的复制,因此主必须启动二进制日志记录并建立唯一的服务器ID,复制组中的每个服务器都必须配置唯一的服务器ID.如果您省略ser ...
- 代码演示神器——jsfiddle
目录: 1. 介绍 2. jsfiddle的具体使用 3. 总结 1. 介绍 很多时候,我们需要在我们写的文章或博客中,即时显示出我们写的demo,能方便的解释出我们的思路.很久之前我也写过一篇文章, ...
- hadoop2.6.0的eclipse插件安装
1.安装插件 下载插件hadoop-eclipse-plugin-2.6.0.jar并将其放到eclips安装目录->plugins(插件)文件夹下.然后启动eclipse. 配置 hadoop ...
- @DataProvider的应用
代码中经常有一些数据需要维护,但是每次都写在一个class或者methods中,维护起来是个麻烦事: 这里引入@DataProvider的思想,建一个DataProvider的方法,让我们需要维护的数 ...
- Android数据库框架-----GreenDao3的相关使用和版本升级更新
GreenDAO是一款非要流行的Android平台上的数据库框架,性能优秀,代码简洁:是一个将对象映射到SQLite数据库中的轻量且快速的ORM解决方案. GreenDAO 优势 1.一个精简的库 2 ...
- 1-12 RHEL7-find命令的使用
1.文件查找findfind命令是在目录结构中,搜索文件,并执行特定的操作find命令提供了相当多的查找条件,功能很强大 2.格式usage:find pathname -options[-print ...