Name
ptr_fun function template — Creates a pointer to a function object
Synopsis
template <typename Arg1, typename Arg2, typename Rtn> pointer_to_binary_function<Arg1,Arg2,Rtn> ptr_fun(Rtn (*f)(Arg1, Arg2)); template <typename Arg, typename Rtn> pointer_to_unary_function<Arg, Rtn> ptr_fun(Rtn (*f)(Arg));
The ptr_fun
function
template creates a function object from a pointer to a function. The
resulting function object has an operator(
)
that calls the function. Functions of one and two
arguments are supported.
For example, suppose you have two numeric vectors, a
and b
, and you want to raise each element of
a
to the power of the
corresponding element in b
,
saving the result in a third vector, c
. There is no predefined power function
object, so you can use ptr_fun
and your own power
function
instead, as shown in Example
13-13.
Example
std::vector<double> a, b, c;
double power(double x, double y)
{
return std::pow(x, y);
}
...
std::transform(a.begin(), a.end( ), b.begin( ), c.begin( ),std::ptr_fun(power));
Get C++ In a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.