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

  1. #include "stlexam.h"
  2. #pragma hdrstop
  3. /**************************************************************************
  4.  *
  5.  * alg7.cpp - Illustrate the use of the sort related generic algorithms.
  6.  *    Section 13
  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.  
  44. #include <vector>
  45. #include <deque>
  46. #include <list>
  47. #include <algorithm>
  48. #include <functional>
  49.  
  50. #ifdef _RW_STD_IOSTREAM
  51. #include <iostream>
  52. #else
  53. #include <iostream.h>
  54. #endif
  55.  
  56. #ifndef _RWSTD_NO_NAMESPACE
  57. using namespace std;
  58. #endif
  59.     
  60. int randomInteger (unsigned int n) { return rand() % n; }
  61.     
  62. int randomValue () { return randomInteger(100); }
  63.  
  64. ostream_iterator<int,char,char_traits<char> > intOut(cout, " ");
  65.         
  66. void sortExample ()
  67. {
  68.     cout << "Sort algorithms"  << endl;
  69.     //
  70.     // Fill both a vector and a deque with random integers.
  71.     //
  72.     vector<int,allocator<int> > aVec(15);
  73.     deque<int,allocator<int> >  aDec(15);
  74.     generate (aVec.begin(), aVec.end(), randomValue);
  75.     generate (aDec.begin(), aDec.end(), randomValue);
  76.     //
  77.     // Sort the vector ascending.
  78.     //
  79.     sort (aVec.begin(), aVec.end());
  80.     copy (aVec.begin(), aVec.end(), intOut);
  81.     cout << endl;
  82.     //
  83.     // Sort the deque descending.
  84.     //
  85.     sort (aDec.begin(), aDec.end(), greater<int>() );
  86.     copy (aDec.begin(), aDec.end(), intOut);
  87.     cout << endl;
  88.     //
  89.     // Sort the vector descending?
  90.     //
  91.     sort (aVec.rbegin(), aVec.rend());
  92.     copy (aVec.begin(), aVec.end(), intOut);
  93.     cout << endl;
  94. }
  95.  
  96. void partial_sort_example ()
  97. {
  98.     cout << "Partial sort examples" << endl;
  99.     //
  100.     // Make a vector of 15 random integers.
  101.     //
  102.     vector<int,allocator<int> > aVec(15);
  103.     generate (aVec.begin(), aVec.end(), randomValue);
  104.     copy (aVec.begin(), aVec.end(), intOut);
  105.     cout << endl;
  106.     //
  107.     // Partial sort the first seven positions.
  108.     //
  109.     partial_sort (aVec.begin(), aVec.begin() + 7, aVec.end());
  110.     copy (aVec.begin(), aVec.end(), intOut);
  111.     cout << endl;
  112.     //
  113.     // Make a list of random integers.
  114.     //
  115.     list<int,allocator<int> > aList(15, 0);
  116.     generate (aList.begin(), aList.end(), randomValue);
  117.     copy (aList.begin(), aList.end(), intOut);
  118.     cout << endl;
  119.     //
  120.     // Sort only the first seven elements.
  121.     //
  122.     vector<int,allocator<int> > start(7);
  123.     partial_sort_copy (aList.begin(), aList.end(),
  124.                        start.begin(), start.end(), greater<int>());
  125.     copy (start.begin(), start.end(), intOut);
  126.     cout << endl;   
  127. }
  128.  
  129. //
  130. // Illustrate the use of the nth_largest function.
  131. //
  132.  
  133. void nth_element_example () 
  134. {
  135.     cout << "Nth largest example" << endl;
  136.     //
  137.     // Make a vector of random integers.
  138.     //
  139.     vector<int,allocator<int> > aVec(10);
  140.     generate (aVec.begin(), aVec.end(), randomValue);
  141.     //
  142.     // Now find the 5th largest.
  143.     //
  144.     vector<int,allocator<int> >::iterator nth = aVec.begin() + 4;
  145.     nth_element(aVec.begin(), nth, aVec.end());
  146.     cout << "fifth largest is " << *nth << " in" << endl;
  147.     copy (aVec.begin(), aVec.end(), intOut);
  148.     cout << endl;
  149. }
  150.  
  151. //
  152. // Illustrate the use of the binary search functions.
  153. //
  154.  
  155. void binary_search_example ()
  156. {
  157.     cout << "Binary search example" << endl;
  158.     //
  159.     // Make an ordered vector of 15 random integers.
  160.     //
  161.     vector<int,allocator<int> > aVec(15);
  162.     generate (aVec.begin(), aVec.end(), randomValue);
  163.     sort (aVec.begin(), aVec.end());
  164.     copy (aVec.begin(), aVec.end(), intOut),   cout << endl;
  165.     //
  166.     // See if it contains an eleven.
  167.     //
  168.     if (binary_search(aVec.begin(), aVec.end(), 11))
  169.         cout << "contains an 11" << endl;
  170.     else
  171.         cout << "does not contain an 11"  << endl;
  172.     //
  173.     // Insert an 11 and a 14.
  174.     //
  175.     vector<int,allocator<int> >::iterator where;
  176.     where = lower_bound (aVec.begin(), aVec.end(), 11);
  177.     aVec.insert (where, 11);
  178.     where = upper_bound (aVec.begin(), aVec.end(), 14);
  179.     aVec.insert (where, 14);
  180.     copy (aVec.begin(), aVec.end(), intOut),   cout << endl;
  181. }
  182.  
  183. //
  184. // Illustrate the use of the merge function.
  185. //
  186.  
  187. void merge_example ()
  188. {
  189.     cout << "Merge algorithm examples" << endl;
  190.     //
  191.     // Make a list and vector of 10 random integers.
  192.     //
  193.     vector<int,allocator<int> > aVec(10);
  194.     list<int,allocator<int> > aList(10, 0);
  195.     generate (aVec.begin(), aVec.end(), randomValue);
  196.     sort (aVec.begin(), aVec.end());
  197.     generate_n (aList.begin(), 10, randomValue);
  198.     aList.sort();
  199.     //
  200.     // Merge into a vector.
  201.     //
  202.     vector<int,allocator<int> > vResult (aVec.size() + aList.size());
  203.     merge (aVec.begin(), aVec.end(), aList.begin(), aList.end(), 
  204.            vResult.begin());
  205.     //
  206.     // Merge into a list.
  207.     //
  208.     list<int,allocator<int> > lResult;
  209.     merge (aVec.begin(), aVec.end(), aList.begin(), aList.end(), 
  210.            inserter(lResult, lResult.begin()));
  211.     //
  212.     // Merge into the output.
  213.     //
  214.     merge (aVec.begin(), aVec.end(), aList.begin(), aList.end(), 
  215.            ostream_iterator<int,char,char_traits<char> >(cout, " "));
  216.     cout << endl;
  217. }
  218.  
  219. class iotaGen
  220. {
  221.   public:
  222.     iotaGen (int c = 0) : current(c) { }
  223.     int operator () () { return current++; }
  224.   private:
  225.     int current;
  226. };
  227.  
  228. //
  229. // Illustrate the use of the generic set functions.
  230. //
  231.  
  232. void set_example ()
  233.  
  234. {
  235.     cout << "Set operations:" << endl;
  236.     //
  237.     // Make a couple of ordered lists.
  238.     //
  239.     list <int,allocator<int> > listOne, listTwo;
  240.     generate_n (inserter(listOne, listOne.begin()), 5, iotaGen(1));
  241.     generate_n (inserter(listTwo, listTwo.begin()), 5, iotaGen(3));
  242.     //
  243.     // union - 1 2 3 4 5 6 7
  244.     //
  245.     set_union (listOne.begin(), listOne.end(),
  246.                listTwo.begin(), listTwo.end(), intOut);
  247.     cout << endl;
  248.     //
  249.     // merge - 1 2 3 3 4 4 5 5 6 7
  250.     //
  251.     merge (listOne.begin(), listOne.end(),
  252.            listTwo.begin(), listTwo.end(), intOut);
  253.     cout << endl;
  254.     //
  255.     // intersection 3 4 5
  256.     //
  257.     set_intersection (listOne.begin(), listOne.end(),
  258.                       listTwo.begin(), listTwo.end(), intOut);
  259.     cout << endl;
  260.     //
  261.     // difference 1 2
  262.     //
  263.     set_difference (listOne.begin(), listOne.end(),
  264.                     listTwo.begin(), listTwo.end(), intOut);
  265.     cout << endl;
  266.     //
  267.     // symmetric difference 1 2 6 7
  268.     //
  269.     set_symmetric_difference (listOne.begin(), listOne.end(), 
  270.                               listTwo.begin(), listTwo.end(), intOut);
  271.     cout << endl;
  272.         
  273.     if (includes(listOne.begin(), listOne.end(),
  274.             listTwo.begin(), listTwo.end()))
  275.                 cout << "set is subset" << endl;
  276.     else
  277.         cout << "set is not subset" << endl;
  278. }
  279.  
  280. //
  281. // Illustrate the use of the heap functions.
  282. //
  283.  
  284. void heap_example ()
  285. {
  286.     ostream_iterator<int,char,char_traits<char> > intOut(cout, " ");
  287.     //
  288.     // Make a heap of 15 random integers.
  289.     //
  290.     vector<int,allocator<int> > aVec(15);
  291.     generate (aVec.begin(), aVec.end(), randomValue);
  292.     make_heap (aVec.begin(), aVec.end());
  293.     copy (aVec.begin(), aVec.end(), intOut);
  294.     cout << endl;
  295.     cout << "Largest value " << aVec.front() << endl;
  296.     //
  297.     // Remove largest and reheap.
  298.     //
  299.     pop_heap(aVec.begin(), aVec.end());
  300.     aVec.pop_back();
  301.     copy (aVec.begin(), aVec.end(), intOut);
  302.     cout << endl;
  303.     //
  304.     // Add a 97 to the heap.
  305.     //
  306.     aVec.push_back(97);
  307.     push_heap (aVec.begin(), aVec.end());
  308.     copy (aVec.begin(), aVec.end(), intOut);
  309.     cout << endl;
  310.     //
  311.     // Finally, make into sorted collection.
  312.     //
  313.     sort_heap (aVec.begin(), aVec.end());
  314.     copy (aVec.begin(), aVec.end(), intOut);
  315.     cout << endl;
  316. }
  317.  
  318. int main ()
  319. {
  320.     cout << "Sorting generic algorithms examples" << endl;
  321.  
  322.     sortExample();
  323.     partial_sort_example();
  324.     nth_element_example();
  325.     binary_search_example();
  326.     merge_example();
  327.     set_example();
  328.     heap_example();
  329.     
  330.     cout << "End sorting examples" << endl;
  331.  
  332.     return 0;
  333. }
  334.