Standard C++ Library Class Reference
the function.
The ptr_fun function is overloaded to create instances of pointer_to_unary_function or
pointer_to_binary_function when provided with the appropriate pointer to a function.
Example
//
// pnt2fnct.cpp
//
#include <functional>
#include <deque>
#include <vector>
#include <algorithm>
#include <iostream.h>
//Create a function
int factorial(int x)
{
int result = 1;
for(int i = 2; i <= x; i++)
result *= i;
return result;
}
int main()
{
//Initialize a deque with an array of ints
int init[7] = {1,2,3,4,5,6,7};
deque<int> d(init, init+7);
//Create an empty vector to store the factorials
vector<int> v((size_t)7);
//Transform the numbers in the deque to their factorials and
//store in the vector
transform(d.begin(), d.end(), v.begin(), ptr_fun(factorial));
//Print the results
cout << "The following numbers: " << endl << " ";
copy(d.begin(),d.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
cout << "Have the factorials: " << endl << " ";
copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
return 0;
}
Output :
The following numbers:
1 2 3 4 5 6 7
Have the factorials: