⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fun_8456.htm

📁 C++标准库 C++标准库 C++标准库 C++标准库
💻 HTM
字号:
<HTML><HEAD><TITLE>3.4 Function Adaptors</TITLE></HEAD><BODY><A HREF="ug1.htm"><IMG SRC="images/banner.gif"></A><BR><A HREF="fun_0476.htm"><IMG SRC="images/prev.gif"></A><A HREF="booktoc1.htm"><IMG SRC="images/toc.gif"></A><A HREF="tindex1.htm"><IMG SRC="images/tindex.gif"></A><A HREF="neg_5796.htm"><IMG SRC="images/next.gif"></A><BR><STRONG>Click on the banner to return to the user guide home page.</STRONG><H2>3.4 Function Adaptors</H2><A NAME="idx32"><!></A><P>A <I>function adaptor</I> is an instance of a class that adapts a global or member function so that the function can be used as a function object (a function adaptor may also be used to alter the behavior of a function or function object, as is the case in the next section).  Each function adaptor provides a constructor that takes a global or member function.  The adaptor also provides a parenthesis operator that forwards its call to that associated global or member function.  </P><P>The pointer_to_unary_functoin and pointer_to_binary_function templates adapt global functions of one or two arguments.  These adaptors can be applied directly, or you can use the ptr_fun function template to construct the appropriate adaptor automatically.  For instance, I can adapt a simple times3 function and apply it to a vector of integers as follows:</P><PRE>int times3(int x) {  return 3*x;}int a{} {1,2,3,4,5};vector&#60;int> v(a,a+5), v2;transform(v.begin(),v.end(),v2.end(),ptr_fun(times3));</PRE><P>Alternatively, I could have applied the adaptor, and then passed the new, adapted, function object to my vector.</P><PRE>pointer_to_unary_function&#60;int,int> pf(times3);transform(v.begin(),v.end(),v2.end(),pf);</PRE><P>Here you can see the advantage of allowing the compiler to deduce the types needed by pointer_to_unary_function through the use of ptr_fun. </P><P>The <A HREF="../stdlibcr/mem_1776.htm"><B><I>mem_fun</I></B></A> family of templates adapts member functions, rather than global functions.  For instance, if I have a set of lists, and I want to sort each list in the set, I can use <SAMP>mem_fun_t</SAMP> (or more simply <SAMP>mem_fun</SAMP>) to apply the list sort member function to each element in the set.</P><PRE>set&#60;list&#60;int>* > s;// Initialize the set with lists...// Sort each list in the set.for_each(s.begin(),s.end(),mem_fun(&#38;list&#60;int>::sort));// Now each list in the set is sorted</PRE><P>This is necessary because the generic sort algorithm cannot be used on a list.  This is also the simplest way to access any polymorphic characteristics of an object held in a standard container.  For instance I might invoke a virtual draw function on a collection of objects that are all part of the canonical 'shape' hierarchy like this:</P><PRE>// shape hierarchyclass shape {   virtual void draw();};class circle : public shape {   void draw();};class square : public shape {  void draw();};//  Assemble a vector of shapescircle c;square s;vector&#60;shape*> v;v.push_back(&#38;s);v.push_back(&#38;c);// Call draw on each onefor_each(v.begin(),v.end(), mem_fun(&#38;shape::draw));</PRE><P>Similarly to the global function adaptors, each member function adaptor consists of a class template and an associated function template.  The class is the actual adaptor, while the function simplifies the use of the class by constructing instances of that class on the fly.  For instance, in the above example I could have constructed a <SAMP>mem_fun_t</SAMP> myself, and then passed that to the <SAMP>for_each</SAMP> algorithm:</P><PRE>mem_fun_t&#60;shape> mf(&#38;shape::draw);for_each(v.begin(),v.end(),mf);</PRE><P>Again, you can see that the <A HREF="../stdlibcr/mem_1776.htm"><B><I>mem_fun</I></B></A> function template simplifies the use of the <SAMP>mem_fun_t</SAMP> adaptor by allowing the compiler to deduce the type needed by <SAMP>mem_fun_t</SAMP>.</P><P>The library provides member function adaptors for functions with zero arguments (as above) and one argument.  This can be easily extended to member functions with more arguments.  </P><PRE></PRE><HR><A HREF="fun_0476.htm"><IMG SRC="images/prev.gif"></A> <A HREF="booktoc1.htm"><IMG SRC="images/toc.gif"></A><A HREF="tindex1.htm"><IMG SRC="images/tindex.gif"></A><A HREF="neg_5796.htm"><IMG SRC="images/next.gif"></A><P>&copy;Copyright 1996, Rogue Wave Software, Inc.</P></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -