📄 chap08.htm
字号:
<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 > 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 < 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 >= 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 <= 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 && 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( )</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( )</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 <algorithm>
#include <vector>
#include <iostream>
#include <functional>
<font color=#0000ff>using</font> <font color=#0000ff>namespace</font> std;
<font color=#0000ff>template</font><<font color=#0000ff>typename</font> T>
<font color=#0000ff>void</font> print(vector<T>& v, <font color=#0000ff>char</font>* msg = <font color=#004488>""</font>) {
<font color=#0000ff>if</font>(*msg != 0)
cout << msg << <font color=#004488>":"</font> << endl;
copy(v.begin(), v.end(),
ostream_iterator<T>(cout, <font color=#004488>" "</font>));
cout << endl;
}
<font color=#0000ff>template</font><<font color=#0000ff>typename</font> Contain, <font color=#0000ff>typename</font> UnaryFunc>
<font color=#0000ff>void</font> testUnary(Contain& source, Contain& dest,
UnaryFunc f) {
transform(source.begin(), source.end(),
dest.begin(), f);
}
<font color=#0000ff>template</font><<font color=#0000ff>typename</font> Contain1, <font color=#0000ff>typename</font> Contain2,
<font color=#0000ff>typename</font> BinaryFunc>
<font color=#0000ff>void</font> testBinary(Contain1& src1, Contain1& src2,
Contain2& 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() > 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<<font color=#0000ff>int</font>> 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<<font color=#0000ff>int</font>>(), 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 & y,</font>
<font color=#009900>// putting the result into r:</font>
T(testBinary(x, y, r, plus<<font color=#0000ff>int</font>>()));
T(testBinary(x, y, r, minus<<font color=#0000ff>int</font>>()));
T(testBinary(x, y, r, multiplies<<font color=#0000ff>int</font>>()));
T(testBinary(x, y, r, divides<<font color=#0000ff>int</font>>()));
T(testBinary(x, y, r, modulus<<font color=#0000ff>int</font>>()));
T(testUnary(x, r, negate<<font color=#0000ff>int</font>>()));
vector<<font color=#0000ff>bool</font>> br(sz); <font color=#009900>// For Boolean results</font>
B(testBinary(x, y, br, equal_to<<font color=#0000ff>int</font>>()));
B(testBinary(x, y, br, not_equal_to<<font color=#0000ff>int</font>>()));
B(testBinary(x, y, br, greater<<font color=#0000ff>int</font>>()));
B(testBinary(x, y, br, less<<font color=#0000ff>int</font>>()));
B(testBinary(x, y, br, greater_equal<<font color=#0000ff>int</font>>()));
B(testBinary(x, y, br, less_equal<<font color=#0000ff>int</font>>()));
B(testBinary(x, y, br,
not2(greater_equal<<font color=#0000ff>int</font>>())));
B(testBinary(x,y,br,not2(less_equal<<font color=#0000ff>int</font>>())));
vector<<font color=#0000ff>bool</font>> 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<<font color=#0000ff>int</font>>()));
B(testBinary(b1, b2, br, logical_or<<font color=#0000ff>int</font>>()));
B(testUnary(b1, br, logical_not<<font color=#0000ff>int</font>>()));
B(testUnary(b1, br, not1(logical_not<<font color=#0000ff>int</font>>())));
} <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( )</B> template is designed to print any
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -