Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2571679
  • 博文数量: 315
  • 博客积分: 3901
  • 博客等级: 少校
  • 技术积分: 3640
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-08 15:32
个人简介

知乎:https://www.zhihu.com/people/monkey.d.luffy Android高级开发交流群2: 752871516

文章分类

全部博文(315)

文章存档

2019年(2)

2018年(1)

2016年(7)

2015年(32)

2014年(39)

2013年(109)

2012年(81)

2011年(44)

分类: C/C++

2013-05-05 00:49:48

function template

std::ptr_fun

template 
  pointer_to_unary_function ptr_fun (Result (*f)(Arg));

template 
  pointer_to_binary_function 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  and  are designed to be used with function objects.

It is defined with the same behavior as:

1
2
3
4
5
6
7
8
9
10
11 
template 
  pointer_to_unary_function ptr_fun (Result (*f)(Arg))
{ return pointer_to_unary_function(f);
} template 
  pointer_to_binary_function ptr_fun (Result (*f)(Arg1,Arg2))
{ return pointer_to_binary_function(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.
 and  are function object types, derived respectively from and .

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 
// ptr_fun example  #include  #include  #include  #include  #include  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 

需要研究下这个: 
transform (foo, foo+5, bar, ptr_fun(atoi) );
 
 
transform 是遍历一个容器里面元素 然后执行一个操作
第1和2个参数是数据起始和结束位置(迭代器)
参数3是写入目标的起始位置
参数4是执行的操作(函数)
ptr_fun是将一个普通的函数适配成一个functor,添加上argument type和result type等类型

阅读(1303) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~