//: C05:FunctionObjects.cpp // From Thinking in C++, 2nd Edition // Available at http://www.BruceEckel.com // (c) Bruce Eckel 2000 // Copyright notice in Copyright.txt // Using the predefined function object templates // in the Standard C++ library // This will be defined shortly: #include "Generators.h" #include #include #include #include using namespace std; template void print(vector& v, char* msg = "") { if(*msg != 0) cout << msg << ":" << endl; copy(v.begin(), v.end(), ostream_iterator(cout, " ")); cout << endl; } template void testUnary(Contain& source, Contain& dest, UnaryFunc f) { transform(source.begin(), source.end(), dest.begin(), f); } template void testBinary(Contain1& src1, Contain1& src2, Contain2& dest, BinaryFunc f) { transform(src1.begin(), src1.end(), src2.begin(), dest.begin(), f); } // Executes the expression, then stringizes the // expression into the print statement: #define T(EXPR) EXPR; print(r, "After " #EXPR); // For Boolean tests: #define B(EXPR) EXPR; print(br,"After " #EXPR); // Boolean random generator: struct BRand { BRand() { srand(time(0)); } bool operator()() { return rand() > RAND_MAX / 2; } }; int main() { const int sz = 10; const int max = 50; vector x(sz), y(sz), r(sz); // An integer random number generator: URandGen urg(max); generate_n(x.begin(), sz, urg); generate_n(y.begin(), sz, urg); // Add one to each to guarantee nonzero divide: transform(y.begin(), y.end(), y.begin(), bind2nd(plus(), 1)); // Guarantee one pair of elements is ==: x[0] = y[0]; print(x, "x"); print(y, "y"); // Operate on each element pair of x & y, // putting the result into r: T(testBinary(x, y, r, plus())); T(testBinary(x, y, r, minus())); T(testBinary(x, y, r, multiplies())); T(testBinary(x, y, r, divides())); T(testBinary(x, y, r, modulus())); T(testUnary(x, r, negate())); vector br(sz); // For Boolean results B(testBinary(x, y, br, equal_to())); B(testBinary(x, y, br, not_equal_to())); B(testBinary(x, y, br, greater())); B(testBinary(x, y, br, less())); B(testBinary(x, y, br, greater_equal())); B(testBinary(x, y, br, less_equal())); B(testBinary(x, y, br, not2(greater_equal()))); B(testBinary(x,y,br,not2(less_equal()))); vector b1(sz), b2(sz); generate_n(b1.begin(), sz, BRand()); generate_n(b2.begin(), sz, BRand()); print(b1, "b1"); print(b2, "b2"); B(testBinary(b1, b2, br, logical_and())); B(testBinary(b1, b2, br, logical_or())); B(testUnary(b1, br, logical_not())); B(testUnary(b1, br, not1(logical_not()))); } ///:~