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

  1. //: C20:StringDeque.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. // Converted from StringVector.cpp
  7. #include "../require.h"
  8. #include <string>
  9. #include <deque>
  10. #include <vector>
  11. #include <fstream>
  12. #include <iostream>
  13. #include <iterator>
  14. #include <sstream>
  15. #include <ctime>
  16. using namespace std;
  17.  
  18. int main(int argc, char* argv[]) {
  19.   requireArgs(argc, 1);
  20.   ifstream in(argv[1]);
  21.   assure(in, argv[1]);
  22.   vector<string> vstrings;
  23.   deque<string> dstrings;
  24.   string line;
  25.   // Time reading into vector:
  26.   clock_t ticks = clock();
  27.   while(getline(in, line))
  28.     vstrings.push_back(line);
  29.   ticks = clock() - ticks;
  30.   cout << "Read into vector: " << ticks << endl;
  31.   // Repeat for deque:
  32.   ifstream in2(argv[1]);
  33.   assure(in2, argv[1]);
  34.   ticks = clock();
  35.   while(getline(in2, line))
  36.     dstrings.push_back(line);
  37.   ticks = clock() - ticks;
  38.   cout << "Read into deque: " << ticks << endl;
  39.   // Now compare indexing:
  40.   ticks = clock();
  41.   for(int i = 0; i < vstrings.size(); i++) {
  42.     ostringstream ss;
  43.     ss << i;
  44.     vstrings[i] = ss.str() + ": " + vstrings[i];
  45.   }
  46.   ticks = clock() - ticks;
  47.   cout << "Indexing vector: " << ticks << endl;
  48.   ticks = clock();
  49.   for(int j = 0; j < dstrings.size(); j++) {
  50.     ostringstream ss;
  51.     ss << j;
  52.     dstrings[j] = ss.str() + ": " + dstrings[j];
  53.   }
  54.   ticks = clock() - ticks;
  55.   cout << "Indexing deqeue: " << ticks << endl;
  56.   // Compare iteration
  57.   ofstream tmp1("tmp1.tmp"), tmp2("tmp2.tmp");
  58.   ticks = clock();
  59.   copy(vstrings.begin(), vstrings.end(),
  60.     ostream_iterator<string>(tmp1, "\n"));
  61.   ticks = clock() - ticks;
  62.   cout << "Iterating vector: " << ticks << endl;
  63.   ticks = clock();
  64.   copy(dstrings.begin(), dstrings.end(),
  65.     ostream_iterator<string>(tmp2, "\n"));
  66.   ticks = clock() - ticks;
  67.   cout << "Iterating deqeue: " << ticks << endl;
  68. } ///:~
  69.