summaryrefslogtreecommitdiffstats
path: root/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_17.CPP
blob: bd6398da2d091e175323c7bb8cd5873f406f35b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Fig. 21.17: fig21_17.cpp

// Standard library list class template test program.

#include <iostream>



using std::cout;

using std::endl;



#include <list>       // list class-template definition

#include <algorithm>  // copy algorithm



// prototype for function template printList

template < class T >

void printList( const std::list< T > &listRef );



int main()

{ 

   const int SIZE = 4;

   int array[ SIZE ] = { 2, 6, 4, 8 };



   std::list< int > values;

   std::list< int > otherValues;



   // insert items in values

   values.push_front( 1 );

   values.push_front( 2 );

   values.push_back( 4 );

   values.push_back( 3 );

   

   cout << "values contains: ";

   printList( values );



   values.sort();  // sort values



   cout << "\nvalues after sorting contains: ";

   printList( values );

  

   // insert elements of array into otherValues

   otherValues.insert( otherValues.begin(), 

      array, array + SIZE );



   cout << "\nAfter insert, otherValues contains: ";

   printList( otherValues );



   // remove otherValues elements and insert at end of values

   values.splice( values.end(), otherValues );

   

   cout << "\nAfter splice, values contains: ";

   printList( values );



   values.sort();  // sort values



   cout << "\nAfter sort, values contains: ";

   printList( values );



   // insert elements of array into otherValues

   otherValues.insert( otherValues.begin(), 

      array, array + SIZE );

   otherValues.sort();

   

   cout << "\nAfter insert, otherValues contains: ";

   printList( otherValues );

   

   // remove otherValues elements and insert into values 

   // in sorted order

   values.merge( otherValues );

   

   cout << "\nAfter merge:\n   values contains: ";

   printList( values );

   cout << "\n   otherValues contains: ";

   printList( otherValues );



   values.pop_front();  // remove element from front

   values.pop_back();   // remove element from back

   

   cout << "\nAfter pop_front and pop_back:" 

        << "\n   values contains: ";

   printList( values );

   

   values.unique();  // remove duplicate elements

   

   cout << "\nAfter unique, values contains: ";

   printList( values );



   // swap elements of values and otherValues

   values.swap( otherValues );

   

   cout << "\nAfter swap:\n   values contains: ";

   printList( values );

   cout << "\n   otherValues contains: ";

   printList( otherValues );



   // replace contents of values with elements of otherValues

   values.assign( otherValues.begin(), otherValues.end() );

   

   cout << "\nAfter assign, values contains: ";

   printList( values );



   // remove otherValues elements and insert into values 

   // in sorted order

   values.merge( otherValues ); 

   

   cout << "\nAfter merge, values contains: ";

   printList( values ); 

   

   values.remove( 4 );  // remove all 4s

   

   cout << "\nAfter remove( 4 ), values contains: ";

   printList( values );



   cout << endl;



   return 0;



} // end main



// printList function template definition; uses 

// ostream_iterator and copy algorithm to output list elements

template < class T >

void printList( const std::list< T > &listRef )

{

   if ( listRef.empty() )

      cout << "List is empty";



   else {

      std::ostream_iterator< T > output( cout, " " );

      std::copy( listRef.begin(), listRef.end(), output );



   } // end else



} // end function printList



/**************************************************************************

 * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice      *

 * Hall. All Rights Reserved.                                             *

 *                                                                        *

 * DISCLAIMER: The authors and publisher of this book have used their     *

 * best efforts in preparing the book. These efforts include the          *

 * development, research, and testing of the theories and programs        *

 * to determine their effectiveness. The authors and publisher make       *

 * no warranty of any kind, expressed or implied, with regard to these    *

 * programs or to the documentation contained in these books. The authors *

 * and publisher shall not be liable in any event for incidental or       *

 * consequential damages in connection with, or arising out of, the       *

 * furnishing, performance, or use of these programs.                     *

 *************************************************************************/