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

  1. //: C20:BasicSequenceOperations.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. // The operations available for all the 
  7. // basic sequence Containers.
  8. #include <iostream>
  9. #include <vector>
  10. #include <deque>
  11. #include <list>
  12. using namespace std;
  13.  
  14. template<typename Container>
  15. void print(Container& c, char* s = "") {
  16.   cout << s << ":" << endl;
  17.   if(c.empty()) {
  18.     cout << "(empty)" << endl;
  19.     return;
  20.   }
  21.   typename Container::iterator it;
  22.   for(it = c.begin(); it != c.end(); it++)
  23.     cout << *it << " ";
  24.   cout << endl;
  25.   cout << "size() " << c.size() 
  26.     << " max_size() "<< c.max_size() 
  27.     << " front() " << c.front()
  28.     << " back() " << c.back() << endl;
  29. }
  30.   
  31. template<typename ContainerOfInt>
  32. void basicOps(char* s) {
  33.   cout << "------- " << s << " -------" << endl;
  34.   typedef ContainerOfInt Ci;
  35.   Ci c;
  36.   print(c, "c after default constructor");
  37.   Ci c2(10, 1); // 10 elements, values all 1
  38.   print(c2, "c2 after constructor(10,1)");
  39.   int ia[] = { 1, 3, 5, 7, 9 };
  40.   const int iasz = sizeof(ia)/sizeof(*ia);
  41.   // Initialize with begin & end iterators:
  42.   Ci c3(ia, ia + iasz);
  43.   print(c3, "c3 after constructor(iter,iter)");
  44.   Ci c4(c2); // Copy-constructor
  45.   print(c4, "c4 after copy-constructor(c2)");
  46.   c = c2; // Assignment operator
  47.   print(c, "c after operator=c2");
  48.   c.assign(10, 2); // 10 elements, values all 2
  49.   print(c, "c after assign(10, 2)");
  50.   // Assign with begin & end iterators:
  51.   c.assign(ia, ia + iasz);
  52.   print(c, "c after assign(iter, iter)");
  53.   cout << "c using reverse iterators:" << endl;
  54.   typename Ci::reverse_iterator rit = c.rbegin();
  55.   while(rit != c.rend())
  56.     cout << *rit++ << " ";
  57.   cout << endl;
  58.   c.resize(4);
  59.   print(c, "c after resize(4)");
  60.   c.push_back(47);
  61.   print(c, "c after push_back(47)");
  62.   c.pop_back();
  63.   print(c, "c after pop_back()");
  64.   typename Ci::iterator it = c.begin();
  65.   it++; it++;
  66.   c.insert(it, 74);
  67.   print(c, "c after insert(it, 74)");
  68.   it = c.begin();
  69.   it++;
  70.   c.insert(it, 3, 96);
  71.   print(c, "c after insert(it, 3, 96)");
  72.   it = c.begin();
  73.   it++;
  74.   c.insert(it, c3.begin(), c3.end());
  75.   print(c, "c after insert("
  76.     "it, c3.begin(), c3.end())");
  77.   it = c.begin();
  78.   it++;
  79.   c.erase(it);
  80.   print(c, "c after erase(it)");
  81.   typename Ci::iterator it2 = it = c.begin();
  82.   it++;
  83.   it2++; it2++; it2++; it2++; it2++;
  84.   c.erase(it, it2);
  85.   print(c, "c after erase(it, it2)");
  86.   c.swap(c2);
  87.   print(c, "c after swap(c2)");
  88.   c.clear();
  89.   print(c, "c after clear()");
  90. }
  91.  
  92. int main() {
  93.   basicOps<vector<int> >("vector");
  94.   basicOps<deque<int> >("deque");
  95.   basicOps<list<int> >("list");
  96. } ///:~
  97.