Man Page ptr_fun.3



                       Standard C++ Library
             Copyright 1998, Rogue Wave Software, Inc.



NAME

     ptr_fun

      - A function that is overloaded to adapt  a  pointer  to  a
     function, to take the place of a function.





SYNOPSIS

     #include <functional>
     template<class Arg, class Result>
     pointer_to_unary_function<Arg, Result>
       ptr_fun (Result (*f)(Arg));

     template<class Arg1, class Arg2, class Result>
     pointer_to_binary_function<Arg1, Arg2, Result>
       ptr_fun (Result (*x)(Arg1, Arg2));





DESCRIPTION

     The pointer_to_unary_function and pointer_to_binary_function
     classes encapsulate pointers to functions and use operator()
     so that the resulting object serves as a function object for
     the function.

     The ptr_fun 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.





EXAMPLE

     //
     // pnt2fnct.cpp
     //
      #include <functional>
      #include <deque>
      #include <vector>
      #include <algorithm>
      #include <iostream>
     using namespace std;

      //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,char>(cout," "));

       cout << endl << endl;
       cout << "Have the factorials: " << endl << "     ";
       copy(v.begin(),v.end(),
            ostream_iterator<int,char>(cout," "));

       return 0;
      }

     Program Output




     The following numbers:
         1 2 3 4 5 6 7
     Have the factorials:
         1 2 6 24 120 720 5040





WARNINGS

     If your compiler does not support default  template  parame-
     ters, you always need to supply the Allocator template argu-
     ment. For instance, you need to write:

     vector<int, allocator<int> >

     instead of:

     vector<int>

     If your compiler does not support namespaces,  then  you  do
     not need the using declaration for std.





SEE ALSO

     Function_Objects,                pointer_to_binary_function,
     pointer_to_unary_function