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

  1. #include "stlexam.h"
  2. #pragma hdrstop
  3. /**************************************************************************
  4.  *
  5.  * alg3.cpp - Sample programs for STL generic algorihtms that modify 
  6.  *   their arguments in place.  Section 12.4
  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 <functional>
  47. #include <ctype.h>
  48. #include <string>
  49. #include <string.h>
  50.  
  51. #ifdef _RW_STD_IOSTREAM
  52. #include <iostream>
  53. #else
  54. #include <iostream.h>
  55. #endif
  56.   
  57. #ifndef _RWSTD_NO_NAMESPACE
  58. using namespace std;
  59. #endif
  60.  
  61. class iotaGenerator
  62. {
  63.   public:
  64.     iotaGenerator (int iv) : current(iv) { }
  65.     int operator () () { return current++; }
  66.   private:
  67.     int current;
  68. };
  69.  
  70. bool isEven (int n) { return 0 == (n % 2); }
  71.  
  72. //
  73. // Illustrate the use of the reverse function.
  74. //
  75.  
  76. void reverse_example ()
  77. {
  78.     cout << "Illustrate reverse algorithm" << endl;
  79.     //
  80.     // Example 1, reversing a string.
  81.     //
  82.     char ctext[30] = "Rats live on no evil star";
  83.     char text[30];
  84.     strcpy(text,ctext);
  85.     reverse (text, text + strlen(text));
  86.     cout << text << endl;
  87.     //
  88.     // Example 2, reversing a list.
  89.     //
  90.     list<int,allocator<int> > iList;
  91.     generate_n(inserter(iList, iList.begin()), 10, iotaGenerator(2));
  92.     reverse (iList.begin(), iList.end());
  93.     copy (iList.begin(), iList.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  94.     cout << endl;    
  95. }
  96.  
  97. //
  98. // Illustrate the use of the replace function.
  99. //
  100.  
  101. void replace_example ()
  102. {
  103.     cout << "Illustrate replace algorithm" << endl;
  104.     //
  105.     // Make vector 0 1 2 3 4.
  106.     //
  107.     vector<int,allocator<int> > numbers(11);    
  108.     for (int i = 0; i < 11; i++)
  109.         numbers[i] = i < 5 ? i : 10 - i;
  110.     copy (numbers.begin(), numbers.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  111.     cout << endl;
  112.     //
  113.     // Replace 0 by 2.
  114.     //
  115.     replace (numbers.begin(), numbers.end(), 3, 7);
  116.     copy (numbers.begin(), numbers.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  117.     cout << endl;
  118.     //
  119.     // Replace even numbers by 9.
  120.     //
  121.     replace_if (numbers.begin(), numbers.end(), isEven, 9);
  122.     copy (numbers.begin(), numbers.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  123.     cout << endl;
  124.     //    
  125.     // Copy into a list, replacing 9 by 3.
  126.     //
  127.     int aList[] = { 2, 1, 4, 3, 2, 5 };
  128.     int bList[6];
  129.     int cList[6];
  130.     replace_copy (aList, aList+6, &bList[0], 2, 7);
  131.     replace_copy_if (bList, bList+6, &cList[0], bind2nd(greater<int>(), 3), 8);
  132.     copy (bList, bList + 6, ostream_iterator<int,char,char_traits<char> >(cout, " ")); cout << endl;
  133.     copy (cList, cList + 6, ostream_iterator<int,char,char_traits<char> >(cout, " ")); cout << endl;
  134. }
  135.  
  136. //
  137. // Illustrate the use of the rotate function.
  138. //
  139.  
  140. void rotate_example ()
  141. {
  142.     cout << "Illustrate rotate algorithm" << endl;
  143.     //
  144.     // Create the list 1 2 3 ... 10
  145.     //
  146.     list<int,allocator<int> > iList;
  147.     generate_n(inserter(iList, iList.begin()), 10, iotaGenerator(1));
  148.     //
  149.     // Find the location of the seven.
  150.     //
  151.     list<int,allocator<int> >::iterator  middle = find(iList.begin(), iList.end(), 7);
  152.     //
  153.     // Now rotate around that location.
  154.     //
  155.     rotate(iList.begin(), middle, iList.end());
  156.     copy (iList.begin(), iList.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  157.     cout << endl;
  158.     //    
  159.     // Rotate again around the same location.
  160.     //
  161.     list<int,allocator<int> > cList;
  162.     rotate_copy(iList.begin(), middle,iList.end(),
  163.                 inserter(cList, cList.begin()));
  164.     copy (cList.begin(), cList.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  165.     cout << endl; 
  166. }
  167.  
  168. //
  169. // Illustrate the use of the paration function.
  170. //
  171.  
  172. void partition_example ()
  173. {
  174.     cout << "Illustrate partition algorithm" << endl;
  175.     //
  176.     // First make the vector 1 2 3 ... 10.
  177.     //
  178.     vector<int,allocator<int> > numbers(10);
  179.     generate(numbers.begin(), numbers.end(), iotaGenerator(1));
  180.     //
  181.     // Now put the odd values low, even values high.
  182.     //
  183.     vector<int,allocator<int> >::iterator result = partition(numbers.begin(),numbers.end(),
  184.                                              isEven);
  185.     copy (numbers.begin(), numbers.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  186.     cout << endl;
  187.     cout << "middle location " << result - numbers.begin() << endl; 
  188.     //
  189.     // Now do a stable partition.
  190.     //
  191.     generate(numbers.begin(), numbers.end(), iotaGenerator(1));
  192.     copy (numbers.begin(), numbers.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  193.     cout << endl;
  194. }
  195.  
  196. bool nameCompare (char * a, char * b) { return strcmp(a, b) <= 0; }
  197.  
  198. //
  199. // Illustrate the use of the next_permutation function.
  200. //
  201.  
  202. void permutation_example ()
  203. {
  204.     //
  205.     // Start with the values 1 2 3 in sequence.
  206.     //
  207.     int start [] = {1, 2, 3 };
  208.     
  209.     do
  210.     {
  211.         copy (start, start + 3, ostream_iterator<int,char,char_traits<char> >(cout, " "));
  212.         cout << endl;
  213.     }
  214.     while (next_permutation(start, start + 3));
  215.         
  216.     char * names[] = { "Alpha", "Beta", "Gamma" };
  217.  
  218.     do
  219.     {
  220.         copy (names, names + 3, ostream_iterator<char *,char,char_traits<char> >(cout, " "));
  221.         cout << endl;
  222.     }
  223.     while (next_permutation(names, names + 3, nameCompare));
  224.     
  225.     char * cword = "bela";
  226.     char word[4];
  227.     strcpy(word,cword);
  228.  
  229.     do
  230.         cout << word << ' ';
  231.     while (prev_permutation(word, &word[4]));
  232.  
  233.     cout << endl;   
  234. }
  235.  
  236. //
  237. // Illustrate the use of the inplace_merge function.
  238. //
  239.  
  240. void inplace_merge_example ()
  241. {
  242.     cout << "Illustrate inplace merge algorithm" << endl;
  243.     //
  244.     // First generate the numbers 0 2 4 6 8 1 3 5 7 9.
  245.     //
  246.     vector<int,allocator<int> > numbers(10);
  247.     for (int i = 0; i < 10; i++)
  248.         numbers[i] = i < 5 ? 2 * i : 2 * (i - 5) + 1;
  249.     copy (numbers.begin(), numbers.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  250.     cout << endl; 
  251.     vector<int,allocator<int> >::iterator midvec = find(numbers.begin(), numbers.end(), 1);
  252.     //
  253.     // Copy them into a list.
  254.     //
  255.     list<int,allocator<int> > numList;
  256.     copy(numbers.begin(), numbers.end(), inserter(numList, numList.begin()));
  257.     list<int,allocator<int> >::iterator midList = find(numList.begin(), numList.end(), 1);
  258.     copy (numList.begin(), numList.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  259.     cout << endl; 
  260.     //
  261.     // Now put them back together.
  262.     //
  263.     inplace_merge(numbers.begin(), midvec, numbers.end());
  264.     inplace_merge(numList.begin(), midList, numList.end());
  265.     copy (numList.begin(), numList.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  266.     cout << endl; 
  267. }
  268.  
  269. struct RandomInteger
  270. {
  271.     int operator() (int m) { return rand() % m; }
  272. };
  273.  
  274. //
  275. // Illustrate the use of the random_shuffle function.
  276. //
  277.  
  278. void random_shuffle_example ()
  279. {
  280.     //
  281.     // First make the vector 1 2 3 ... 10.
  282.     //
  283.     vector<int,allocator<int> > numbers(10);
  284.     generate(numbers.begin(), numbers.end(), iotaGenerator(1));
  285.  
  286.     RandomInteger random;
  287.  
  288.     //
  289.     // Randomly shuffle the elements.
  290.     //
  291.     random_shuffle(numbers.begin(), numbers.end(), random);
  292.     copy (numbers.begin(), numbers.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  293.     cout << endl;
  294.     //
  295.     // Do it again.
  296.     //
  297.     random_shuffle(numbers.begin(), numbers.end(), random);
  298.     copy (numbers.begin(), numbers.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  299.     cout << endl;
  300. }
  301.  
  302. int main ()
  303. {
  304.     cout << "STL generic algorithms -- in-place algorithms" << endl;
  305.  
  306.     reverse_example();
  307.     replace_example();
  308.     rotate_example();
  309.     partition_example();
  310.     permutation_example();
  311.     inplace_merge_example();
  312.     random_shuffle_example();
  313.     
  314.     cout << "End of in-place algorithm sample program"  << endl;
  315.  
  316.     return 0;
  317. }
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.