home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / LISTS.PAK / SDLIST.CPP < prev    next >
C/C++ Source or Header  |  1995-08-29  |  2KB  |  75 lines

  1. // ---------------------------------------------------------------------------
  2. // Copyright (C) 1994 Borland International
  3. // sdlist.cpp
  4. //    Must link with myclass.cpp.
  5. // ---------------------------------------------------------------------------
  6.  
  7. #include <classlib/dlistimp.h>
  8. #include <iostream.h>
  9. #include <strstrea.h>
  10. #include "../myclass.h"
  11.  
  12. typedef TSDoubleListImp<MyClass> ContainerType;
  13. typedef TSDoubleListIteratorImp<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.         MyClass tempItem(buf);
  30.         container.Add(tempItem);
  31.         }
  32. }
  33.  
  34. void UseForwardIterator(ContainerType& container)
  35. {
  36.     IteratorType iterator(container);
  37.     while( iterator )
  38.         {
  39.         cout << iterator.Current() << endl;
  40.         iterator++;
  41.         }
  42. }
  43.  
  44. void UseBackwardIterator(ContainerType& container)
  45. {
  46.     IteratorType iterator(container);
  47.     iterator.RestartAtTail();
  48.     while( iterator )
  49.         {
  50.         cout << iterator.Current() << endl;
  51.         iterator--;
  52.         }
  53. }
  54.  
  55. int main()
  56. {
  57.     ContainerType container;
  58.     AddItems(container, MaxItems);
  59.   
  60.     cout << "--- Starting ForEach" << endl;
  61.     container.ForEach(ForEachCallBack, (void*)"FE ");
  62.   
  63.     cout << "--- Starting Iterator (forward)" << endl;
  64.     UseForwardIterator(container);
  65.   
  66.     cout << "--- Deleting '3 hello'" << endl;
  67.     container.Detach(MyClass("3 hello"));  // detach and delete
  68.   
  69.     cout << "--- Starting Iterator (backward)" << endl;
  70.     UseBackwardIterator(container);
  71.   
  72.     return 0;
  73. }
  74.  
  75.