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

  1. #include "stlexam.h"
  2. #pragma hdrstop
  3. /**************************************************************************
  4.  *
  5.  * alg4.cpp - Example programs for STL generic algorithms removal 
  6.  *    algorithms. Section 12.5
  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 <list>
  44. #include <set>
  45. #include <algorithm>
  46.  
  47. #ifdef _RW_STD_IOSTREAM
  48. #include <iostream>
  49. #else
  50. #include <iostream.h>
  51. #endif
  52.  
  53. #ifndef _RWSTD_NO_NAMESPACE
  54. using namespace std;
  55. #endif
  56.     
  57. bool isEven (int n) { return 0 == (n % 2); }
  58.  
  59. //
  60. // Illustrate the use of the remove algorithm.
  61. //
  62.  
  63. void remove_example ()
  64. {
  65.     cout << "Remove Algorithm examples" << endl;
  66.     //
  67.     // Create a list of numbers.
  68.     //
  69.     int data[] = { 1, 2, 4, 3, 1, 4, 2 };
  70.     list<int,allocator<int> > aList;
  71.     copy (data, data+7, inserter(aList, aList.begin()));
  72.     cout << "Original list: ";
  73.     copy (aList.begin(), aList.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  74.     cout << endl;
  75.     //
  76.     // Remove 2's, copy into a new list.
  77.     //
  78.     list<int,allocator<int> > newList;
  79.     remove_copy (aList.begin(), aList.end(), back_inserter(newList), 2);
  80.     cout << "After removing 2's: ";
  81.     copy (newList.begin(), newList.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  82.     cout << endl;
  83.     //
  84.     // Remove 2's in place.
  85.     //
  86.     list<int,allocator<int> >::iterator where;
  87.     where = remove(aList.begin(), aList.end(), 2);
  88.     cout << "List after removal, before erase: ";
  89.     copy (aList.begin(), aList.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  90.     cout << endl;
  91.     aList.erase(where, aList.end());
  92.     cout << "List after erase: ";
  93.     copy (aList.begin(), aList.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  94.     cout << endl;
  95.     //
  96.     // Remove all even values.
  97.     //
  98.     where = remove_if (aList.begin(), aList.end(), isEven);
  99.     aList.erase(where, aList.end());
  100.     cout << "List after removing even values: ";
  101.     copy (aList.begin(), aList.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  102.     cout << endl;
  103. }
  104.  
  105. //
  106. // Illustrate use of the unqiue algorithm.
  107. //
  108. void unique_example ()
  109. {
  110.     //
  111.     // First make a list of values.
  112.     //
  113.     int data[] = { 1, 3, 3, 2, 2, 4 };
  114.     list<int,allocator<int> > aList;
  115.     copy(data, data+6, inserter(aList, aList.begin()));
  116.     cout << "Origianal List: ";
  117.     copy(aList.begin(), aList.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  118.     cout << endl;
  119.     //
  120.     // Copy unique elements into a set.
  121.     //
  122.     set<int, less<int>,allocator<int>  > aSet;
  123. //    unique_copy(aList.begin(), aList.end(), inserter(aSet, aSet.begin()));
  124.     cout << "Set after unique_copy: ";
  125.     copy(aSet.begin(), aSet.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  126.     cout << endl;
  127.     //
  128.     // Copy unique elements in place.
  129.     //
  130.     list<int,allocator<int> >::iterator where;
  131.     where = unique(aList.begin(), aList.end());
  132.     cout << "List after calling unique: ";
  133.     copy(aList.begin(), aList.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  134.     cout << endl;
  135.     //
  136.     // Remove trailing values.
  137.     //
  138.     aList.erase(where, aList.end());
  139.     cout << "List after erase: ";
  140.     copy(aList.begin(), aList.end(), ostream_iterator<int,char,char_traits<char> >(cout, " "));
  141.     cout << endl;
  142. }
  143.  
  144. int main ()
  145. {
  146.     cout << "STL generic algorithms -- Removal Algorithms" << endl;
  147.  
  148.     remove_example();
  149.     unique_example();
  150.     
  151.     cout << "End of removal algorithms sample program" << endl;
  152.  
  153.     return 0;
  154. }
  155.