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

📄 functors.html

📁 指导程序员合理、高效的进行标准模板库编程。
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="Author" CONTENT="Zafir Anjum">
   <TITLE>MFC Programmer's SourceBook : STL Programmer's Guide</TITLE>
    <META name="description" 
     content="A freely available implementation 
     of the C++ Standard Template Library, including 
     hypertext documentation.">
	<META name="keywords" 
	content="generic programming, STL, standard template library">
</HEAD>

<SCRIPT LANGUAGE="JavaScript"><!--
var adcategory = "cpp";
// -->
</SCRIPT>
<body background="../../fancyhome/back.gif" bgcolor="#FFFFFF" >
<SCRIPT LANGUAGE="JavaScript"><!--
var nfrm = location.href.indexOf("_nfrm_");
var validframes = (top.frames.length > 0 && top.frames['ad'] && top.frames['logo'] );
var random = Math.random();

if( !validframes && nfrm == -1 )
{
	var dclkPage = "www.codeguru.com/";
	if( self.adcategory )
		dclkPage += adcategory;
	else
		dclkPage += "mfc";
	document.write('<nolayer><center>');
	document.write('<iframe src="http://ad.doubleclick.net/adi/' + dclkPage + ';ord='
	 + random + '" width=470 height=62 marginwidth=0 marginheight=0 hspace=0 vspace=0 '
	 + 'frameborder=0 scrolling=no bordercolor="#000000">');
	document.write('<a href="http://ad.doubleclick.net/jump/' + dclkPage + ';ord='
	 + random + '">');
	document.write('<img src="http://ad.doubleclick.net/ad/' + dclkPage + ';ord='
	 + random + '" height=60 width=468>' + '</a>');
	document.write('</iframe>');
	document.write('</center></nolayer>');
	document.write('<layer  src="http://ad.doubleclick.net/adl/' + dclkPage + 
	 ';ord=' + random + '"></layer>');
	document.write('<ilayer visibility=hide width=468 height=83></ilayer>');
}


//		top.location = "/show.cgi?" + adcategory + "=" + location.pathname;


// -->
</SCRIPT>
<noscript>
<p align="center">
<a href="http://ad.doubleclick.net/jump/www.codeguru.com/cpp;ord=Nupbf9FCY34AAHkKVkA">
<img src="http://ad.doubleclick.net/ad/www.codeguru.com/cpp;ord=Nupbf9FCY34AAHkKVkA"></a>
</p>
</noscript>





<BR Clear>
<H1>Function Objects</H1>

<Table CellPadding=0 CellSpacing=0 width=100%>
<TR>
<TD Align=left><Img src = "functors.gif" Alt=""   WIDTH = "194"  HEIGHT = "38" ></TD>
<TD Align=right><Img src = "overview.gif" Alt=""   WIDTH = "194"  HEIGHT = "38" ></TD>
</TR>
<TR>
<TD Align=left VAlign=top><b>Category</b>: functors</TD>
<TD Align=right VAlign=top><b>Component type</b>: overview</TD>
</TR>
</Table>

<h3>Summary</h3>
A <i>Function Object</i>, or <i>Functor</i> (the two terms are synonymous)
is simply any object that can be called as if it is a function.
An ordinary function is a function object, and so is a function pointer;
more generally, so is an object of a class that defines
<tt>operator()</tt>.
<h3>Description</h3>
The basic function object concepts are <A href="Generator.html">Generator</A>,
<A href="UnaryFunction.html">Unary Function</A>, and <A href="BinaryFunction.html" tppabs="http://www.sgi.com/Technology/STL/BinaryFunction.shtml">Binary Function</A>: these describe,
respectively, objects that can be called as <tt>f()</tt>, <tt>f(x)</tt>, and
<tt>f(x,y)</tt>.  (This list could obviously be extended to <i>ternary function</i>
and beyond, but, in practice, no STL algorithms require function
objects of more than two arguments.)  All other function object
concepts defined by the STL are refinements of these three.
<P>
Function objects that return <tt>bool</tt> are
an important special case.
A <A href="UnaryFunction.html">Unary Function</A> whose return type is <tt>bool</tt> is called a
<A href="Predicate.html">Predicate</A>, and a <A href="BinaryFunction.html" tppabs="http://www.sgi.com/Technology/STL/BinaryFunction.shtml">Binary Function</A> whose return type is 
<tt>bool</tt> is called a <A href="BinaryPredicate.html">Binary Predicate</A>.
<P>
There is an important distinction, but a somewhat subtle one, between
function objects and <i>adaptable function objects</i>. <A href="#1">[1]</A>  In general, a
function object has restrictions on the type of its argument.  The
type restrictions need not be simple, though: <tt>operator()</tt> may be
overloaded, or may be a member template, or both.  Similarly, there
need be no way for a program to determine what those restrictions are.
An adaptable function object, however, does specify what the argument
and return types are, and provides nested <tt>typedef</tt>s so that those
types can be named and used in programs.  If a type <tt>F0</tt> is a model of
<A href="AdaptableGenerator.html">Adaptable Generator</A>, then it must define
<tt>F0::result_type</tt>.  Similarly, if <tt>F1</tt> is a model of 
<A href="AdaptableUnaryFunction.html">Adaptable Unary Function</A> then it must define 
<tt>F1::argument_type</tt> and <tt>F1::result_type</tt>, and if <tt>F2</tt> is a model
of <A href="AdaptableBinaryFunction.html">Adaptable Binary Function</A> then it must define 
<tt>F2::first_argument_type</tt>, <tt>F2::second_argument_type</tt>, and
<tt>F2::result_type</tt>.
The STL provides base classes <tt><A href="unary_function.html">unary_function</A></tt> and
<tt><A href="binary_function.html">binary_function</A></tt> to simplify the definition of
<A href="AdaptableUnaryFunction.html">Adaptable Unary Functions</A> and <A href="AdaptableBinaryFunction.html" tppabs="http://www.sgi.com/Technology/STL/AdaptableBinaryFunction.shtml">Adaptable Binary Functions</A>. <A href="#2">[2]</A>
<P>
Adaptable function objects are important because they can be used by
<i>function object adaptors</i>: function objects that transform or
manipulate other function objects.  The STL provides many different
function object adaptors, including <tt><A href="unary_negate.html">unary_negate</A></tt> (which returns
the logical complement of the value returned by a particular
<A href="AdaptablePredicate.html">AdaptablePredicate</A>), and <tt><A href="unary_compose.html" tppabs="http://www.sgi.com/Technology/STL/unary_compose.shtml">unary_compose</A></tt> and
<tt><A href="binary_compose.html">binary_compose</A></tt>, which perform composition of function object.
<P>
Finally, the STL includes many different predefined function 
objects, including arithmetic operations
(<tt><A href="plus.html">plus</A></tt>, <tt><A href="minus.html" tppabs="http://www.sgi.com/Technology/STL/minus.shtml">minus</A></tt>, <tt><A href="times.html" tppabs="http://www.sgi.com/Technology/STL/times.shtml">multiplies</A></tt>, <tt><A href="divides.html" tppabs="http://www.sgi.com/Technology/STL/divides.shtml">divides</A></tt>, <tt><A href="modulus.html" tppabs="http://www.sgi.com/Technology/STL/modulus.shtml">modulus</A></tt>,
and <tt><A href="negate.html">negate</A></tt>), comparisons (<tt><A href="equal_to.html" tppabs="http://www.sgi.com/Technology/STL/equal_to.shtml">equal_to</A></tt>, <tt><A href="not_equal_to.html" tppabs="http://www.sgi.com/Technology/STL/not_equal_to.shtml">not_equal_to</A></tt>
<tt><A href="greater.html">greater</A></tt>, <tt><A href="less.html" tppabs="http://www.sgi.com/Technology/STL/less.shtml">less</A></tt>, <tt><A href="greater_equal.html" tppabs="http://www.sgi.com/Technology/STL/greater_equal.shtml">greater_equal</A></tt>, and <tt><A href="less_equal.html" tppabs="http://www.sgi.com/Technology/STL/less_equal.shtml">less_equal</A></tt>),
and logical operations (<tt><A href="logical_and.html">logical_and</A></tt>, <tt><A href="logical_or.html" tppabs="http://www.sgi.com/Technology/STL/logical_or.shtml">logical_or</A></tt>, and
<tt><A href="logical_not.html">logical_not</A></tt>).  It is possible to perform very sophisticated
operations without actually writing a new function object, simply
by combining predefined function objects and function object
adaptors.
<h3>Examples</h3>
Fill a <tt><A href="Vector.html">vector</A></tt> with random numbers.  In this example, the function object
is simply a function pointer.
<pre>
    <A href="Vector.html">vector</A>&lt;int&gt; V(100);
    <A href="generate.html">generate</A>(V.begin(), V.end(), rand);
</pre>
<P>
Sort a <tt><A href="Vector.html">vector</A></tt> of <tt>double</tt> by magnitude, <i>i.e.</i> ignoring the elements' signs.
In this example, the function object is an object of a user-defined
class.
<pre>
    struct less_mag : public <A href="binary_function.html">binary_function</A>&lt;double, double, bool&gt; {
	bool operator()(double x, double y) { return fabs(x) &lt; fabs(y); }
    };

    <A href="Vector.html">vector</A>&lt;double&gt; V;
    ...
    <A href="sort.html">sort</A>(V.begin(), V.end(), less_mag());
</pre>
<P>
Find the sum of elements in a <tt><A href="Vector.html">vector</A></tt>.  In this example, the function
object is of a user-defined class that has local state.
<pre>
    struct adder : public <A href="unary_function.html">unary_function</A>&lt;double, void&gt;
    {
      adder() : sum(0) {}
      double sum;
      void operator()(double x) { sum += x; }
    };

    <A href="Vector.html">vector</A>&lt;double&gt; V;
    ...
    adder result = <A href="for_each.html">for_each</A>(V.begin(), V.end(), adder()); <A href="#3">[3]</A>
    cout &lt;&lt; &quot;The sum is &quot; &lt;&lt; result.sum &lt;&lt; endl;
</pre>
<P>
Remove all elements from a <tt><A href="List.html">list</A></tt> that are greater than 100 and
less than 1000.
<pre>
    <A href="List.html">list</A>&lt;int&gt; L;
    ...
    <A href="List.html">list</A>&lt;int&gt;::iterator new_end = 
	 <A href="remove_if.html">remove_if</A>(L.begin(), L.end(),
		   <A href="binary_compose.html">compose2</A>(<A href="logical_and.html" tppabs="http://www.sgi.com/Technology/STL/logical_and.shtml">logical_and</A>&lt;bool&gt;(),
			    <A href="binder2nd.html">bind2nd</A>(<A href="greater.html" tppabs="http://www.sgi.com/Technology/STL/greater.shtml">greater</A>&lt;int&gt;(), 100),
			    <A href="binder2nd.html">bind2nd</A>(<A href="less.html" tppabs="http://www.sgi.com/Technology/STL/less.shtml">less</A>&lt;int&gt;(), 1000)));
    L.erase(new_end, L.end());
</pre>
<h3>Concepts</h3>
<UL>
<LI>
 <A href="Generator.html">Generator</A>
<LI>
 <A href="UnaryFunction.html">Unary Function</A>
<LI>
 <A href="BinaryFunction.html">Binary Function</A>
</UL>
<UL>
<LI>
 <A href="Predicate.html">Predicate</A>

⌨️ 快捷键说明

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