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