home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 May / PCFMay2001.iso / Xenon / C++ / FreeCommandLineTools.exe / Examples / StdLib / alg2.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-31  |  7.1 KB  |  250 lines

  1. #include "stlexam.h"
  2. #pragma hdrstop
  3. /**************************************************************************
  4.  *
  5.  * alg2.cpp - test program for STL generic algorithm that search for 
  6.  *    elements that satisfy a condition.  Section 12.3
  7.  *
  8.  ***************************************************************************
  9.  *
  10.  * (c) Copyright 1994, 1998 Rogue Wave Software, Inc.
  11.  * ALL RIGHTS RESERVED
  12.  *
  13.  * The software and information contained herein are proprietary to, and
  14.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  15.  * intends to preserve as trade secrets such software and information.
  16.  * This software is furnished pursuant to a written license agreement and
  17.  * may be used, copied, transmitted, and stored only in accordance with
  18.  * the terms of such license and with the inclusion of the above copyright
  19.  * notice.  This software and information or any other copies thereof may
  20.  * not be provided or otherwise made available to any other person.
  21.  *
  22.  * Notwithstanding any other lease or license that may pertain to, or
  23.  * accompany the delivery of, this computer software and information, the
  24.  * rights of the Government regarding its use, reproduction and disclosure
  25.  * are as set forth in Section 52.227-19 of the FARS Computer
  26.  * Software-Restricted Rights clause.
  27.  * 
  28.  * Use, duplication, or disclosure by the Government is subject to
  29.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  30.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  31.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  32.  * P.O. Box 2328, Corvallis, Oregon 97339.
  33.  *
  34.  * This computer software and information is distributed with "restricted
  35.  * rights."  Use, duplication or disclosure is subject to restrictions as
  36.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  37.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  38.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  39.  * then the "Alternate III" clause applies.
  40.  *
  41.  **************************************************************************/
  42.  
  43. #include <vector>
  44. #include <list>
  45. #include <algorithm>
  46. #include <ctype.h>
  47. #include <string>
  48. #include <string.h>
  49.  
  50. #ifdef _RW_STD_IOSTREAM
  51. #include <iostream>
  52. #else
  53. #include <iostream.h>
  54. #endif
  55.  
  56. #ifndef _RWSTD_NO_NAMESPACE
  57. using namespace std;
  58. #endif
  59.  
  60. int randomInteger (int m) {   return rand() % m; }
  61.  
  62. bool isLeapYear (unsigned int year)
  63. {
  64.     if (year % 1000 == 0)
  65.         return true;
  66.     if (year % 100 == 0)
  67.         return false;
  68.     if (year % 4 == 0)
  69.         return true;
  70.     return false;
  71. }
  72.  
  73.  
  74. void split (const string & text, const string & separators,
  75.             list<string,allocator<string> > & words)
  76. {
  77.     int n     = text.length();
  78.     int start = text.find_first_not_of(separators);
  79.  
  80.     while ((start >= 0) && (start < n))
  81.     {
  82.         int stop = text.find_first_of(separators, start);
  83.         if ((stop < 0) || (stop > n)) stop = n;
  84.         words.push_back (text.substr(start, stop-start));
  85.         start = text.find_first_not_of(separators, stop+1);
  86.     }
  87. }
  88.  
  89. //
  90. // Illustrate use of the find function.
  91. //
  92.  
  93. void find_test ()
  94. {
  95.     cout << "Test of algorithm find" << endl;
  96.  
  97.     int vintageYears[] = { 1967, 1972, 1974, 1980, 1995 };
  98.  
  99.     vector<int,allocator<int> >::iterator start = vintageYears;
  100.     vector<int,allocator<int> >::iterator stop  = vintageYears + 5;
  101.     
  102.     vector<int,allocator<int> >::iterator where = find_if(start, stop, isLeapYear);
  103.     
  104.     if (where != stop)
  105.         cout << "first vintage leap year is " << *where << endl;
  106.     else
  107.         cout << "no vintage leap years" << endl;
  108.         
  109.     where = find(start, stop, 1995);
  110.     
  111.     if (where != stop)
  112.         cout << "1995 is position " << where - start << " in sequence" << endl;
  113.     else
  114.         cout << "1995 does not occur in sequence" << endl;
  115. }
  116.  
  117. void find_adjacent_test ()
  118. {
  119.     cout << "Test of algorithm find adjacent" << endl;
  120.  
  121.     char * text = "The bookkeeper carefully opened the door";
  122.     
  123.     vector<char,allocator<char> >::iterator start = text;
  124.     vector<char,allocator<char> >::iterator stop = text + strlen(text);
  125.     
  126.     vector<char,allocator<char> >::iterator where = start;
  127.     
  128.     cout << "In the text " << text << endl;
  129.  
  130.     while ((where = adjacent_find(where, stop)) != stop)
  131.     {
  132.         cout << "double " << *where << " in position " << where-start << endl;
  133.         ++where;
  134.     }
  135. }
  136.  
  137. //
  138. // Illustrate the use of the search function.
  139. //
  140.  
  141. void search_test ()
  142. {
  143.     cout << "Test of algorithm search" << endl;
  144.  
  145.     char * base = "aspirations";
  146.     char * text = "ration";
  147.     
  148.     char * where = search(base, base+strlen(base), text, text+strlen(text));
  149.     
  150.     if (*where != '\0')
  151.         cout << "substring begins in position " << where - base << endl;
  152.     else
  153.         cout << "substring does not occur in text" << endl;
  154. }
  155.  
  156. //
  157. // Illustrate use of max_element and min_element algorithms.
  158. //
  159.  
  160. void max_min_example ()
  161. {
  162.     cout << "Test of max and min algorithms " << endl;
  163.     //
  164.     // Make a vector of random numbers between 0 and 99.
  165.     //
  166.     vector<int,allocator<int> > numbers(25);
  167.     for (int i = 0; i < 25; i++)
  168.         numbers[i] = randomInteger(100);
  169.     //
  170.     // Print the maximum.
  171.     //
  172.     vector<int,allocator<int> >::iterator max = max_element(numbers.begin(), numbers.end());
  173.     cout << "largest value was " << *max << endl;
  174.     //
  175.     // Example using strings.
  176.     //
  177.     string text = "it was the best of times, it was the worst of times.";
  178.     list<string,allocator<string> >words;
  179.     split(text, " .,!:;", words);
  180.     cout << "The smallest word is "
  181.         << * min_element(words.begin(), words.end())
  182.         << " and the largest word is "
  183.         << * max_element(words.begin(), words.end())
  184.         << endl;
  185. }
  186.  
  187. //
  188. // Illustrate the use of the mismatch function.
  189. //
  190.  
  191. void mismatch_test (char * a, char * b) 
  192. {
  193.     pair<char *, char *> differPositions(0, 0);
  194.     char * aDiffPos;
  195.     char * bDiffPos;
  196.  
  197.     if (strlen(a) < strlen(b))
  198.     {
  199.         differPositions = mismatch(a, a + strlen(a), b);
  200.         aDiffPos = differPositions.first;
  201.         bDiffPos = differPositions.second;
  202.     }
  203.     else
  204.     {
  205.         differPositions = mismatch(b, b + strlen(b), a);
  206.         aDiffPos = differPositions.second;
  207.         bDiffPos = differPositions.first;
  208.     }
  209.         
  210.     cout << "string " << a;
  211.  
  212.     if (*aDiffPos == *bDiffPos)
  213.         cout << " is equal to ";
  214.     else if (*aDiffPos < *bDiffPos)
  215.         cout << " is less than ";
  216.     else
  217.         cout << " is greater than ";
  218.  
  219.     cout << b << endl;
  220. }
  221.  
  222. int main ()
  223. {
  224.     cout << "STL generic algorithms -- Searching Algorithms" << endl;
  225.  
  226.     find_test();
  227.     find_adjacent_test();
  228.     search_test();
  229.     max_min_example();
  230.     mismatch_test("goody", "goody");
  231.     mismatch_test("good", "goody");
  232.     mismatch_test("goody", "good");
  233.     mismatch_test("good", "fred");
  234.     mismatch_test("fred", "good");
  235.     
  236.     cout << "End of search algorithms test program" << endl;
  237.  
  238.     return 0;
  239. }
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.