다음을 통해 공유


ptr_fun

Helper template functions used to convert unary and binary function pointers, respectively, into unary and binary adaptable functions.

template<class Arg, class Result>
   pointer_to_unary_function<Arg, Result, Result (*)(Arg)>
      ptr_fun(Result (*_pfunc)(Arg));
template<class Arg1, class Arg2, class Result>
   pointer_to_binary_function<Arg1, Arg2, Result, Result (*)(Arg1, Arg2)>
      ptr_fun(Result (*_pfunc)(Arg1, Arg2));

Parameters

  • _pfunc
    The unary or binary function pointer to be converted to an adaptable function.

Return Value

The first template function returns the unary function pointer_to_unary_function <Arg, Result>(*_pfunc).

The second template function returns binary function pointer_to_binary_function <Arg1, Arg2, Result>(*_pfunc).

Remarks

A function pointer is a function object and may be passed to any Standard Template Library algorithm that is expecting a function as a parameter, but it is not adaptable. To use it with an adaptor, such as binding a value to it or using it with a negator, it must be supplied with the nested types that make such an adaptation possible. The conversion of unary and binary function pointers by the ptr_fun helper function allows the function adaptors to work with unary and binary function pointers.

Example

// functional_ptr_fun.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional>
#include <cstring>
#include <iostream>

int main( )
{
   using namespace std;
   vector <char*> v1;
   vector <char*>::iterator Iter1, RIter;

   v1.push_back ( "Open" );
   v1.push_back ( "up" );
   v1.push_back ( "the" );
   v1.push_back ( "pearly" );
   v1.push_back ( "gates" );

   cout << "Original sequence contains: " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << endl;

   // To search the sequence for "pearly"
   // use a pointer_to_function conversion
   RIter = find_if( v1.begin( ), v1.end( ),
      not1 ( bind2nd (ptr_fun ( strcmp ), "pearly" ) ) );

   if ( RIter != v1.end( ) )  
   {
      cout << "The search for 'pearly' was successful.\n";
      cout << "The next character string is: " 
           << *++RIter << "." << endl;
   }
}

Output

Original sequence contains: Open up the pearly gates 
The search for 'pearly' was successful.
The next character string is: gates.

Requirements

Header: <functional>

Namespace: std

See Also

Concepts

<functional> Members

Standard Template Library