home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / VSCPPv8.zip / VACPP / IBMCPP / samples / IOC / ANIMALS / ANIMALS.CPP < prev    next >
Text File  |  1995-03-15  |  4KB  |  109 lines

  1. /*************************************************************************
  2.   IBM C/C++ Tools Version 3.00 - Collection Class Library
  3.  (C) Copyright IBM Corporation 1992 ,1995, Licensed Program-Property of
  4.  IBM.  All Rights Reserved.  US Government Users Restricted Rights - Use,
  5.  duplication or disclosure restricted by GSA ADP Schedule Contract with
  6.  IBM Corp.
  7.  *************************************************************************/
  8.  
  9. /*------------------------------------------------------------*\
  10. | animals.CPP  -  Example for the use of the Key Bag           |
  11. |                                            """""""           |
  12. | We keep a Key Bag of our observations on animals.  Elements  |
  13. | handled in this Key Bag are of type animal, the key is the   |
  14. | name of the animal.                                          |
  15. | This Key Bag allows us to efficiently access all observations|
  16. | on an animal.                                                |
  17. | We use a Sequence to store the names of all extinct animals. |
  18. | At last we remove all extinct animals from the Key Bag.      |
  19. \*------------------------------------------------------------*/
  20.  
  21. #include <iostream.h>
  22.                // Class Animal:
  23. #include "animal.h"
  24.  
  25.                // Let's use the default Key Bag:
  26. #include <ikeybag.h>
  27. typedef IKeyBag<Animal, IString> Animals;
  28.  
  29.                // For keys let's use the default Sequence:
  30. #include <iseq.h>
  31. typedef ISequence<IString> Names;
  32.  
  33.  
  34. main() {
  35.  
  36.    Animals animals;
  37.    Animals::Cursor animalsCur1(animals), animalsCur2(animals);
  38.  
  39.    animals.add(Animal("bear", "heavy"));
  40.    animals.add(Animal("bear", "strong"));
  41.    animals.add(Animal("dinosaur", "heavy"));
  42.    animals.add(Animal("dinosaur", "huge"));
  43.    animals.add(Animal("dinosaur", "extinct"));
  44.    animals.add(Animal("eagle", "black"));
  45.    animals.add(Animal("eagle", "strong"));
  46.    animals.add(Animal("lion", "dangerous"));
  47.    animals.add(Animal("lion", "strong"));
  48.    animals.add(Animal("mammoth", "long haired"));
  49.    animals.add(Animal("mammoth", "extinct"));
  50.    animals.add(Animal("sabre tooth tiger", "extinct"));
  51.    animals.add(Animal("zebra", "striped"));
  52.  
  53.                  // Display all elements in animals:
  54.    cout << endl
  55.         << "All our observations on animals:" << endl;
  56.    forCursor(animalsCur1)  cout << "    " << animalsCur1.element();
  57.  
  58.    cout << endl << endl
  59.         << "Number of observations on animals: "
  60.         << animals.numberOfElements() << endl;
  61.  
  62.    cout << endl
  63.         << "Number of different animals: "
  64.         << animals.numberOfDifferentKeys() << endl;
  65.  
  66.    Names namesOfExtinct(animals.numberOfDifferentKeys());
  67.    Names::Cursor extinctCur1(namesOfExtinct);
  68.  
  69.    animalsCur1.setToFirst();
  70.    do {
  71.       IString name = animalsCur1.element().name();
  72.  
  73.       cout << endl
  74.            << "We have " << animals.numberOfElementsWithKey(name)
  75.            << " observations on " << name << ":" << endl;
  76.  
  77.                    // We need to use a separate cursor here
  78.                    // because otherwise animalsCur1 would become
  79.                    // invalid after last locateNextElement...()
  80.       animals.locateElementWithKey(name, animalsCur2);
  81.       do  {
  82.          IString attribute = animalsCur2.element().attribute();
  83.          cout << "    " << attribute << endl;
  84.          if (attribute == "extinct") namesOfExtinct.add(name);
  85.       } while (animals.locateNextElementWithKey(name, animalsCur2));
  86.  
  87.    } while (animals.setToNextWithDifferentKey(animalsCur1));
  88.  
  89.                  // Remove all observations on extinct animals:
  90.    forCursor(extinctCur1)
  91.       animals.removeAllElementsWithKey(extinctCur1.element());
  92.  
  93.                  // Display all elements in animals:
  94.    cout << endl << endl
  95.         << "After removing all observations on extinct animals:"
  96.         << endl;
  97.    forCursor(animalsCur1)  cout << "    " << animalsCur1.element();
  98.  
  99.    cout << endl
  100.         << "Number of observations on animals: "
  101.         << animals.numberOfElements() << endl;
  102.  
  103.    cout << endl
  104.         << "Number of different animals: "
  105.         << animals.numberOfDifferentKeys() << endl;
  106.  
  107.    return 0;
  108. }
  109.