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

  1. #include "stlexam.h"
  2. #pragma hdrstop
  3. /**************************************************************************
  4.  *
  5.  * alg6.cpp - STL generic algorithms that produce new sequences
  6.  *    section 12.7
  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.  
  48. #ifdef _RW_STD_IOSTREAM
  49. #include <iostream>
  50. #else
  51. #include <iostream.h>
  52. #endif
  53.     
  54. #ifndef _RWSTD_NO_NAMESPACE
  55. using namespace std;
  56. #endif
  57.  
  58. int square (int n) { return n * n; }
  59.  
  60. class iotaGen
  61. {
  62.   public:
  63.     iotaGen (int iv) : current(iv) { }
  64.     int operator () () { return current++; }
  65.   private:
  66.     int current;
  67. };
  68.  
  69. //
  70. // Illustrate the use of the transform algorithm.
  71. //
  72.  
  73. void transform_example ()
  74.  
  75. {
  76.     //
  77.     // Generate a list of values from 1 to 6.
  78.     //
  79.     list<int,allocator<int> > aList;
  80.     generate_n (inserter(aList, aList.begin()), 6, iotaGen(1));
  81.     cout << "Original list: ";
  82.     copy(aList.begin(), aList.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  83.     cout << endl;
  84.     //
  85.     // Transform elements by squaring, copy into vector.
  86.     //
  87.     vector<int,allocator<int> > aVec(6);
  88.     transform (aList.begin(), aList.end(), aVec.begin(), square);
  89.     cout << "After squaring: ";
  90.     copy(aVec.begin(), aVec.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  91.     cout << endl;
  92.     //
  93.     // Transform vector again, in place, yielding 4th powers.
  94.     //
  95.     transform (aVec.begin(), aVec.end(), aVec.begin(), square);
  96.     cout << "After squaring again: ";
  97.     copy(aVec.begin(), aVec.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  98.     cout << endl;
  99.     //
  100.     // Transform in parallel, yielding cubes.
  101.     //
  102.     vector<int,allocator<int> > cubes(6);
  103.     transform (aVec.begin(), aVec.end(), aList.begin(), cubes.begin(),
  104.                divides<int>());
  105.     cout << "After division: ";
  106.     copy(cubes.begin(), cubes.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  107.     cout << endl;
  108. }
  109.  
  110. //
  111. // Illustrate the use of the partial sum algorithm.
  112. //
  113.  
  114. void partial_sum_example ()
  115. {
  116.     //
  117.     // Generate values 1 to 5.
  118.     //
  119.     vector<int,allocator<int> > aVec(5);
  120.     generate (aVec.begin(), aVec.end(), iotaGen(1));
  121.     //
  122.     // Output partial sums.
  123.     //
  124.     cout << "Partial sums examples" << endl;
  125.     cout << "Partial sums : ";
  126.     partial_sum (aVec.begin(), aVec.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  127.     cout << endl;
  128.     //    
  129.     // Output partial products.
  130.     //
  131.     cout << "Partial products: ";
  132.     partial_sum (aVec.begin(), aVec.end(), 
  133.                  ostream_iterator<int,char,char_traits<char> >(cout, " "),
  134.                  multiplies<int>() );
  135.  
  136.     cout << endl;
  137. }
  138.  
  139. //
  140. // Illustrate the use of the adjacent difference algorithm.
  141. //
  142.  
  143. void adjacent_difference_example ()
  144. {
  145.     //
  146.     // Generate values 1 to 5.
  147.     //
  148.     vector<int,allocator<int> > aVec(5);
  149.     generate (aVec.begin(), aVec.end(), iotaGen(1));
  150.     //
  151.     // Output partial sums.
  152.     //
  153.     cout << "Adjacent Differences examples" << endl;
  154.     cout << "Adjacent Differences : ";
  155.     adjacent_difference (aVec.begin(), aVec.end(),
  156.                          ostream_iterator<int,char,char_traits<char> >(cout, " "));
  157.     cout << endl;
  158.     //
  159.     // Output partial products.
  160.     //
  161.     cout << "Adjacent sums: ";
  162.     adjacent_difference (aVec.begin(), aVec.end(),
  163.                          ostream_iterator<int,char,char_traits<char> >(cout, " "), plus<int>());
  164.     cout << endl;
  165. }
  166.  
  167.  
  168. int main ()
  169.  {
  170.     cout << "STL generic algorithms -- that transform sequences"  << endl;
  171.     
  172.     transform_example();
  173.     partial_sum_example();
  174.     adjacent_difference_example ();
  175.     
  176.     cout << "End generic transform algorithms example" << endl;
  177.  
  178.     return 0;
  179. }
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.