.\" ident @(#)ptr_fun.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH ptr_fun 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2ptr_fun\fP \ - A function that is overloaded to adapt a pointer to a function, to take the place of a function. .SH SYNOPSIS .br #include .br template .br pointer_to_unary_function .RE .RS 2 ptr_fun (Result (*f)(Arg)); .RE .RS 0 .br template .br pointer_to_binary_function .RE .RS 2 ptr_fun (Result (*x)(Arg1, Arg2)); .SH DESCRIPTION The pointer_to_unary_function and pointer_to_binary_function classes encapsulate pointers to functions and use \f2operator()\fP so that the resulting object serves as a function object for the function. The \f2ptr_fun\fP function is overloaded to create instances of pointer_to_unary_function or pointer_to_binary_function when included with the appropriate pointer to a function. .SH EXAMPLE .RE .RS 0 // .br // pnt2fnct.cpp .br // .RE .RS 1 #include .br #include .br #include .br #include .br #include .RE .RS 0 using namespace std; .br .RE .RS 1 //Create a function .RE .RS 0 int factorial(int x) .RE .RS 1 { .RE .RS 2 int result = 1; .br for(int i = 2; i <= x; i++) .RE .RS 6 result *= i; .RE .RS 2 return result; .RE .RS 1 } .RE .RS 0 .br int main() .RE .RS 1 { .RE .RS 3 //Initialize a deque with an array of ints .RE .RS 2 int init[7] = {1,2,3,4,5,6,7}; .br deque d(init, init+7); .RE .RS 0 .RE .RS 3 //Create an empty vector to store the factorials .RE .RS 2 vector v((size_t)7); .RE .RS 0 .RE .RS 3 //Transform the numbers in the deque to their .br //factorials and store in the vector .RE .RS 2 transform(d.begin(), d.end(), v.begin(), .RE .RS 13 ptr_fun(factorial)); .RE .RS 0 .RE .RS 3 //Print the results .RE .RS 2 cout << "The following numbers: " << endl << " "; .br copy(d.begin(),d.end(), .RE .RS 7 ostream_iterator(cout," ")); .RE .RS 0 .RE .RS 2 cout << endl << endl; .br cout << "Have the factorials: " << endl << " "; .br copy(v.begin(),v.end(), .RE .RS 7 ostream_iterator(cout," ")); .RE .RS 0 .RE .RS 2 return 0; .RE .RS 1 } .br .RE .RS 0 Program Output .RE .RS 0 .br The following numbers: .RE .RS 4 1 2 3 4 5 6 7 .RE .RS 0 Have the factorials: .RE .RS 4 1 2 6 24 120 720 5040 .SH WARNINGS If your compiler does not support default template parameters, you always need to supply the \f2Allocator\fP template argument. For instance, you need to write: \f2vector >\fP instead of: \f2vector\fP If your compiler does not support namespaces, then you do not need the using declaration for \f2std\fP. .SH SEE ALSO Function_Objects, pointer_to_binary_function, pointer_to_unary_function