home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 May / PCFMay2001.iso / Xenon / C++ / FreeCommandLineTools.exe / Examples / StdLib / alg5.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-31  |  6.0 KB  |  208 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.  ***************************************************************************
  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 <numeric>
  47. #include <string.h>
  48. #include <string>
  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. //
  61. // Forward declarations.
  62. //
  63. bool isVowel (char);
  64. void count_example();
  65. void accumulate_example();
  66. template<class T>
  67. list<T,allocator<void> > & listadd(list<T,allocator<void> > & base, T & newValue);
  68. void inner_product_example();
  69. void equal_example();
  70.  
  71. bool isVowel (char c)
  72. {
  73.     switch (c)
  74.     {
  75.         case 'a': case 'A':
  76.         case 'e': case 'E':
  77.         case 'i': case 'I':
  78.         case 'o': case 'O':
  79.         case 'u': case 'U':
  80.             return true;
  81.     }
  82.     return false;
  83. }
  84.  
  85. //
  86. // Illustrate the use of the count function.
  87. //
  88.  
  89. void count_example ()
  90.  
  91. {
  92.     int ecount     = 0;
  93.     int vowelCount = 0;
  94.     
  95.     char * text = "Now is the time to begin";
  96.     
  97.     count (text, text + strlen(text), 'e', ecount);
  98.     count_if (text, text + strlen(text), isVowel, vowelCount);
  99.     
  100.     cout << "There are " << ecount << " letter e's " << endl << "and "
  101.         << vowelCount << " vowels in the text:" << text << endl;
  102. }
  103.  
  104. //
  105. // Add n to 1 to list.
  106. //
  107. list<int,allocator<int> >& intReplicate (list<int,allocator<int> >& nums, int n)
  108. {
  109.     while (n) nums.push_back(n--);
  110.     return nums;
  111. }
  112.  
  113. //
  114. // Illustrate the use of the accumulate function.
  115. //
  116.  
  117. void accumulate_example ()
  118. {
  119.     int numbers[] = { 1, 2, 3, 4, 5 };
  120.     
  121.     int sum     = accumulate(numbers, numbers+5, 0);
  122.     int product = accumulate(numbers, numbers+5, 1, multiplies<int>());
  123.  
  124.     cout << "The sum of the first five numbers is "     << sum     << endl;
  125.     cout << "The product of the first five numbers is " << product << endl;
  126.     //
  127.     // Example with different types for init.
  128.     //
  129.     list<int,allocator<int> > nums;
  130.     nums = accumulate(numbers, numbers+5, nums, intReplicate);
  131.     copy (nums.begin(), nums.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  132.     cout << endl;
  133. }
  134.  
  135. //
  136. // Illustrate the use of the inner_product function.
  137. //
  138.  
  139. void inner_product_example ()
  140. {
  141.     int a[] = { 4, 3, -2 };
  142.     int b[] = { 7, 3, 2  };
  143.     //
  144.     // Example 1, simple inner product.
  145.     //
  146.     int in1 = inner_product(a, a+3, b, 0);
  147.     cout << "Inner product is " << in1 << endl;
  148.     //
  149.     // Example 2, using different operations.
  150.     //
  151.     bool anyequal = inner_product(a, a+3, b, true, logical_or<bool>(),
  152.                                   equal_to<int>());
  153.     cout << "any equal? " << anyequal << endl;  
  154. }
  155.  
  156. //
  157. // Illustrate the use of the equal function.
  158. //
  159.  
  160. void equal_example ()
  161. {
  162.     int a[] = { 4, 5, 3 };
  163.     int b[] = { 4, 3, 3 };
  164.     int c[] = { 4, 5, 3 };
  165.     
  166.     cout << "a = b is:" << equal(a, a+3, b) << endl;
  167.     cout << "a = c is:" << equal(a, a+3, c) << endl;
  168.     cout << "a pair-wise-greater_equal b is"
  169.          << equal(a, a+3, b, greater_equal<int>()) << endl;
  170. }
  171.  
  172. //
  173. // Illustrate the use of the lexical_comparison function.
  174. //
  175.  
  176. void lexical_comparison_example ()
  177. {
  178.     char * wordOne = "everything";
  179.     char * wordTwo = "everybody";
  180.     
  181.     cout << "compare everybody to everything "
  182.          << lexicographical_compare(wordTwo, wordTwo+strlen(wordTwo), wordOne,
  183.                                     wordOne+strlen(wordOne))
  184.          << endl;
  185.             
  186.     int a[] = { 3, 4, 5, 2 };
  187.     int b[] = { 3, 4, 5 };
  188.     int c[] = { 3, 5 };
  189.     
  190.     cout << "compare a to b: " << lexicographical_compare(a,a+4,b,b+3) << endl;
  191.     cout << "compare a to c: " << lexicographical_compare(a,a+4,c,c+2) << endl;
  192. }
  193.  
  194. int main ()
  195.  {
  196.     cout << "STL generic algorithms -- algorithms that produce scalar results" << endl;
  197.  
  198.     count_example();
  199.     accumulate_example();
  200.     inner_product_example();
  201.     equal_example();
  202.     lexical_comparison_example();
  203.     
  204.     cout << "End of scalar algorithms test"  << endl;
  205.  
  206.     return 0;
  207. }
  208.