class template
<functional>
std::pointer_to_unary_functiontemplate <class Arg, class Result> class pointer_to_unary_function;
Generate unary function object class from pointer
Generates an unary function object class from a pointer to a function that takes a single argument of type
Argand returns a value of type
Result.
pointer_to_unary_function is generally used as a type. The function ptr_fun (also defined in header <functional>) can be used to directly construct an object of this type.
This class is derived from unary_function and is typically defined as:
1
2
3
4
5
6
7
8
9
10
template <class Arg, class Result>
class pointer_to_unary_function : public unary_function <Arg,Result>
{
protected:
Result(*pfunc)(Arg);
public:
explicit pointer_to_unary_function ( Result (*f)(Arg) ) : pfunc (f) {}
Result operator() (Arg x) const
{ return pfunc(x); }
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// pointer_to_unary_function example
#include <iostream>
#include <functional>
#include <algorithm>
#include <cmath>
using namespace std;
int main () {
pointer_to_unary_function <double,double> LogObject (log);
double numbers[] = {10.0, 20.0, 40.0, 80.0, 160.0};
double logs[5];
transform (numbers, numbers+5, logs, LogObject);
for (int i=0; i<5; i++)
cout << logs[i] << " ";
cout << endl;
return 0;
}
Possible output:
2.30259 2.99573 3.68888 4.38203 5.07517See also
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4