home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
cset21v1.zip
/
IBMCPP
/
SAMPLES
/
ICLCC
/
ANIMALS.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-05-07
|
5KB
|
110 lines
/******************************************************************************/
/* */
/* COPYRIGHT: */
/* ---------- */
/* Copyright (C) International Business Machines Corp., 1991,1992. */
/* */
/* DISCLAIMER OF WARRANTIES: */
/* ------------------------- */
/* The following [enclosed] code is sample code created by IBM */
/* Corporation. This sample code is not part of any standard IBM product */
/* and is provided to you solely for the purpose of assisting you in the */
/* development of your applications. The code is provided "AS IS", */
/* without warranty of any kind. IBM shall not be liable for any damages */
/* arising out of your use of the sample code, even if they have been */
/* advised of the possibility of such damages. */
/* */
/******************************************************************************/
/*------------------------------------------------------------*\
| animals.C - Example for the use of the Key Bag |
| """"""" |
| We keep a Key Bag of our observations on animals. Elements |
| handled in this Key Bag are of type animal, the key is the |
| name of the animal. |
| This Key Bag allows us to efficiently access all observations|
| on an animal. |
| We use a Sequence to store the names of all extinct animals. |
| At last we remove all extinct animals from the Key Bag. |
\*------------------------------------------------------------*/
#include <iostream.h>
// Class Animal:
#include "animal.h"
// Let's use the default Key Bag:
#include <ikeybag.h>
typedef IKeyBag<Animal, ToyString> Animals;
// For keys let's use the default Sequence:
#include <iseq.h>
typedef ISequence<ToyString> Names;
main() {
Animals animals;
Animals::Cursor animalsCur1(animals), animalsCur2(animals);
animals.add(Animal("bear", "heavy"));
animals.add(Animal("bear", "strong"));
animals.add(Animal("dinosaur", "heavy"));
animals.add(Animal("dinosaur", "huge"));
animals.add(Animal("dinosaur", "extinct"));
animals.add(Animal("eagle", "black"));
animals.add(Animal("eagle", "strong"));
animals.add(Animal("lion", "dangerous"));
animals.add(Animal("lion", "strong"));
animals.add(Animal("mammoth", "long haired"));
animals.add(Animal("mammoth", "extinct"));
animals.add(Animal("sabre tooth tiger", "extinct"));
animals.add(Animal("zebra", "striped"));
// Display all elements in animals:
cout << "\nAll our observations on animals:\n";
forCursor(animalsCur1) cout << " " << animalsCur1.element();
cout << "\n\nNumber of observations on animals: "
<< animals.numberOfElements() << "\n";
cout << "\nNumber of different animals: "
<< animals.numberOfDifferentKeys() << "\n";
Names namesOfExtinct(animals.numberOfDifferentKeys());
Names::Cursor extinctCur1(namesOfExtinct);
animalsCur1.setToFirst();
do {
ToyString name = animalsCur1.element().name();
cout << "\nWe have " << animals.numberOfElementsWithKey(name)
<< " observations on " << name.text() << ":\n";
// We need to use a separate cursor here
// because otherwise animalsCur1 would become
// invalid after last locateNextElement...()
animals.locateElementWithKey(name, animalsCur2);
do {
ToyString attribute = animalsCur2.element().attribute();
cout << " " << attribute << "\n";
if (attribute == "extinct") namesOfExtinct.add(name);
} while (animals.locateNextElementWithKey(name, animalsCur2));
} while (animals.setToNextWithDifferentKey(animalsCur1));
// Remove all observations on extinct animals:
forCursor(extinctCur1)
animals.removeAllElementsWithKey(extinctCur1.element());
// Display all elements in animals:
cout << "\n\nAfter removing all observations on extinct animals:\n";
forCursor(animalsCur1) cout << " " << animalsCur1.element();
cout << "\nNumber of observations on animals: "
<< animals.numberOfElements() << "\n";
cout << "\nNumber of different animals: "
<< animals.numberOfDifferentKeys() << "\n";
return 0;
}