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

  1. // ---------------------------------------------------------------------------
  2. // Copyright (C) 1994 Borland International
  3. // idlist.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 TIDoubleListImp<MyClass> ContainerType;
  13. typedef TIDoubleListIteratorImp<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.         if( i%2 == 0 )
  30.             container.Add( new MyClass(buf) );
  31.         else
  32.             container.AddAtTail( new MyClass(buf) );
  33.         }
  34. }
  35.  
  36. void UseForwardIterator(ContainerType& container)
  37. {
  38.     IteratorType iterator(container);
  39.     while( iterator )
  40.         {
  41.         cout << *iterator.Current() << endl;
  42.         iterator++;
  43.         }
  44. }
  45.  
  46. void UseBackwardIterator(ContainerType& container)
  47. {
  48.     IteratorType iterator(container);
  49.     iterator.RestartAtTail();
  50.     while( iterator )
  51.         {
  52.         cout << *iterator.Current() << endl;
  53.         iterator--;
  54.         }
  55. }
  56.  
  57. int main()
  58. {
  59.     ContainerType container;
  60.     AddItems(container, MaxItems);
  61.   
  62.     cout << "--- Starting ForEach" << endl;
  63.     container.ForEach(ForEachCallBack, (void*)"FE ");
  64.   
  65.     cout << "--- Starting Iterator (forward)" << endl;
  66.     UseForwardIterator(container);
  67.   
  68.     MyClass test("3 hello");
  69.     cout << "--- Deleting '" << test << "'" << endl;
  70.     container.Detach(&test);  // detach and delete
  71.     cout << endl;
  72.   
  73.     cout << "--- Starting Iterator (backward)" << endl;
  74.     UseBackwardIterator(container);
  75.   
  76.     return 0;
  77. }
  78.  
  79.