home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 May / Chip_2002-05_cd1.bin / chplus / cpp / 3 / stl.exe / alg5.cpp < prev    next >
C/C++ Source or Header  |  1998-02-09  |  7KB  |  222 lines

  1. #include "stlexam.h"
  2. #pragma hdrstop
  3. /**************************************************************************
  4.  *
  5.  * alg5.cpp - Example programs for STL generic algorithms those producing 
  6.  *    scalar values. Section 12.6
  7.  *
  8.  * $Id: alg5.cpp,v 1.17 1996/08/28 01:18:51 smithey Exp $
  9.  *
  10.  ***************************************************************************
  11.  *
  12.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  13.  * ALL RIGHTS RESERVED *
  14.  * The software and information contained herein are proprietary to, and
  15.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  16.  * intends to preserve as trade secrets such software and information.
  17.  * This software is furnished pursuant to a written license agreement and
  18.  * may be used, copied, transmitted, and stored only in accordance with
  19.  * the terms of such license and with the inclusion of the above copyright
  20.  * notice.  This software and information or any other copies thereof may
  21.  * not be provided or otherwise made available to any other person.
  22.  *
  23.  * Notwithstanding any other lease or license that may pertain to, or
  24.  * accompany the delivery of, this computer software and information, the
  25.  * rights of the Government regarding its use, reproduction and disclosure
  26.  * are as set forth in Section 52.227-19 of the FARS Computer
  27.  * Software-Restricted Rights clause.
  28.  * 
  29.  * Use, duplication, or disclosure by the Government is subject to
  30.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  31.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  32.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  33.  * P.O. Box 2328, Corvallis, Oregon 97339.
  34.  *
  35.  * This computer software and information is distributed with "restricted
  36.  * rights."  Use, duplication or disclosure is subject to restrictions as
  37.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  38.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  39.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  40.  * then the "Alternate III" clause applies.
  41.  *
  42.  **************************************************************************/
  43.  
  44. #ifndef _RWSTD_HEADER_REQUIRES_HPP
  45. #include <vector>
  46. #include <list>
  47. #include <algorithm>
  48. #include <numeric>
  49. #include <string.h>
  50. #include <string>
  51. #else
  52. #include <vector.hpp>
  53. #include <list.hpp>
  54. #include <algorithm.hpp>
  55. #include <numeric.hpp>
  56. #include <string.h>
  57. #include <string.hpp>
  58. #endif
  59.  
  60. #ifdef _RW_STD_IOSTREAM
  61. #include <iostream>
  62. #else
  63. #include <iostream.h>
  64. #endif
  65.    
  66. #ifndef _RWSTD_NO_NAMESPACE
  67.   using namespace std;
  68. #endif
  69.  
  70. //
  71. // Forward declarations.
  72. //
  73. bool isVowel (char);
  74. void count_example();
  75. void accumulate_example();
  76. template<class T>
  77. list<T,allocator<void> > & listadd(list<T,allocator<void> > & base, T & newValue);
  78. void inner_product_example();
  79. void equal_example();
  80.  
  81. bool isVowel (char c)
  82. {
  83.     switch (c)
  84.     {
  85.         case 'a': case 'A':
  86.         case 'e': case 'E':
  87.         case 'i': case 'I':
  88.         case 'o': case 'O':
  89.         case 'u': case 'U':
  90.             return true;
  91.     }
  92.     return false;
  93. }
  94.  
  95. //
  96. // Illustrate the use of the count function.
  97. //
  98.  
  99. void count_example ()
  100.  
  101. {
  102.     int ecount     = 0;
  103.     int vowelCount = 0;
  104.     
  105.     char * text = "Now is the time to begin";
  106.     
  107.     count (text, text + strlen(text), 'e', ecount);
  108.     count_if (text, text + strlen(text), isVowel, vowelCount);
  109.     
  110.     cout << "There are " << ecount << " letter e's " << endl << "and "
  111.         << vowelCount << " vowels in the text:" << text << endl;
  112. }
  113.  
  114. //
  115. // Add n to 1 to list.
  116. //
  117. #ifndef HPPA_WA 
  118. list<int,allocator<int> >& intReplicate (list<int,allocator<int> >& nums, int n)
  119. #else
  120. list<int,allocator<int> > intReplicate (list<int,allocator<int> > nums, int n)
  121. #endif       
  122. {
  123.     while (n) nums.push_back(n--);
  124.     return nums;
  125. }
  126.  
  127. //
  128. // Illustrate the use of the accumulate function.
  129. //
  130.  
  131. void accumulate_example ()
  132. {
  133.     int numbers[] = { 1, 2, 3, 4, 5 };
  134.     
  135.     int sum     = accumulate(numbers, numbers+5, 0);
  136.     int product = accumulate(numbers, numbers+5, 1, multiplies<int>());
  137.  
  138.     cout << "The sum of the first five numbers is "     << sum     << endl;
  139.     cout << "The product of the first five numbers is " << product << endl;
  140.     //
  141.     // Example with different types for init.
  142.     //
  143.     list<int,allocator<int> > nums;
  144.     nums = accumulate(numbers, numbers+5, nums, intReplicate);
  145.     copy (nums.begin(), nums.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  146.     cout << endl;
  147. }
  148.  
  149. //
  150. // Illustrate the use of the inner_product function.
  151. //
  152.  
  153. void inner_product_example ()
  154. {
  155.     int a[] = { 4, 3, -2 };
  156.     int b[] = { 7, 3, 2  };
  157.     //
  158.     // Example 1, simple inner product.
  159.     //
  160.     int in1 = inner_product(a, a+3, b, 0);
  161.     cout << "Inner product is " << in1 << endl;
  162.     //
  163.     // Example 2, using different operations.
  164.     //
  165.     bool anyequal = inner_product(a, a+3, b, true, logical_or<bool>(),
  166.                                   equal_to<int>());
  167.     cout << "any equal? " << anyequal << endl;  
  168. }
  169.  
  170. //
  171. // Illustrate the use of the equal function.
  172. //
  173.  
  174. void equal_example ()
  175. {
  176.     int a[] = { 4, 5, 3 };
  177.     int b[] = { 4, 3, 3 };
  178.     int c[] = { 4, 5, 3 };
  179.     
  180.     cout << "a = b is:" << equal(a, a+3, b) << endl;
  181.     cout << "a = c is:" << equal(a, a+3, c) << endl;
  182.     cout << "a pair-wise-greater_equal b is"
  183.          << equal(a, a+3, b, greater_equal<int>()) << endl;
  184. }
  185.  
  186. //
  187. // Illustrate the use of the lexical_comparison function.
  188. //
  189.  
  190. void lexical_comparison_example ()
  191. {
  192.     char * wordOne = "everything";
  193.     char * wordTwo = "everybody";
  194.     
  195.     cout << "compare everybody to everything "
  196.          << lexicographical_compare(wordTwo, wordTwo+strlen(wordTwo), wordOne,
  197.                                     wordOne+strlen(wordOne))
  198.          << endl;
  199.             
  200.     int a[] = { 3, 4, 5, 2 };
  201.     int b[] = { 3, 4, 5 };
  202.     int c[] = { 3, 5 };
  203.     
  204.     cout << "compare a to b: " << lexicographical_compare(a,a+4,b,b+3) << endl;
  205.     cout << "compare a to c: " << lexicographical_compare(a,a+4,c,c+2) << endl;
  206. }
  207.  
  208. int main ()
  209.  {
  210.     cout << "STL generic algorithms -- algorithms that produce scalar results" << endl;
  211.  
  212.     count_example();
  213.     accumulate_example();
  214.     inner_product_example();
  215.     equal_example();
  216.     lexical_comparison_example();
  217.     
  218.     cout << "End of scalar algorithms test"  << endl;
  219.  
  220.     return 0;
  221. }
  222.