home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C16 / TStashTest.cpp < prev   
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.5 KB  |  59 lines

  1. //: C16:TStashTest.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. // Test TStash
  7. #include "TStash.h"
  8. #include "../require.h"
  9. #include <fstream>
  10. #include <vector>
  11. #include <string>
  12. using namespace std;
  13. ofstream out("tstest.out");
  14.  
  15. class Int {
  16.   int i;
  17. public:
  18.   Int(int ii = 0) : i(ii) {
  19.     out << ">" << i << endl;
  20.   }
  21.   ~Int() { out << "~" << i << endl; }
  22.   operator int() const { return i; }
  23.   friend ostream&
  24.     operator<<(ostream& os, const Int& x) {
  25.       return os << x.i;
  26.   }
  27. };
  28.  
  29. int main() {
  30.   TStash<Int> intStash; // Instantiate for Int
  31.   for(int i = 0; i < 30; i++)
  32.     intStash.add(new Int(i));
  33.   TStashIter<Int> intIter(intStash);
  34.   intIter.forward(5);
  35.   for(int j = 0; j < 20; j++, intIter++)
  36.     intIter.remove(); // Default removal
  37.   for(int k = 0; k < intStash.count(); k++)
  38.     if(intStash[k]) // Remove() causes "holes"
  39.       out << *intStash[k] << endl;
  40.  
  41.   ifstream file("TStashTest.cpp");
  42.   assure(file, "TStashTest.cpp");
  43.   // Instantiate for String:
  44.   TStash<string> stringStash;
  45.   string line;
  46.   while(getline(file, line))
  47.     stringStash.add(new string(line));
  48.   for(int u = 0; u < stringStash.count(); u++)
  49.     if(stringStash[u])
  50.       out << *stringStash[u] << endl;
  51.   TStashIter<string> it(stringStash);
  52.   int n = 25;
  53.   it.forward(n);
  54.   while(it) {
  55.     out << n++ << ": " << it->c_str() << endl;
  56.     it++;
  57.   }
  58. } ///:~
  59.