cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : Miscellaneous : functional : pointer_to_unary_function
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Miscellaneous
functional
iterator
memory
utility
functional
binary_function
unary_function
operator classes:
· divides
· equal_to
· greater
· greater_equal
· less
· less_equal
· logical_and
· logical_not
· logical_or
· minus
· modulus
· multiplies
· negate
· not_equal_to
· plus
adaptor functions:
· bind1st
· bind2nd
· mem_fun
· mem_fun_ref
· not1
· not2
· ptr_fun
types:
· binary_negate
· binder1st
· binder2nd
· const_mem_fun1_ref_t
· const_mem_fun1_t
· const_mem_fun_ref_t
· const_mem_fun_t
· mem_fun1_ref_t
· mem_fun1_t
· mem_fun_ref_t
· mem_fun_t
· pointer_to_binary_funct...
· pointer_to_unary_function
· unary_negate

-

pointer_to_unary_function class template
template <class Arg, class Result> class pointer_to_unary_function;
<functional>

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 Arg and 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:

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); }
};

Members

constructor
Constructs an unary function object class from pointer to a function that takes one argument of type Arg and returns a value of type Result.
operator()
Member function taking a single parameter and returning the result of calling the function pointer used at construction.

Example

// 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.07517

See also

ptr_fun Convert function pointer to function object (function template)
pointer_to_binary_function Generate binary function object class from pointer (class template)
unary_function Unary function object base class (class template)

Home page | Privacy policy
© cplusplus.com, 2000-2008 - All rights reserved - v2.2
Spotted an error? contact us