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