| <HTML> |
| <HEAD> |
| <TITLE>ptr_fun()</TITLE> |
| <LINK REL=StyleSheet HREF="../rw.css" TYPE="text/css" TITLE="Apache stdcxx Stylesheet"></HEAD> |
| <BODY BGCOLOR=#FFFFFF> |
| <A HREF="priority-queue.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="push-heap.html"><IMG SRC="images/bnext.gif" WIDTH=25 HEIGHT=21 ALT="Next file" BORDER=O></A><DIV CLASS="DOCUMENTNAME"><B>Apache C++ Standard Library Reference Guide</B></DIV> |
| <H2>ptr_fun()</H2> |
| <P><B>Library:</B> <A HREF="2-4.html">General utilities</A></P> |
| |
| <PRE><HR><B><I>Function</I></B><HR></PRE> |
| |
| <UL> |
| <LI><A HREF="#sec1">Local Index</A></LI> |
| <LI><A HREF="#sec2">Summary</A></LI> |
| <LI><A HREF="#sec3">Synopsis</A></LI> |
| <LI><A HREF="#sec4">Description</A></LI> |
| <LI><A HREF="#sec5">Example</A></LI> |
| <LI><A HREF="#sec6">See Also</A></LI> |
| <LI><A HREF="#sec7">Standards Conformance</A></LI> |
| </UL> |
| <A NAME="sec1"><H3>Local Index</H3></A> |
| No Entries |
| <A NAME="sec2"><H3>Summary</H3></A> |
| <P>A function adaptor that creates a function object from a pointer to a function </P> |
| <A NAME="sec3"><H3>Synopsis</H3></A> |
| |
| <PRE>#include <functional> |
| |
| namespace std { |
| 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)); |
| } |
| </PRE> |
| <A NAME="sec4"><H3>Description</H3></A> |
| <P>The <B><I><A HREF="pointer-to-unary-function.html">pointer_to_unary_function</A></I></B> and <B><I><A HREF="pointer-to-binary-function.html">pointer_to_binary_function</A></I></B> classes encapsulate pointers to functions, and use <SAMP>operator()</SAMP> so that the resulting object serves as a function object for the function.</P> |
| <P>The <SAMP>ptr_fun()</SAMP> function is overloaded to create instances of <B><I><A HREF="pointer-to-unary-function.html">pointer_to_unary_function</A></I></B> or <B><I><A HREF="pointer-to-binary-function.html">pointer_to_binary_function</A></I></B> when included with the appropriate pointer to a function. </P> |
| <A NAME="sec5"><H3>Example</H3></A> |
| |
| <UL><PRE>// |
| // pnt2fnct.cpp |
| // |
| |
| #include <algorithm> // for copy, transform |
| #include <functional> // for ptr_fun |
| #include <iostream> // cout, endl |
| #include <iterator> // for ostream_iterator |
| #include <deque> // for deque |
| #include <vector> // for vector |
| |
| |
| int factorial (int x) |
| { |
| return x > 1 ? x * factorial (x - 1) : x; |
| } |
| |
| |
| int main () |
| { |
| typedef std::deque<int, std::allocator<int> > Deque; |
| typedef std::vector<int, std::allocator<int> > Vector; |
| typedef std::ostream_iterator<int, char, |
| std::char_traits<char> > |
| Iter; |
| |
| // Initialize a deque with an array of integers. |
| const Deque::value_type a[] = { 1, 2, 3, 4, 5, 6, 7 }; |
| Deque d (a + 0, a + sizeof a / sizeof *a); |
| |
| // Create an empty vector to store the factorials. |
| Vector v (Vector::size_type (7)); |
| |
| // Compute factorials of the contents of 'd' and |
| // store results in 'v'. |
| std::transform (d.begin (), d.end (), v.begin (), |
| std::ptr_fun (factorial)); |
| |
| // Print the results. |
| std::cout << "The following numbers: \n "; |
| std::copy (d.begin (), d.end (), Iter (std::cout," ")); |
| |
| std::cout << "\n\nHave the factorials: \n "; |
| std::copy (v.begin (), v.end (), Iter (std::cout, " ")); |
| std::cout << std::endl; |
| |
| return 0; |
| } |
| |
| |
| Program Output: |
| </PRE></UL> |
| <UL><PRE>The following numbers: |
| 1 2 3 4 5 6 7 |
| |
| Have the factorials: |
| 1 2 6 24 120 720 5040 |
| </PRE></UL> |
| <A NAME="sec6"><H3>See Also</H3></A> |
| <P><A HREF="functionobjects.html">Function Objects</A>, <B><I><A HREF="pointer-to-binary-function.html">pointer_to_binary_function</A></I></B>, <B><I><A HREF="pointer-to-unary-function.html">pointer_to_unary_function</A></I></B>, <B><I><A HREF="mem-fun.html">mem_fun</A></I></B></P> |
| <A NAME="sec7"><H3>Standards Conformance</H3></A> |
| <P><I>ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 20.3.7</I></P> |
| |
| <BR> |
| <HR> |
| <A HREF="priority-queue.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="push-heap.html"><IMG SRC="images/bnext.gif" WIDTH=20 HEIGHT=21 ALT="Next file" BORDER=O></A> |
| |
| <!-- Google Analytics tracking code --> |
| <script src="http://www.google-analytics.com/urchin.js" type="text/javascript"> |
| </script> |
| <script type="text/javascript"> |
| _uacct = "UA-1775151-1"; |
| urchinTracker(); |
| </script> |
| <!-- end of Google Analytics tracking code --> |
| |
| </BODY> |
| </HTML> |