home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
cset21v1.zip
/
IBMCPP
/
SAMPLES
/
ICLCC
/
WORDBAG.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-05-07
|
3KB
|
73 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. */
/* */
/******************************************************************************/
/*-------------------------------------------------------------*\
| wordbag.C - Word Bag, example for the use of the |
| Key Sorted Bag to perform statistics on words. |
| """""""""""""" |
| The elements handled in the collections are the words of a |
| phrase. The key of each word is the number of its letters. |
| The Key Sorted Bag will store all occurences of each key. |
| We use this to calculate the number of n-letter words in |
| the phrase. |
| |
\*-------------------------------------------------------------*/
#include <iostream.h>
// Class Word:
#include "toyword.h"
// Let's use the defaults:
#include <iksbag.h>
typedef IKeySortedBag <Word, int> WordBag;
int main() {
Word phrase[] = {"people", "who", "live", "in", "glass",
"houses", "should", "not", "throw", "stones"};
const int phraseWords = sizeof(phrase) / sizeof(Word);
WordBag wordbag(phraseWords);
for (int cnt=0; cnt < phraseWords; cnt++) {
wordbag.add(phrase[cnt]);
}
cout << "Contents of our WordBag sorted by number of letters:\n";
WordBag::Cursor wordBagCursor(wordbag);
forCursor(wordBagCursor)
cout << "WB: " << wordBagCursor.element().text() << "\n";
cout << "\nOur phrase has " << phraseWords << " words.\n";
cout << "In our WordBag are " << wordbag.numberOfElements()
<< " words.\n\n";
cout << "There are " << wordbag.numberOfDifferentKeys()
<< " different word lengths.\n\n";
wordBagCursor.setToFirst();
do {
int letters = wordbag.key(wordBagCursor.element());
cout << "There are "
<< wordbag.numberOfElementsWithKey(letters)
<< " words with " << letters << " letters.\n";
} while (wordbag.setToNextWithDifferentKey(wordBagCursor));
return 0;
}