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 >
C/C++ Source or Header  |  1993-05-07  |  5KB  |  121 lines

  1. /******************************************************************************/
  2. /*                                                                            */
  3. /* COPYRIGHT:                                                                 */
  4. /* ----------                                                                 */
  5. /* Copyright (C) International Business Machines Corp., 1991,1992.            */
  6. /*                                                                            */
  7. /* DISCLAIMER OF WARRANTIES:                                                  */
  8. /* -------------------------                                                  */
  9. /* The following [enclosed] code is sample code created by IBM                */
  10. /* Corporation.  This sample code is not part of any standard IBM product     */
  11. /* and is provided to you solely for the purpose of assisting you in the      */
  12. /* development of your applications.  The code is provided "AS IS",           */
  13. /* without warranty of any kind.  IBM shall not be liable for any damages     */
  14. /* arising out of your use of the sample code, even if they have been         */
  15. /* advised of the possibility of such damages.                                */
  16. /*                                                                            */
  17. /******************************************************************************/
  18. /*-------------------------------------------------------------*\
  19. |  wordseq.C  -  Example for using the Sequence.                |
  20. |                                      """"""""                 |
  21. |  This Sequence is used to handle elements of type Word.       |
  22. |                                                               |
  23. |  This example also demonstrates two different ways of         |
  24. |  iteration, using an object of an iterator class              |
  25. |  and using a cursor.                                          |
  26. \*-------------------------------------------------------------*/
  27.  
  28. #include <iostream.h>
  29.  
  30.               // Get definition and declaration of class Word:
  31. #include "toyword.h"
  32.  
  33.               // Define a compare function to be used for sort:
  34. inline long wordCompare ( Word const& w1, Word const& w2) {
  35.    return compare (w1.text(), w2.text());
  36. }
  37.  
  38. /*-------------------------------------------------------------*\
  39. |  We want to use the default Sequence called ISequence.        |
  40. \*-------------------------------------------------------------*/
  41. #include <iseq.h>
  42.  
  43. typedef ISequence <Word> WordSeq;
  44. typedef IIterator <Word> WordIter;
  45.  
  46.  
  47. /*-------------------------------------------------------------*\
  48. | Test variables to put into the Sequence.                      |
  49. \*-------------------------------------------------------------*/
  50.  
  51. char *String[9] = {
  52.    "the",    "quick",   "brown",   "fox",   "jumps",
  53.    "over",   "a",       "lazy",    "dog"
  54. };
  55.  
  56.  
  57.  
  58. /*-------------------------------------------------------------*\
  59. |  Our Iterator class for use with allElementsDo().             |
  60. |                                                               |
  61. |  The alternative method of iteration, using a cursor, does    |
  62. |  not need such an iterator class.   If you want to see        |
  63. |  this alternative, search for occurences of cursor below.     |
  64. |                                             """"""            |
  65. \*-------------------------------------------------------------*/
  66. class PrintClass : public WordIter
  67. {
  68. public:
  69.    Boolean applyTo(Word &w)
  70.       {
  71.       cout << "\n" << w.text();    // Print the string
  72.       return(True);
  73.       }
  74. };
  75.  
  76.  
  77.  
  78.  
  79. /*-------------------------------------------------------------*\
  80. | Main program                                                  |
  81. \*-------------------------------------------------------------*/
  82. int main()  {
  83.    WordSeq WL;
  84.    WordSeq::Cursor cursor(WL);
  85.    PrintClass Print;
  86.  
  87.    int i;
  88.  
  89.    for (i = 0; i < 9; i ++) {  // Put all strings into Sequence
  90.       Word aWord(String[i]);   // Fill object with right value
  91.       WL.addAsLast(aWord);     // Add it to the Sequence at end
  92.    }
  93.  
  94.    cout << "\nSequence in initial order:\n";
  95.    WL.allElementsDo(Print);
  96.  
  97.    WL.sort(wordCompare);       // Sort the Sequence ascending
  98.    cout << "\n\nSequnce in sorted order:\n";
  99.    WL.allElementsDo(Print);
  100.  
  101.    // Use iteration via cursor now:
  102.  
  103.    cout << "\n\nLook for \"fox\" in the Sequence:\n";
  104.    for (cursor.setToFirst();
  105.         cursor.isValid() && (WL.elementAt(cursor) != "fox");
  106.         cursor.setToNext());
  107.  
  108.    if (WL.elementAt(cursor) != "fox") {
  109.        cout << "\n The element was not found.\n";
  110.    }
  111.    else {
  112.        cout << "\n The element was found.\n";
  113.    }
  114.  
  115.    cout << "\n\nThe element at position 9: "
  116.         <<  WL.elementAtPosition(9).text();
  117.  
  118.    return(0);
  119. }
  120.  
  121.