home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional Developers Kit 1992 November / Disc01 / Disc01.mdf / cppbeta / bbxxsamk / xseq.c__ / XSEQ.C
Encoding:
C/C++ Source or Header  |  1992-10-26  |  1.9 KB  |  66 lines

  1. /* Copyright (c) IBM Corp. 1992 */
  2. /*--------------------------------------------------------------------------*\ 
  3. *                                                                            *
  4. | Example program of Building Block SEQUENCE.                               |
  5. *                                                                            *
  6. \*--------------------------------------------------------------------------*/
  7.  
  8. #include <iostream.h>
  9. #include <string.h>
  10. #include <iseq.h>
  11.  
  12. typedef ISequence <char> CharSeq;
  13.  
  14. typedef IIterator <char> CharIterator;
  15.  
  16. class PrintClass : public CharIterator
  17. {
  18. public:
  19.    Boolean applyTo(char &c)
  20.       {
  21.       cout << c;    // Print the character
  22.       return(True);
  23.       }
  24. };
  25.  
  26.  
  27. char * String = "BuildingBlocks";
  28.  
  29. /*--------------------------------------------------------------------------*\ 
  30. * Main program                                                               *
  31. \*--------------------------------------------------------------------------*/
  32. int main() {
  33.    CharSeq CS;
  34.    CharSeq::Cursor cursor(CS);
  35.  
  36.    PrintClass Print;
  37.    int i;
  38.  
  39.    cout << "\n*** Example of SEQUENCE use ***\n";
  40.  
  41.    for (i = 0; String[i] != 0; i++)          // Put all characters in the
  42.       {                                      // sequence.
  43.       CS.add(String[i]);
  44.       }
  45.  
  46.    cout << "\nSequence contains:\n";         // Print the contents
  47.    CS.allElementsDo(Print);
  48.    cout << "\nNumber of elements = " << CS.numberOfElements() << "\n";
  49.    
  50.    while (CS.numberOfElements () >= 5) {
  51.      CS.setToPosition(5, cursor);
  52.      CS.allElementsDo(Print);
  53.       cout << "\n";
  54.      CS.removeAt(cursor);
  55.    }
  56.  
  57.    cout << "\nSequence without the tail:\n"; // Print the contents again
  58.    CS.allElementsDo(Print);
  59.    cout << "\nNumber of elements = \n" << CS.numberOfElements();
  60.  
  61.    cout << "\n*** End of example ***\n";
  62.  
  63.    return(0);
  64. }
  65.  
  66.