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

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