home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C21 / FunctionObjects.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  2.9 KB  |  97 lines

  1. //: C21:FunctionObjects.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Using the predefined function object templates
  7. // in the Standard C++ library
  8. // This will be defined shortly:
  9. #include "Generators.h"
  10. #include <algorithm>
  11. #include <vector>
  12. #include <iostream>
  13. #include <functional>
  14. using namespace std;
  15.  
  16. template<typename T> 
  17. void print(vector<T>& v, char* msg = "") {
  18.   if(*msg != 0)
  19.     cout << msg << ":" << endl;
  20.   copy(v.begin(), v.end(), 
  21.     ostream_iterator<T>(cout, " "));
  22.   cout << endl;
  23. }
  24.  
  25. template<typename Contain, typename UnaryFunc> 
  26. void testUnary(Contain& source, Contain& dest,
  27.   UnaryFunc f) {
  28.   transform(source.begin(), source.end(), 
  29.     dest.begin(), f);
  30. }
  31.  
  32. template<typename Contain1, typename Contain2, 
  33.   typename BinaryFunc> 
  34. void testBinary(Contain1& src1, Contain1& src2,
  35.   Contain2& dest, BinaryFunc f) {
  36.   transform(src1.begin(), src1.end(), 
  37.     src2.begin(), dest.begin(), f);
  38. }
  39.  
  40. // Executes the expression, then stringizes the
  41. // expression into the print statement:
  42. #define T(EXPR) EXPR; print(r, "After " #EXPR);
  43. // For Boolean tests:
  44. #define B(EXPR) EXPR; print(br,"After " #EXPR);
  45.  
  46. // Boolean random generator:
  47. struct BRand {
  48.   BRand() { srand(time(0)); }
  49.   bool operator()() {
  50.     return rand() > RAND_MAX / 2;
  51.   }
  52. };
  53.  
  54. int main() {
  55.   const int sz = 10;
  56.   const int max = 50;
  57.   vector<int> x(sz), y(sz), r(sz);
  58.   // An integer random number generator:
  59.   URandGen urg(max);
  60.   generate_n(x.begin(), sz, urg);
  61.   generate_n(y.begin(), sz, urg);
  62.   // Add one to each to guarantee nonzero divide:
  63.   transform(y.begin(), y.end(), y.begin(),
  64.     bind2nd(plus<int>(), 1));
  65.   // Guarantee one pair of elements is ==:
  66.   x[0] = y[0];
  67.   print(x, "x");
  68.   print(y, "y");
  69.   // Operate on each element pair of x & y,
  70.   // putting the result into r:
  71.   T(testBinary(x, y, r, plus<int>()));
  72.   T(testBinary(x, y, r, minus<int>()));
  73.   T(testBinary(x, y, r, multiplies<int>()));
  74.   T(testBinary(x, y, r, divides<int>()));
  75.   T(testBinary(x, y, r, modulus<int>()));
  76.   T(testUnary(x, r, negate<int>()));
  77.   vector<bool> br(sz); // For Boolean results
  78.   B(testBinary(x, y, br, equal_to<int>()));
  79.   B(testBinary(x, y, br, not_equal_to<int>()));
  80.   B(testBinary(x, y, br, greater<int>()));
  81.   B(testBinary(x, y, br, less<int>()));
  82.   B(testBinary(x, y, br, greater_equal<int>()));
  83.   B(testBinary(x, y, br, less_equal<int>()));
  84.   B(testBinary(x, y, br, 
  85.     not2(greater_equal<int>())));
  86.   B(testBinary(x,y,br,not2(less_equal<int>())));
  87.   vector<bool> b1(sz), b2(sz);
  88.   generate_n(b1.begin(), sz, BRand());
  89.   generate_n(b2.begin(), sz, BRand());
  90.   print(b1, "b1");
  91.   print(b2, "b2");
  92.   B(testBinary(b1, b2, br, logical_and<int>()));
  93.   B(testBinary(b1, b2, br, logical_or<int>()));
  94.   B(testUnary(b1, br, logical_not<int>()));
  95.   B(testUnary(b1, br, not1(logical_not<int>())));
  96. } ///:~
  97.