home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
cset21v1.zip
/
IBMCPP
/
SAMPLES
/
ICLCC
/
WORDSEQ.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-05-07
|
5KB
|
121 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. */
/* */
/******************************************************************************/
/*-------------------------------------------------------------*\
| wordseq.C - Example for using the Sequence. |
| """""""" |
| This Sequence is used to handle elements of type Word. |
| |
| This example also demonstrates two different ways of |
| iteration, using an object of an iterator class |
| and using a cursor. |
\*-------------------------------------------------------------*/
#include <iostream.h>
// Get definition and declaration of class Word:
#include "toyword.h"
// Define a compare function to be used for sort:
inline long wordCompare ( Word const& w1, Word const& w2) {
return compare (w1.text(), w2.text());
}
/*-------------------------------------------------------------*\
| We want to use the default Sequence called ISequence. |
\*-------------------------------------------------------------*/
#include <iseq.h>
typedef ISequence <Word> WordSeq;
typedef IIterator <Word> WordIter;
/*-------------------------------------------------------------*\
| Test variables to put into the Sequence. |
\*-------------------------------------------------------------*/
char *String[9] = {
"the", "quick", "brown", "fox", "jumps",
"over", "a", "lazy", "dog"
};
/*-------------------------------------------------------------*\
| Our Iterator class for use with allElementsDo(). |
| |
| The alternative method of iteration, using a cursor, does |
| not need such an iterator class. If you want to see |
| this alternative, search for occurences of cursor below. |
| """""" |
\*-------------------------------------------------------------*/
class PrintClass : public WordIter
{
public:
Boolean applyTo(Word &w)
{
cout << "\n" << w.text(); // Print the string
return(True);
}
};
/*-------------------------------------------------------------*\
| Main program |
\*-------------------------------------------------------------*/
int main() {
WordSeq WL;
WordSeq::Cursor cursor(WL);
PrintClass Print;
int i;
for (i = 0; i < 9; i ++) { // Put all strings into Sequence
Word aWord(String[i]); // Fill object with right value
WL.addAsLast(aWord); // Add it to the Sequence at end
}
cout << "\nSequence in initial order:\n";
WL.allElementsDo(Print);
WL.sort(wordCompare); // Sort the Sequence ascending
cout << "\n\nSequnce in sorted order:\n";
WL.allElementsDo(Print);
// Use iteration via cursor now:
cout << "\n\nLook for \"fox\" in the Sequence:\n";
for (cursor.setToFirst();
cursor.isValid() && (WL.elementAt(cursor) != "fox");
cursor.setToNext());
if (WL.elementAt(cursor) != "fox") {
cout << "\n The element was not found.\n";
}
else {
cout << "\n The element was found.\n";
}
cout << "\n\nThe element at position 9: "
<< WL.elementAtPosition(9).text();
return(0);
}