home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / arrays.pak / SARRAY.CPP < prev   
C/C++ Source or Header  |  1997-07-23  |  2KB  |  65 lines

  1. // ---------------------------------------------------------------------------
  2. // Copyright (C) 1994 Borland International
  3. // sarray.cpp
  4. //    Must link with myclass.cpp.
  5. // ---------------------------------------------------------------------------
  6.  
  7. #include <classlib/arrays.h>
  8. #include <iostream.h>
  9. #include <strstrea.h>
  10. #include "../myclass.h" 
  11.  
  12. typedef TSArrayAsVector<MyClass> ContainerType;
  13. typedef TSArrayAsVectorIterator<MyClass> IteratorType;
  14.  
  15. const int MaxItems=6;
  16.  
  17. void ForEachCallBack(MyClass& mc, void* s)
  18. {
  19.     cout << (char*)s << mc << endl;
  20. }
  21.  
  22. void AddItems(ContainerType& container, int numItems)
  23. {
  24.     for( int i=numItems; i>0; i-- )
  25.         {
  26.         char buf[80];
  27.         ostrstream str( buf, sizeof(buf) );
  28.         str << i << " hello" << ends;
  29.         container.Add(MyClass(buf));
  30.         }
  31. }
  32.  
  33. void UseForwardIterator(ContainerType& container)
  34. {
  35.     IteratorType iterator(container);
  36.     while( iterator )
  37.         {
  38.         cout << iterator.Current() << endl;
  39.         iterator++;
  40.         }
  41. }
  42.  
  43. void UseIndex(ContainerType& container)
  44. {
  45.     for( int i=container.LowerBound(); i<container.UpperBound(); i++ )
  46.         cout << container[i] << endl;
  47. }
  48.  
  49. int main()
  50. {
  51.     ContainerType container(MaxItems);
  52.     AddItems(container, MaxItems);
  53.   
  54.     cout << "--- Starting ForEach" << endl;
  55.     container.ForEach(ForEachCallBack, (void*)"FE ");
  56.   
  57.     cout << "--- Starting Iterator (forward)" << endl;
  58.     UseForwardIterator(container);
  59.   
  60.     cout << "--- Using operator[]" << endl;
  61.     UseIndex(container);
  62.   
  63.     return 0;
  64. }
  65.