home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 May / PCFMay2001.iso / Xenon / C++ / FreeCommandLineTools.exe / Examples / StdLib / alg1.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-31  |  7.3 KB  |  230 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.  ***************************************************************************
  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 <ctype.h>
  47. #include <string>
  48. #include <string.h>
  49.  
  50. #include <iostream>
  51.  
  52. #ifndef _RWSTD_NO_NAMESPACE
  53. using namespace std;
  54. #endif
  55.  
  56. class iotaGen
  57. {
  58.   public:
  59.     iotaGen (int iv) : current(iv) { }
  60.     int operator () () { return current++; }
  61.   private:
  62.     int current;
  63. };
  64.  
  65. //
  66. // Illustrate the use of the fill and fill_n functions.
  67. //
  68.  
  69. void fill_example ()
  70. {
  71.     cout << "Illustrate fill function" << endl;
  72.     //
  73.     // Example 1, fill an array with initial values
  74.     //
  75.     char buffer[100], *bufferp = buffer;
  76.     fill (bufferp,(bufferp + 100),'\0');
  77.     fill_n (bufferp, 10, 'x');
  78.     cout << buffer << endl;
  79.     //
  80.     // Example 2, use fill to initialize a list.
  81.     //
  82.     list<string,allocator<string> > aList;
  83.     fill_n (inserter(aList, aList.begin()), 10, "empty");
  84.     copy(aList.begin(), aList.end(), ostream_iterator<string,char,char_traits<char> >(cout, " "));
  85.     cout << endl;
  86.     //
  87.     // Example 3, use fill to overwrite values in a list.
  88.     //
  89.     fill (aList.begin(), aList.end(), "full");
  90.     copy(aList.begin(), aList.end(), ostream_iterator<string,char,char_traits<char> >(cout, " "));
  91.     cout << endl;
  92.     //
  93.     // Example 4, fill in a portion of a list.
  94.     //
  95.     vector<int,allocator<int> > iVec(10);
  96.     generate (iVec.begin(), iVec.end(), iotaGen(1));
  97.     vector<int,allocator<int> >::iterator seven = find(iVec.begin(), iVec.end(), 7);
  98. #ifndef _RWSTD_FILL_NAME_CLASH
  99.     fill(iVec.begin(), seven, 0);
  100. #else
  101.     std_fill(iVec.begin(), seven, 0);
  102. #endif
  103.     copy(iVec.begin(), iVec.end(), ostream_iterator<int,char,char_traits<char> >(cout));
  104.     cout << endl;
  105. }
  106.  
  107. //
  108. // Illustrate the use of the copy function.
  109. //
  110.  
  111. void copy_example ()
  112. {
  113.     cout << "Illustrate copy function " << endl;
  114.     //
  115.     // Example 1, a simple copy.
  116.     //
  117.     char * source  = "reprise";
  118.     char * surpass = "surpass";
  119.     char buffer[120], *bufferp = buffer;
  120.     copy(source, source + strlen(source) + 1, bufferp);
  121.     //
  122.     // Example 2, self copies.
  123.     //
  124.     *copy(bufferp + 2, bufferp+ strlen(buffer), bufferp) = '\0';
  125.     int buflen = strlen(buffer) + 1;
  126.     copy_backward(bufferp, bufferp + buflen, bufferp + buflen + 3);
  127.     copy(surpass, surpass + 3, bufferp);
  128.     //
  129.     // Example 3, copy to output.
  130.     //
  131.     copy(bufferp, bufferp + strlen(buffer), ostream_iterator<char,char,char_traits<char> >(cout));
  132.     cout << endl;
  133.     //
  134.     // Example 4, use copy to convert type.
  135.     //
  136.     list<char,allocator<char> > char_list;
  137.     copy(bufferp, bufferp + strlen(buffer),
  138.          inserter(char_list,char_list.end()));
  139.     char * big = "big ";
  140.     copy(big, big + 4, inserter(char_list, char_list.begin()));
  141.     
  142.     char buffer2[200], *buffer2p = buffer2;
  143.     *copy(char_list.begin(), char_list.end(), buffer2p) = '\0';
  144.     cout << buffer2 << endl;
  145. }
  146.  
  147.  
  148. #include <sstream>
  149.  
  150. string generateLabel ()
  151. {
  152.     //
  153.     // Generate a label string of the form L_ddd.
  154.     //
  155.     static int lastLabel = 0;
  156.     ostringstream ost;
  157.     ost << "L_" << lastLabel++ << '\0';
  158.     return ost.str();
  159. }
  160.  
  161. //
  162. // Illustrate the use of the generate and genrate_n functions.
  163. //
  164.  
  165. void generate_example ()
  166. {
  167.     cout << "Illustrate generate algorithm" << endl;
  168.     //
  169.     // Example 1, generate a list of label numbers.
  170.     //
  171.     list<string,allocator<string> > labelList;
  172.     generate_n (inserter(labelList, labelList.begin()), 4, generateLabel);  
  173.     copy(labelList.begin(),labelList.end(),ostream_iterator<string,char,char_traits<char> >(cout," "));
  174.     cout << endl;
  175.     //
  176.     // Example 2, generate an arithmetic progression.
  177.     //
  178.     vector<int,allocator<int> > iVec(10);
  179.     generate (iVec.begin(), iVec.end(), iotaGen(2));
  180.     generate_n (iVec.begin(), 5, iotaGen(7));
  181.     copy (iVec.begin(), iVec.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  182.     cout << endl;
  183. }
  184.  
  185. //
  186. // Illustrate the use of the algorithm swap_ranges.
  187. //
  188.  
  189. void swap_example ()
  190. {
  191.     cout << "Illustrate swap_ranges algorithm" << endl;
  192.     //
  193.     // First make two parallel sequences.
  194.     //
  195.     int data[] = {12, 27, 14, 64}, *datap = data;
  196.     vector<int,allocator<int> > aVec(4);
  197.     generate (aVec.begin(), aVec.end(), iotaGen(1));
  198.     //
  199.     // Illustrate swap and swap_itr.
  200.     //
  201.     swap(data[0], data[2]);
  202.     copy (data, data+4, ostream_iterator<int,char,char_traits<char> >(cout, " "));
  203.     cout << endl;
  204.     vector<int,allocator<int> >::iterator last = aVec.end(); last--;
  205.     iter_swap(aVec.begin(), last);
  206.     copy (aVec.begin(), aVec.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  207.     cout << endl;
  208.     //
  209.     // Now swap the entire sequence.
  210.     //
  211.     swap_ranges (aVec.begin(), aVec.end(), datap);
  212.     copy (data, data+4, ostream_iterator<int,char,char_traits<char> >(cout, " ")), cout << endl;
  213.     copy (aVec.begin(), aVec.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  214.     cout << endl;
  215. }
  216.  
  217. int main ()
  218. {
  219.     cout << "STL generic algorithms -- initialization algorithms" << endl;
  220.  
  221.     fill_example();
  222.     copy_example();
  223.     generate_example();
  224.     swap_example();
  225.     
  226.     cout << "End of initialization tests" << endl;
  227.  
  228.     return 0;
  229. }
  230.