summaryrefslogtreecommitdiffstats
path: root/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_17.CPP
diff options
context:
space:
mode:
authorSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
committerSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
commit33613a85afc4b1481367fbe92a17ee59c240250b (patch)
tree670b842326116b376b505ec2263878912fca97e2 /Bachelor/Prog2/Codebeispiele/7_ch21/fig21_17.CPP
downloadStudium-master.tar.gz
Studium-master.tar.bz2
add new repoHEADmaster
Diffstat (limited to 'Bachelor/Prog2/Codebeispiele/7_ch21/fig21_17.CPP')
-rw-r--r--Bachelor/Prog2/Codebeispiele/7_ch21/fig21_17.CPP145
1 files changed, 145 insertions, 0 deletions
diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_17.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_17.CPP
new file mode 100644
index 0000000..bd6398d
--- /dev/null
+++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_17.CPP
@@ -0,0 +1,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. *
+ *************************************************************************/ \ No newline at end of file