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

📄 chap08.htm

📁 C++编程思想,很不错的一本介绍C++的书,学C++的同志们必备的东西.
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">BinaryFunction</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">arg1 / arg2</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">modulus</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">BinaryFunction</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">arg1 % arg2</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">negate</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">UnaryFunction</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">- arg1</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">equal_to</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">BinaryPredicate</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">arg1 == arg2</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">not_equal_to</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">BinaryPredicate</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">arg1 != arg2</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">greater</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">BinaryPredicate</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">arg1 &gt; arg2</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">less</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">BinaryPredicate</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">arg1 &lt; arg2</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">greater_equal</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">BinaryPredicate</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">arg1 &gt;= arg2</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">less_equal</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">BinaryPredicate</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">arg1 &lt;= arg2</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">logical_and</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">BinaryPredicate</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">arg1 &amp;&amp; arg2</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">logical_or</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">BinaryPredicate</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">arg1 || arg2</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">logical_not</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">UnaryPredicate</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">!arg1</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">not1(&#160;)</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Unary Logical</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">!(UnaryPredicate(arg1))</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">not2(&#160;)</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Binary Logical</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">!(BinaryPredicate(arg1,
arg2))</FONT><BR></P></DIV>
</TD>
</TR>
</TABLE></P></DIV>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The following example provides simple
tests for each of the built-in basic function object templates. This way, you
can see how to use each one, along with their resulting
behavior.</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C08:FunctionObjects.cpp</font>
<font color=#009900>// Using the predefined function object templates</font>
<font color=#009900>// in the Standard C++ library</font>
<font color=#009900>//{L} ../TestSuite/Test</font>
<font color=#009900>// This will be defined shortly:</font>
#include <font color=#004488>"Generators.h"</font>
#include &lt;algorithm&gt;
#include &lt;vector&gt;
#include &lt;iostream&gt;
#include &lt;functional&gt;
<font color=#0000ff>using</font> <font color=#0000ff>namespace</font> std;

<font color=#0000ff>template</font>&lt;<font color=#0000ff>typename</font> T&gt; 
<font color=#0000ff>void</font> print(vector&lt;T&gt;&amp; v, <font color=#0000ff>char</font>* msg = <font color=#004488>""</font>) {
  <font color=#0000ff>if</font>(*msg != 0)
    cout &lt;&lt; msg &lt;&lt; <font color=#004488>":"</font> &lt;&lt; endl;
  copy(v.begin(), v.end(), 
    ostream_iterator&lt;T&gt;(cout, <font color=#004488>" "</font>));
  cout &lt;&lt; endl;
}

<font color=#0000ff>template</font>&lt;<font color=#0000ff>typename</font> Contain, <font color=#0000ff>typename</font> UnaryFunc&gt; 
<font color=#0000ff>void</font> testUnary(Contain&amp; source, Contain&amp; dest,
  UnaryFunc f) {
  transform(source.begin(), source.end(), 
    dest.begin(), f);
}

<font color=#0000ff>template</font>&lt;<font color=#0000ff>typename</font> Contain1, <font color=#0000ff>typename</font> Contain2, 
  <font color=#0000ff>typename</font> BinaryFunc&gt; 
<font color=#0000ff>void</font> testBinary(Contain1&amp; src1, Contain1&amp; src2,
  Contain2&amp; dest, BinaryFunc f) {
  transform(src1.begin(), src1.end(), 
    src2.begin(), dest.begin(), f);
}

<font color=#009900>// Executes the expression, then stringizes the</font>
<font color=#009900>// expression into the print statement:</font>
#define T(EXPR) EXPR; print(r, <font color=#004488>"After "</font> #EXPR);
<font color=#009900>// For Boolean tests:</font>
#define B(EXPR) EXPR; print(br,<font color=#004488>"After "</font> #EXPR);

<font color=#009900>// Boolean random generator:</font>
<font color=#0000ff>struct</font> BRand {
  BRand() { srand(time(0)); }
  <font color=#0000ff>bool</font> <font color=#0000ff>operator</font>()() {
    <font color=#0000ff>return</font> rand() &gt; RAND_MAX / 2;
  }
};

<font color=#0000ff>int</font> main() {
  <font color=#0000ff>const</font> <font color=#0000ff>int</font> sz = 10;
  <font color=#0000ff>const</font> <font color=#0000ff>int</font> max = 50;
  vector&lt;<font color=#0000ff>int</font>&gt; x(sz), y(sz), r(sz);
  <font color=#009900>// An integer random number generator:</font>
  URandGen urg(max);
  generate_n(x.begin(), sz, urg);
  generate_n(y.begin(), sz, urg);
  <font color=#009900>// Add one to each to guarantee nonzero divide:</font>
  transform(y.begin(), y.end(), y.begin(),
    bind2nd(plus&lt;<font color=#0000ff>int</font>&gt;(), 1));
  <font color=#009900>// Guarantee one pair of elements is ==:</font>
  x[0] = y[0];
  print(x, <font color=#004488>"x"</font>);
  print(y, <font color=#004488>"y"</font>);
  <font color=#009900>// Operate on each element pair of x &amp; y,</font>
  <font color=#009900>// putting the result into r:</font>
  T(testBinary(x, y, r, plus&lt;<font color=#0000ff>int</font>&gt;()));
  T(testBinary(x, y, r, minus&lt;<font color=#0000ff>int</font>&gt;()));
  T(testBinary(x, y, r, multiplies&lt;<font color=#0000ff>int</font>&gt;()));
  T(testBinary(x, y, r, divides&lt;<font color=#0000ff>int</font>&gt;()));
  T(testBinary(x, y, r, modulus&lt;<font color=#0000ff>int</font>&gt;()));
  T(testUnary(x, r, negate&lt;<font color=#0000ff>int</font>&gt;()));
  vector&lt;<font color=#0000ff>bool</font>&gt; br(sz); <font color=#009900>// For Boolean results</font>
  B(testBinary(x, y, br, equal_to&lt;<font color=#0000ff>int</font>&gt;()));
  B(testBinary(x, y, br, not_equal_to&lt;<font color=#0000ff>int</font>&gt;()));
  B(testBinary(x, y, br, greater&lt;<font color=#0000ff>int</font>&gt;()));
  B(testBinary(x, y, br, less&lt;<font color=#0000ff>int</font>&gt;()));
  B(testBinary(x, y, br, greater_equal&lt;<font color=#0000ff>int</font>&gt;()));
  B(testBinary(x, y, br, less_equal&lt;<font color=#0000ff>int</font>&gt;()));
  B(testBinary(x, y, br, 
    not2(greater_equal&lt;<font color=#0000ff>int</font>&gt;())));
  B(testBinary(x,y,br,not2(less_equal&lt;<font color=#0000ff>int</font>&gt;())));
  vector&lt;<font color=#0000ff>bool</font>&gt; b1(sz), b2(sz);
  generate_n(b1.begin(), sz, BRand());
  generate_n(b2.begin(), sz, BRand());
  print(b1, <font color=#004488>"b1"</font>);
  print(b2, <font color=#004488>"b2"</font>);
  B(testBinary(b1, b2, br, logical_and&lt;<font color=#0000ff>int</font>&gt;()));
  B(testBinary(b1, b2, br, logical_or&lt;<font color=#0000ff>int</font>&gt;()));
  B(testUnary(b1, br, logical_not&lt;<font color=#0000ff>int</font>&gt;()));
  B(testUnary(b1, br, not1(logical_not&lt;<font color=#0000ff>int</font>&gt;())));
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To keep this example small, some tools
are created. The <B>print(&#160;)</B> template is designed to print any

⌨️ 快捷键说明

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