📄 ptr_4059.htm
字号:
<HTML><TITLE>ptr_fun</TITLE><BODY>
<A HREF="ref.htm"><IMG SRC="images/banner.gif"></A>
<P><STRONG>Click on the banner to return to the Class Reference home page.</STRONG></P>
<P>©Copyright 1996 Rogue Wave Software</P>
<H2>ptr_fun</H2>
<HR><PRE> Function Adaptor</PRE><HR>
<A NAME="Summary"><H3>Summary</H3></A>
<P>A function that is overloaded to adapt a <I>pointer to a function </I>to work where a function is called for.</P>
<H3>Contents</H3>
<UL>
<A HREF="#Synopsis"><LI>Synopsis</LI></A>
<A HREF="#Description"><LI>Description</LI></A>
<A HREF="#Example"><LI>Example</LI></A>
<A HREF="#Warning"><LI>Warning</LI></A>
<A HREF="#See Also"><LI>See Also</LI></A>
</UL>
<A NAME="Synopsis"><H3>Synopsis</H3></A>
<PRE>#include <functional></PRE>
<PRE>
template<class Arg, class Result>
pointer_to_unary_function<Arg, Result>
<B>ptr_fun</B> (Result (*f)(Arg));
template<class Arg1, class Arg2, class Result>
pointer_to_binary_function<Arg1, Arg2, Result>
<B>ptr_fun</B> (Result (*x)(Arg1, Arg2));
</PRE>
<A NAME="Description"><H3>Description</H3></A>
<P>The <A HREF="poi_7375.htm"><B><I>pointer_to_unary_function</B></I></A> and <A HREF="poi_3537.htm"><B><I>pointer_to_binary_function</B></I></A> classes encapsulate pointers to functions and provide an <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 <A HREF="poi_7375.htm"><B><I>pointer_to_unary_function</B></I></A> or <A HREF="poi_3537.htm"><B><I>pointer_to_binary_function</B></I></A> when provided with the appropriate pointer to a function. </P>
<A NAME="Example"><H3>Example</H3></A>
<PRE>//
// pnt2fnct.cpp
//
#include <functional>
#include <deque>
#include <vector>
#include <algorithm>
#include <iostream.h>
//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(), <B>ptr_fun</B>(factorial));
//Print the results
cout << "The following numbers: " << endl << " ";
copy(d.begin(),d.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
cout << "Have the factorials: " << endl << " ";
copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
return 0;
}
Output :
The following numbers:
1 2 3 4 5 6 7
Have the factorials:
1 2 6 24 120 720 5040</PRE>
<A NAME="Warning"><H3>Warning</H3></A>
<P>If your compiler does not support default template parameters, you need to always supply the <SAMP>Allocator</SAMP> template argument. For instance, you will need to write :</P>
<P><SAMP>vector<int, allocator></SAMP></P>
<P>instead of :</P>
<P><SAMP>vector<int></SAMP></P>
<A NAME="See Also"><H3>See Also</H3></A>
<P><A HREF="Fun_7437.htm"><B><I>Function Objects</B></I></A>, <A HREF="poi_3537.htm"><B><I>pointer_to_binary_function</B></I></A>, <A HREF="poi_7375.htm"><B><I>pointer_to_unary_function</B></I></A></P>
<HR>
<A HREF="pri_2327.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="pus_5295.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -