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