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

  1. #include "stlexam.h"
  2. #pragma hdrstop
  3. /**************************************************************************
  4.  *
  5.  * alg1.cpp - Example program for STL generic algorithms that initialize 
  6.  *    sequences.  Section 12.2
  7.  *
  8.  * $Id: alg1.cpp,v 1.18 1996/08/28 01:18:43 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 <ctype.h>
  49. #include <string>
  50. #include <string.h>
  51. #else
  52. #include <vector.hpp>
  53. #include <list.hpp>
  54. #include <algorithm.hpp>
  55. #include <ctype.h>
  56. #include <string.hpp>
  57. #include <string.h>
  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. class iotaGen
  71. {
  72.   public:
  73.     iotaGen (int iv) : current(iv) { }
  74.     int operator () () { return current++; }
  75.   private:
  76.     int current;
  77. };
  78.  
  79. //
  80. // Illustrate the use of the fill and fill_n functions.
  81. //
  82.  
  83. void fill_example ()
  84. {
  85.     cout << "Illustrate fill function" << endl;
  86.     //
  87.     // Example 1, fill an array with initial values
  88.     //
  89.     char buffer[100], *bufferp = buffer;
  90. #ifndef _RWSTD_FILL_NAME_CLASH
  91.     fill (bufferp,(bufferp + 100),'\0');
  92. #else
  93.     std_fill (bufferp,(bufferp + 100),'\0');
  94. #endif
  95.     fill_n (bufferp, 10, 'x');
  96.     cout << buffer << endl;
  97.     //
  98.     // Example 2, use fill to initialize a list.
  99.     //
  100.     list<string,allocator<string> > aList;
  101.     fill_n (inserter(aList, aList.begin()), 10, "empty");
  102.     copy(aList.begin(), aList.end(), ostream_iterator<string,char,char_traits<char> >(cout, " "));
  103.     cout << endl;
  104.     //
  105.     // Example 3, use fill to overwrite values in a list.
  106.     //
  107. #ifndef _RWSTD_FILL_NAME_CLASH
  108.     fill (aList.begin(), aList.end(), "full");
  109. #else
  110.     std_fill (aList.begin(), aList.end(), "full");
  111. #endif
  112.     copy(aList.begin(), aList.end(), ostream_iterator<string,char,char_traits<char> >(cout, " "));
  113.     cout << endl;
  114.     //
  115.     // Example 4, fill in a portion of a list.
  116.     //
  117.     vector<int,allocator<int> > iVec(10);
  118.     generate (iVec.begin(), iVec.end(), iotaGen(1));
  119.     vector<int,allocator<int> >::iterator seven = find(iVec.begin(), iVec.end(), 7);
  120. #ifndef _RWSTD_FILL_NAME_CLASH
  121.     fill(iVec.begin(), seven, 0);
  122. #else
  123.     std_fill(iVec.begin(), seven, 0);
  124. #endif
  125.     copy(iVec.begin(), iVec.end(), ostream_iterator<int,char,char_traits<char> >(cout));
  126.     cout << endl;
  127. }
  128.  
  129. //
  130. // Illustrate the use of the copy function.
  131. //
  132.  
  133. void copy_example ()
  134. {
  135.     cout << "Illustrate copy function " << endl;
  136.     //
  137.     // Example 1, a simple copy.
  138.     //
  139.     char * source  = "reprise";
  140.     char * surpass = "surpass";
  141.     char buffer[120], *bufferp = buffer;
  142.     copy(source, source + strlen(source) + 1, bufferp);
  143.     //
  144.     // Example 2, self copies.
  145.     //
  146.     *copy(bufferp + 2, bufferp+ strlen(buffer), bufferp) = '\0';
  147.     int buflen = strlen(buffer) + 1;
  148.     copy_backward(bufferp, bufferp + buflen, bufferp + buflen + 3);
  149.     copy(surpass, surpass + 3, bufferp);
  150.     //
  151.     // Example 3, copy to output.
  152.     //
  153.     copy(bufferp, bufferp + strlen(buffer), ostream_iterator<char,char,char_traits<char> >(cout));
  154.     cout << endl;
  155.     //
  156.     // Example 4, use copy to convert type.
  157.     //
  158.     list<char,allocator<char> > char_list;
  159.     copy(bufferp, bufferp + strlen(buffer),
  160.          inserter(char_list,char_list.end()));
  161.     char * big = "big ";
  162.     copy(big, big + 4, inserter(char_list, char_list.begin()));
  163.     
  164.     char buffer2[200], *buffer2p = buffer2;
  165.     *copy(char_list.begin(), char_list.end(), buffer2p) = '\0';
  166.     cout << buffer2 << endl;
  167. }
  168.  
  169. #ifdef _RW_STD_IOSTREAM
  170. #include <strstream>
  171. #else //!_RWSTD_IOSTREAM
  172. #ifndef _RWSTD_NO_LONG_HEADER_NAME
  173. #include <strstream.h>
  174. #else
  175. #include <strstrea.h>
  176. #endif
  177. #endif //_RW_STD_IOSTREAM
  178.  
  179. string generateLabel ()
  180. {
  181.     //
  182.     // Generate a label string of the form L_ddd.
  183.     //
  184.     static int lastLabel = 0;
  185.     char labelBuffer[80];
  186.     ostrstream ost(labelBuffer, 80);
  187.     ost << "L_" << lastLabel++ << '\0';
  188.     return string(labelBuffer);
  189. }
  190.  
  191. //
  192. // Illustrate the use of the generate and genrate_n functions.
  193. //
  194.  
  195. void generate_example ()
  196. {
  197.     cout << "Illustrate generate algorithm" << endl;
  198.     //
  199.     // Example 1, generate a list of label numbers.
  200.     //
  201.     list<string,allocator<string> > labelList;
  202.     generate_n (inserter(labelList, labelList.begin()), 4, generateLabel);  
  203.     copy(labelList.begin(),labelList.end(),ostream_iterator<string,char,char_traits<char> >(cout," "));
  204.     cout << endl;
  205.     //
  206.     // Example 2, generate an arithmetic progression.
  207.     //
  208.     vector<int,allocator<int> > iVec(10);
  209.     generate (iVec.begin(), iVec.end(), iotaGen(2));
  210.     generate_n (iVec.begin(), 5, iotaGen(7));
  211.     copy (iVec.begin(), iVec.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  212.     cout << endl;
  213. }
  214.  
  215. //
  216. // Illustrate the use of the algorithm swap_ranges.
  217. //
  218.  
  219. void swap_example ()
  220. {
  221.     cout << "Illustrate swap_ranges algorithm" << endl;
  222.     //
  223.     // First make two parallel sequences.
  224.     //
  225.     int data[] = {12, 27, 14, 64}, *datap = data;
  226.     vector<int,allocator<int> > aVec(4);
  227.     generate (aVec.begin(), aVec.end(), iotaGen(1));
  228.     //
  229.     // Illustrate swap and swap_itr.
  230.     //
  231.     swap(data[0], data[2]);
  232.     copy (data, data+4, ostream_iterator<int,char,char_traits<char> >(cout, " "));
  233.     cout << endl;
  234.     vector<int,allocator<int> >::iterator last = aVec.end(); last--;
  235.     iter_swap(aVec.begin(), last);
  236.     copy (aVec.begin(), aVec.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  237.     cout << endl;
  238.     //
  239.     // Now swap the entire sequence.
  240.     //
  241.     swap_ranges (aVec.begin(), aVec.end(), datap);
  242.     copy (data, data+4, ostream_iterator<int,char,char_traits<char> >(cout, " ")), cout << endl;
  243.     copy (aVec.begin(), aVec.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  244.     cout << endl;
  245. }
  246.  
  247. int main ()
  248. {
  249.     cout << "STL generic algorithms -- initialization algorithms" << endl;
  250.  
  251.     fill_example();
  252.     copy_example();
  253.     generate_example();
  254.     swap_example();
  255.     
  256.     cout << "End of initialization tests" << endl;
  257.  
  258.     return 0;
  259. }
  260.