From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- Bachelor/Prog2/Codebeispiele/7_ch21/fig21_05.CPP | 46 +++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_14.CPP | 85 +++++++++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_15.CPP | 86 ++++++++++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_17.CPP | 145 +++++++++++++++++++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_18.CPP | 57 +++++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_19.CPP | 90 ++++++++++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_20.CPP | 68 +++++++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_21.CPP | 62 ++++++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_22.CPP | 68 +++++++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_23.CPP | 74 ++++++++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_24.CPP | 46 +++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_25.CPP | 46 +++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_26.CPP | 73 ++++++++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_27.cpp | 86 ++++++++++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_28.cpp | 102 ++++++++++++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_29.CPP | 99 ++++++++++++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_30.CPP | 116 ++++++++++++++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_31.CPP | 97 +++++++++++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_32.CPP | 60 ++++++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_33.CPP | 79 ++++++++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_34.CPP | 69 +++++++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_35.CPP | 94 +++++++++++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_36.CPP | 96 +++++++++++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_37.CPP | 79 ++++++++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_38.cpp | 38 ++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_40.CPP | 82 +++++++++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_42.cpp | 86 ++++++++++++++ 27 files changed, 2129 insertions(+) create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_05.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_14.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_15.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_17.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_18.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_19.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_20.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_21.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_22.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_23.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_24.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_25.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_26.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_27.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_28.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_29.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_30.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_31.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_32.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_33.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_34.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_35.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_36.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_37.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_38.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_40.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_42.cpp (limited to 'Bachelor/Prog2/Codebeispiele/7_ch21') diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_05.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_05.CPP new file mode 100644 index 0000000..0fe4205 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_05.CPP @@ -0,0 +1,46 @@ +// Fig. 21.5: fig21_05.cpp +// Demonstrating input and output with iterators. +#include + +using std::cout; +using std::cin; +using std::endl; + +#include // ostream_iterator and istream_iterator + +int main() +{ + cout << "Enter two integers: "; + + // create istream_iterator for reading int values from cin + std::istream_iterator< int > inputInt( cin ); + + int number1 = *inputInt; // read int from standard input + ++inputInt; // move iterator to next input value + int number2 = *inputInt; // read int from standard input + + // create ostream_iterator for writing int values to cout + std::ostream_iterator< int > outputInt( cout ); + + cout << "The sum is: "; + *outputInt = number1 + number2; // output result to cout + cout << endl; + + return 0; + +} // end main + +/************************************************************************** + * (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 diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_14.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_14.CPP new file mode 100644 index 0000000..8a42288 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_14.CPP @@ -0,0 +1,85 @@ +// Fig. 21.14: fig21_14.cpp +// Demonstrating standard library vector class template. +#include + +using std::cout; +using std::cin; +using std::endl; + +#include // vector class-template definition + +// prototype for function template printVector +template < class T > +void printVector( const std::vector< T > &integers2 ); + +int main() +{ + const int SIZE = 6; + int array[ SIZE ] = { 1, 2, 3, 4, 5, 6 }; + + std::vector< int > integers; + + cout << "The initial size of integers is: " + << integers.size() + << "\nThe initial capacity of integers is: " + << integers.capacity(); + + // function push_back is in every sequence collection + integers.push_back( 2 ); + integers.push_back( 3 ); + integers.push_back( 4 ); + + cout << "\nThe size of integers is: " << integers.size() + << "\nThe capacity of integers is: " + << integers.capacity(); + + cout << "\n\nOutput array using pointer notation: "; + + for ( int *ptr = array; ptr != array + SIZE; ++ptr ) + cout << *ptr << ' '; + + cout << "\nOutput vector using iterator notation: "; + printVector( integers ); + + cout << "\nReversed contents of vector integers: "; + + std::vector< int >::reverse_iterator reverseIterator; + + for ( reverseIterator = integers.rbegin(); + reverseIterator!= integers.rend(); + ++reverseIterator ) + cout << *reverseIterator << ' '; + + cout << endl; + + return 0; + +} // end main + +// function template for outputting vector elements +template < class T > +void printVector( const std::vector< T > &integers2 ) +{ + std::vector< T >::const_iterator constIterator; + + for ( constIterator = integers2.begin(); + constIterator != integers2.end(); + constIterator++ ) + cout << *constIterator << ' '; + +} // end function printVector + +/************************************************************************** + * (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 diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_15.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_15.CPP new file mode 100644 index 0000000..bb74911 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_15.CPP @@ -0,0 +1,86 @@ +// Fig. 21.15: fig21_15.cpp +// Testing Standard Library vector class template +// element-manipulation functions. +#include + +using std::cout; +using std::endl; + +#include // vector class-template definition +#include // copy algorithm + +int main() +{ + const int SIZE = 6; + int array[ SIZE ] = { 1, 2, 3, 4, 5, 6 }; + + std::vector< int > integers( array, array + SIZE ); + std::ostream_iterator< int > output( cout, " " ); + + cout << "Vector integers contains: "; + std::copy( integers.begin(), integers.end(), output ); + + cout << "\nFirst element of integers: " << integers.front() + << "\nLast element of integers: " << integers.back(); + + integers[ 0 ] = 7; // set first element to 7 + integers.at( 2 ) = 10; // set element at position 2 to 10 + + // insert 22 as 2nd element + integers.insert( integers.begin() + 1, 22 ); + + cout << "\n\nContents of vector integers after changes: "; + std::copy( integers.begin(), integers.end(), output ); + + // access out-of-range element + try { + integers.at( 100 ) = 777; + + } // end try + + // catch out_of_range exception + catch ( std::out_of_range outOfRange ) { + cout << "\n\nException: " << outOfRange.what(); + + } // end catch + + // erase first element + integers.erase( integers.begin() ); + cout << "\n\nVector integers after erasing first element: "; + std::copy( integers.begin(), integers.end(), output ); + + // erase remaining elements + integers.erase( integers.begin(), integers.end() ); + cout << "\nAfter erasing all elements, vector integers " + << ( integers.empty() ? "is" : "is not" ) << " empty"; + + // insert elements from array + integers.insert( integers.begin(), array, array + SIZE ); + cout << "\n\nContents of vector integers before clear: "; + std::copy( integers.begin(), integers.end(), output ); + + // empty integers; clear calls erase to empty a collection + integers.clear(); + cout << "\nAfter clear, vector integers " + << ( integers.empty() ? "is" : "is not" ) << " empty"; + + cout << endl; + + return 0; + +} // end main + +/************************************************************************** + * (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 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 + +using std::cout; +using std::endl; + +#include // list class-template definition +#include // 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 diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_18.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_18.CPP new file mode 100644 index 0000000..c8edb5d --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_18.CPP @@ -0,0 +1,57 @@ +// Fig. 21.18: fig21_18.cpp +// Standard library class deque test program. +#include + +using std::cout; +using std::endl; + +#include // deque class-template definition +#include // copy algorithm + +int main() +{ + std::deque< double > values; + std::ostream_iterator< double > output( cout, " " ); + + // insert elements in values + values.push_front( 2.2 ); + values.push_front( 3.5 ); + values.push_back( 1.1 ); + + cout << "values contains: "; + + // use subscript operator to obtain elements of values + for ( int i = 0; i < values.size(); ++i ) + cout << values[ i ] << ' '; + + values.pop_front(); // remove first element + + cout << "\nAfter pop_front, values contains: "; + std::copy( values.begin(), values.end(), output ); + + // use subscript operator to modify element at location 1 + values[ 1 ] = 5.4; + + cout << "\nAfter values[ 1 ] = 5.4, values contains: "; + std::copy( values.begin(), values.end(), output ); + + cout << endl; + + return 0; + +} // end main + +/************************************************************************** + * (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 diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_19.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_19.CPP new file mode 100644 index 0000000..8b3e890 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_19.CPP @@ -0,0 +1,90 @@ +// Fig. 21.19: fig21_19.cpp +// Testing Standard Library class multiset +#include + +using std::cout; +using std::endl; + +#include // multiset class-template definition + +// define short name for multiset type used in this program +typedef std::multiset< int, std::less< int > > ims; + +#include // copy algorithm + +int main() +{ + const int SIZE = 10; + int a[ SIZE ] = { 7, 22, 9, 1, 18, 30, 100, 22, 85, 13 }; + + ims intMultiset; // ims is typedef for "integer multiset" + std::ostream_iterator< int > output( cout, " " ); + + cout << "There are currently " << intMultiset.count( 15 ) + << " values of 15 in the multiset\n"; + + intMultiset.insert( 15 ); // insert 15 in intMultiset + intMultiset.insert( 15 ); // insert 15 in intMultiset + + cout << "After inserts, there are " + << intMultiset.count( 15 ) + << " values of 15 in the multiset\n\n"; + + // iterator that cannot be used to change element values + ims::const_iterator result; + + // find 15 in intMultiset; find returns iterator + result = intMultiset.find( 15 ); + + if ( result != intMultiset.end() ) // if iterator not at end + cout << "Found value 15\n"; // found search value 15 + + // find 20 in intMultiset; find returns iterator + result = intMultiset.find( 20 ); + + if ( result == intMultiset.end() ) // will be true hence + cout << "Did not find value 20\n"; // did not find 20 + + // insert elements of array a into intMultiset + intMultiset.insert( a, a + SIZE ); + + cout << "\nAfter insert, intMultiset contains:\n"; + std::copy( intMultiset.begin(), intMultiset.end(), output ); + + // determine lower and upper bound of 22 in intMultiset + cout << "\n\nLower bound of 22: " + << *( intMultiset.lower_bound( 22 ) ); + cout << "\nUpper bound of 22: " + << *( intMultiset.upper_bound( 22 ) ); + + // p represents pair of const_iterators + std::pair< ims::const_iterator, ims::const_iterator > p; + + // use equal_range to determine lower and upper bound + // of 22 in intMultiset + p = intMultiset.equal_range( 22 ); + + cout << "\n\nequal_range of 22:" + << "\n Lower bound: " << *( p.first ) + << "\n Upper bound: " << *( p.second ); + + cout << endl; + + return 0; + +} // end main + +/************************************************************************** + * (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 diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_20.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_20.CPP new file mode 100644 index 0000000..76415d7 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_20.CPP @@ -0,0 +1,68 @@ +// Fig. 21.20: fig21_20.cpp +// Standard library class set test program. +#include + +using std::cout; +using std::endl; + +#include + +// define short name for set type used in this program +typedef std::set< double, std::less< double > > double_set; + +#include + +int main() +{ + const int SIZE = 5; + double a[ SIZE ] = { 2.1, 4.2, 9.5, 2.1, 3.7 }; + + double_set doubleSet( a, a + SIZE );; + std::ostream_iterator< double > output( cout, " " ); + + cout << "doubleSet contains: "; + std::copy( doubleSet.begin(), doubleSet.end(), output ); + + // p represents pair containing const_iterator and bool + std::pair< double_set::const_iterator, bool > p; + + // insert 13.8 in doubleSet; insert returns pair in which + // p.first represents location of 13.8 in doubleSet and + // p.second represents whether 13.8 was inserted + p = doubleSet.insert( 13.8 ); // value not in set + + cout << "\n\n" << *( p.first ) + << ( p.second ? " was" : " was not" ) << " inserted"; + + cout << "\ndoubleSet contains: "; + std::copy( doubleSet.begin(), doubleSet.end(), output ); + + // insert 9.5 in doubleSet + p = doubleSet.insert( 9.5 ); // value already in set + + cout << "\n\n" << *( p.first ) + << ( p.second ? " was" : " was not" ) << " inserted"; + + cout << "\ndoubleSet contains: "; + std::copy( doubleSet.begin(), doubleSet.end(), output ); + + cout << endl; + + return 0; + +} // end main + +/************************************************************************** + * (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 diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_21.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_21.CPP new file mode 100644 index 0000000..66036ae --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_21.CPP @@ -0,0 +1,62 @@ +// Fig. 21.21: fig21_21.cpp +// Standard library class multimap test program. +#include + +using std::cout; +using std::endl; + +#include // map class-template definition + +// define short name for multimap type used in this program +typedef std::multimap< int, double, std::less< int > > mmid; + +int main() +{ + mmid pairs; + + cout << "There are currently " << pairs.count( 15 ) + << " pairs with key 15 in the multimap\n"; + + // insert two value_type objects in pairs + pairs.insert( mmid::value_type( 15, 2.7 ) ); + pairs.insert( mmid::value_type( 15, 99.3 ) ); + + cout << "After inserts, there are " + << pairs.count( 15 ) + << " pairs with key 15\n\n"; + + // insert five value_type objects in pairs + pairs.insert( mmid::value_type( 30, 111.11 ) ); + pairs.insert( mmid::value_type( 10, 22.22 ) ); + pairs.insert( mmid::value_type( 25, 33.333 ) ); + pairs.insert( mmid::value_type( 20, 9.345 ) ); + pairs.insert( mmid::value_type( 5, 77.54 ) ); + + cout << "Multimap pairs contains:\nKey\tValue\n"; + + // use const_iterator to walk through elements of pairs + for ( mmid::const_iterator iter = pairs.begin(); + iter != pairs.end(); ++iter ) + cout << iter->first << '\t' + << iter->second << '\n'; + + cout << endl; + + return 0; + +} // end main + +/************************************************************************** + * (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 diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_22.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_22.CPP new file mode 100644 index 0000000..5dc0f95 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_22.CPP @@ -0,0 +1,68 @@ +// Fig. 21.22: fig21_22.cpp +// Standard library class map test program. +#include + +using std::cout; +using std::endl; + +#include // map class-template definition + +// define short name for map type used in this program +typedef std::map< int, double, std::less< int > > mid; + +int main() +{ + mid pairs; + + // insert eight value_type objects in pairs + pairs.insert( mid::value_type( 15, 2.7 ) ); + pairs.insert( mid::value_type( 30, 111.11 ) ); + pairs.insert( mid::value_type( 5, 1010.1 ) ); + pairs.insert( mid::value_type( 10, 22.22 ) ); + pairs.insert( mid::value_type( 25, 33.333 ) ); + pairs.insert( mid::value_type( 5, 77.54 ) ); // dupe ignored + pairs.insert( mid::value_type( 20, 9.345 ) ); + pairs.insert( mid::value_type( 15, 99.3 ) ); // dupe ignored + + cout << "pairs contains:\nKey\tValue\n"; + + // use const_iterator to walk through elements of pairs + for ( mid::const_iterator iter = pairs.begin(); + iter != pairs.end(); ++iter ) + cout << iter->first << '\t' + << iter->second << '\n'; + + // use subscript operator to change value for key 25 + pairs[ 25 ] = 9999.99; + + // use subscript operator insert value for key 40 + pairs[ 40 ] = 8765.43; + + cout << "\nAfter subscript operations, pairs contains:" + << "\nKey\tValue\n"; + + for ( mid::const_iterator iter2 = pairs.begin(); + iter2 != pairs.end(); ++iter2 ) + cout << iter2->first << '\t' + << iter2->second << '\n'; + + cout << endl; + + return 0; + +} // end main + +/************************************************************************** + * (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 diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_23.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_23.CPP new file mode 100644 index 0000000..6614c19 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_23.CPP @@ -0,0 +1,74 @@ +// Fig. 21.23: fig21_23.cpp +// Standard library adapter stack test program. +#include + +using std::cout; +using std::endl; + +#include // stack adapter definition +#include // vector class-template definition +#include // list class-template definition + +// popElements function-template prototype +template< class T > +void popElements( T &stackRef ); + +int main() +{ + // stack with default underlying deque + std::stack< int > intDequeStack; + + // stack with underlying vector + std::stack< int, std::vector< int > > intVectorStack; + + // stack with underlying list + std::stack< int, std::list< int > > intListStack; + + // push the values 0-9 onto each stack + for ( int i = 0; i < 10; ++i ) { + intDequeStack.push( i ); + intVectorStack.push( i ); + intListStack.push( i ); + + } // end for + + // display and remove elements from each stack + cout << "Popping from intDequeStack: "; + popElements( intDequeStack ); + cout << "\nPopping from intVectorStack: "; + popElements( intVectorStack ); + cout << "\nPopping from intListStack: "; + popElements( intListStack ); + + cout << endl; + + return 0; + +} // end main + +// pop elements from stack object to which stackRef refers +template< class T > +void popElements( T &stackRef ) +{ + while ( !stackRef.empty() ) { + cout << stackRef.top() << ' '; // view top element + stackRef.pop(); // remove top element + + } // end while + +} // end function popElements + +/************************************************************************** + * (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 diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_24.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_24.CPP new file mode 100644 index 0000000..14fd951 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_24.CPP @@ -0,0 +1,46 @@ +// Fig. 21.24: fig21_24.cpp +// Standard library adapter queue test program. +#include + +using std::cout; +using std::endl; + +#include // queue adapter definition + +int main() +{ + std::queue< double > values; + + // push elements onto queue values + values.push( 3.2 ); + values.push( 9.8 ); + values.push( 5.4 ); + + cout << "Popping from values: "; + + while ( !values.empty() ) { + cout << values.front() << ' '; // view front element + values.pop(); // remove element + + } // end while + + cout << endl; + + return 0; + +} // end main + +/************************************************************************** + * (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 diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_25.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_25.CPP new file mode 100644 index 0000000..f4b0e2d --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_25.CPP @@ -0,0 +1,46 @@ +// Fig. 21.25: fig21_25.cpp +// Standard library adapter priority_queue test program. +#include + +using std::cout; +using std::endl; + +#include // priority_queue adapter definition + +int main() +{ + std::priority_queue< double > priorities; + + // push elements onto priorities + priorities.push( 3.2 ); + priorities.push( 9.8 ); + priorities.push( 5.4 ); + + cout << "Popping from priorities: "; + + while ( !priorities.empty() ) { + cout << priorities.top() << ' '; // view top element + priorities.pop(); // remove top element + + } // end while + + cout << endl; + + return 0; + +} // end main + +/************************************************************************** + * (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 diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_26.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_26.CPP new file mode 100644 index 0000000..b1aa127 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_26.CPP @@ -0,0 +1,73 @@ +// Fig. 21.26: fig21_26.cpp +// Standard library algorithms fill, fill_n, generate +// and generate_n. +#include + +using std::cout; +using std::endl; + +#include // algorithm definitions +#include // vector class-template definition + +char nextLetter(); // prototype + +int main() +{ + std::vector< char > chars( 10 ); + std::ostream_iterator< char > output( cout, " " ); + + // fill chars with 5s + std::fill( chars.begin(), chars.end(), '5' ); + + cout << "Vector chars after filling with 5s:\n"; + std::copy( chars.begin(), chars.end(), output ); + + // fill first five elements of chars with As + std::fill_n( chars.begin(), 5, 'A' ); + + cout << "\n\nVector chars after filling five elements" + << " with As:\n"; + std::copy( chars.begin(), chars.end(), output ); + + // generate values for all elements of chars with nextLetter + std::generate( chars.begin(), chars.end(), nextLetter ); + + cout << "\n\nVector chars after generating letters A-J:\n"; + std::copy( chars.begin(), chars.end(), output ); + + // generate values for first five elements of chars + // with nextLetter + std::generate_n( chars.begin(), 5, nextLetter ); + + cout << "\n\nVector chars after generating K-O for the" + << " first five elements:\n"; + std::copy( chars.begin(), chars.end(), output ); + + cout << endl; + + return 0; + +} // end main + +// returns next letter in the alphabet (starts with A) +char nextLetter() +{ + static char letter = 'A'; + return letter++; + +} // end function nextLetter + +/************************************************************************** + * (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 diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_27.cpp b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_27.cpp new file mode 100644 index 0000000..c016b57 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_27.cpp @@ -0,0 +1,86 @@ +// Fig. 21.27: fig21_27.cpp +// Standard library functions equal, +// mismatch and lexicographical_compare. +#include + +using std::cout; +using std::endl; + +#include // algorithm definitions +#include // vector class-template definition + +int main() +{ + const int SIZE = 10; + int a1[ SIZE ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + int a2[ SIZE ] = { 1, 2, 3, 4, 1000, 6, 7, 8, 9, 10 }; + + std::vector< int > v1( a1, a1 + SIZE ); + std::vector< int > v2( a1, a1 + SIZE ); + std::vector< int > v3( a2, a2 + SIZE ); + + std::ostream_iterator< int > output( cout, " " ); + + cout << "Vector v1 contains: "; + std::copy( v1.begin(), v1.end(), output ); + cout << "\nVector v2 contains: "; + std::copy( v2.begin(), v2.end(), output ); + cout << "\nVector v3 contains: "; + std::copy( v3.begin(), v3.end(), output ); + + // compare vectors v1 and v2 for equality + bool result = + std::equal( v1.begin(), v1.end(), v2.begin() ); + + cout << "\n\nVector v1 " << ( result ? "is" : "is not" ) + << " equal to vector v2.\n"; + + // compare vectors v1 and v3 for equality + result = std::equal( v1.begin(), v1.end(), v3.begin() ); + cout << "Vector v1 " << ( result ? "is" : "is not" ) + << " equal to vector v3.\n"; + + // location represents pair of vector iterators + std::pair< std::vector< int >::iterator, + std::vector< int >::iterator > location; + + // check for mismatch between v1 and v3 + location = + std::mismatch( v1.begin(), v1.end(), v3.begin() ); + + cout << "\nThere is a mismatch between v1 and v3 at " + << "location " << ( location.first - v1.begin() ) + << "\nwhere v1 contains " << *location.first + << " and v3 contains " << *location.second + << "\n\n"; + + char c1[ SIZE ] = "HELLO"; + char c2[ SIZE ] = "BYE BYE"; + + // perform lexicographical comparison of c1 and c2 + result = std::lexicographical_compare( + c1, c1 + SIZE, c2, c2 + SIZE ); + + cout << c1 + << ( result ? " is less than " : + " is greater than or equal to " ) + << c2 << endl; + + return 0; + +} // end main + +/************************************************************************** + * (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. * + *************************************************************************/ diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_28.cpp b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_28.cpp new file mode 100644 index 0000000..9d36184 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_28.cpp @@ -0,0 +1,102 @@ +// Fig. 21.28: fig21_28.cpp +// Standard library functions remove, remove_if, +// remove_copy and remove_copy_if. +#include + +using std::cout; +using std::endl; + +#include // algorithm definitions +#include // vector class-template definition + +bool greater9( int ); // prototype + +int main() +{ + const int SIZE = 10; + int a[ SIZE ] = { 10, 2, 10, 4, 16, 6, 14, 8, 12, 10 }; + + std::ostream_iterator< int > output( cout, " " ); + + std::vector< int > v( a, a + SIZE ); + std::vector< int >::iterator newLastElement; + + cout << "Vector v before removing all 10s:\n "; + std::copy( v.begin(), v.end(), output ); + + // remove 10 from v + newLastElement = std::remove( v.begin(), v.end(), 10 ); + + cout << "\nVector v after removing all 10s:\n "; + std::copy( v.begin(), newLastElement, output ); + + std::vector< int > v2( a, a + SIZE ); + std::vector< int > c( SIZE, 0 ); + + cout << "\n\nVector v2 before removing all 10s " + << "and copying:\n "; + std::copy( v2.begin(), v2.end(), output ); + + // copy from v2 to c, removing 10s in the process + std::remove_copy( v2.begin(), v2.end(), c.begin(), 10 ); + + cout << "\nVector c after removing all 10s from v2:\n "; + std::copy( c.begin(), c.end(), output ); + + std::vector< int > v3( a, a + SIZE ); + + cout << "\n\nVector v3 before removing all elements" + << "\ngreater than 9:\n "; + std::copy( v3.begin(), v3.end(), output ); + + // remove elements greater than 9 from v3 + newLastElement = + std::remove_if( v3.begin(), v3.end(), greater9 ); + + cout << "\nVector v3 after removing all elements" + << "\ngreater than 9:\n "; + std::copy( v3.begin(), newLastElement, output ); + + std::vector< int > v4( a, a + SIZE ); + std::vector< int > c2( SIZE, 0 ); + + cout << "\n\nVector v4 before removing all elements" + << "\ngreater than 9 and copying:\n "; + std::copy( v4.begin(), v4.end(), output ); + + // copy elements from v4 to c2, removing elements greater + // than 9 in the process + std::remove_copy_if( + v4.begin(), v4.end(), c2.begin(), greater9 ); + + cout << "\nVector c2 after removing all elements" + << "\ngreater than 9 from v4:\n "; + std::copy( c2.begin(), c2.end(), output ); + + cout << endl; + + return 0; + +} // end main + +// determine whether argument is greater than 9 +bool greater9( int x ) +{ + return x > 9; + +} // end greater9 + +/************************************************************************** + * (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 diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_29.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_29.CPP new file mode 100644 index 0000000..252fed4 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_29.CPP @@ -0,0 +1,99 @@ +// Fig. 21.29: fig21_29.cpp +// Standard library functions replace, replace_if, +// replace_copy and replace_copy_if. +#include + +using std::cout; +using std::endl; + +#include +#include + +bool greater9( int ); + +int main() +{ + const int SIZE = 10; + int a[ SIZE ] = { 10, 2, 10, 4, 16, 6, 14, 8, 12, 10 }; + + std::ostream_iterator< int > output( cout, " " ); + + std::vector< int > v1( a, a + SIZE ); + cout << "Vector v1 before replacing all 10s:\n "; + std::copy( v1.begin(), v1.end(), output ); + + // replace 10s in v1 with 100 + std::replace( v1.begin(), v1.end(), 10, 100 ); + + cout << "\nVector v1 after replacing 10s with 100s:\n "; + std::copy( v1.begin(), v1.end(), output ); + + std::vector< int > v2( a, a + SIZE ); + std::vector< int > c1( SIZE ); + + cout << "\n\nVector v2 before replacing all 10s " + << "and copying:\n "; + std::copy( v2.begin(), v2.end(), output ); + + // copy from v2 to c1, replacing 10s with 100s + std::replace_copy( + v2.begin(), v2.end(), c1.begin(), 10, 100 ); + + cout << "\nVector c1 after replacing all 10s in v2:\n "; + std::copy( c1.begin(), c1.end(), output ); + + std::vector< int > v3( a, a + SIZE ); + + cout << "\n\nVector v3 before replacing values greater" + << " than 9:\n "; + std::copy( v3.begin(), v3.end(), output ); + + // replace values greater than 9 in v3 with 100 + std::replace_if( v3.begin(), v3.end(), greater9, 100 ); + + cout << "\nVector v3 after replacing all values greater" + << "\nthan 9 with 100s:\n "; + std::copy( v3.begin(), v3.end(), output ); + + std::vector< int > v4( a, a + SIZE ); + std::vector< int > c2( SIZE ); + + cout << "\n\nVector v4 before replacing all values greater " + << "than 9 and copying:\n "; + std::copy( v4.begin(), v4.end(), output ); + + // copy v4 to c2, replacing elements greater than 9 with 100 + std::replace_copy_if( + v4.begin(), v4.end(), c2.begin(), greater9, 100 ); + + cout << "\nVector c2 after replacing all values greater " + << "than 9 in v4:\n "; + std::copy( c2.begin(), c2.end(), output ); + + cout << endl; + + return 0; + +} // end main + +// determine whether argument is greater than 9 +bool greater9( int x ) +{ + return x > 9; + +} // end function greater9 + +/************************************************************************** + * (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 diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_30.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_30.CPP new file mode 100644 index 0000000..c961976 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_30.CPP @@ -0,0 +1,116 @@ +// Fig. 21.30: fig21_30.cpp +// Mathematical algorithms of the standard library. +#include + +using std::cout; +using std::endl; + +#include // algorithm definitions +#include // accumulate is defined here +#include + +bool greater9( int ); +void outputSquare( int ); +int calculateCube( int ); + +int main() +{ + const int SIZE = 10; + int a1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + + std::vector< int > v( a1, a1 + SIZE ); + std::ostream_iterator< int > output( cout, " " ); + + cout << "Vector v before random_shuffle: "; + std::copy( v.begin(), v.end(), output ); + + // shuffle elements of v + std::random_shuffle( v.begin(), v.end() ); + + cout << "\nVector v after random_shuffle: "; + std::copy( v.begin(), v.end(), output ); + + int a2[] = { 100, 2, 8, 1, 50, 3, 8, 8, 9, 10 }; + std::vector< int > v2( a2, a2 + SIZE ); + + cout << "\n\nVector v2 contains: "; + std::copy( v2.begin(), v2.end(), output ); + + // count number of elements in v2 with value 8 + int result = std::count( v2.begin(), v2.end(), 8 ); + + std::cout << "\nNumber of elements matching 8: " << result; + + // count number of elements in v2 that are greater than 9 + result = std::count_if( v2.begin(), v2.end(), greater9 ); + + cout << "\nNumber of elements greater than 9: " << result; + + // locate minimum element in v2 + cout << "\n\nMinimum element in Vector v2 is: " + << *( std::min_element( v2.begin(), v2.end() ) ); + + // locate maximum element in v2 + cout << "\nMaximum element in Vector v2 is: " + << *( std::max_element( v2.begin(), v2.end() ) ); + + // calculate sum of elements in v + cout << "\n\nThe total of the elements in Vector v is: " + << std::accumulate( v.begin(), v.end(), 0 ); + + cout << "\n\nThe square of every integer in Vector v is:\n"; + + // output square of every element in v + std::for_each( v.begin(), v.end(), outputSquare ); + + std::vector< int > cubes( SIZE ); + + // calculate cube of each element in v; + // place results in cubes + std::transform( + v.begin(), v.end(), cubes.begin(), calculateCube ); + + cout << "\n\nThe cube of every integer in Vector v is:\n"; + std::copy( cubes.begin(), cubes.end(), output ); + + cout << endl; + + return 0; + +} // end main + +// determine whether argument is greater than 9 +bool greater9( int value ) +{ + return value > 9; + +} // end function greater9 + +// output square of argument +void outputSquare( int value ) +{ + cout << value * value << ' '; + +} // end function outputSquare + +// return cube of argument +int calculateCube( int value ) +{ + return value * value * value; + +} // end function calculateCube + +/************************************************************************** + * (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. * + *************************************************************************/ diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_31.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_31.CPP new file mode 100644 index 0000000..97720e4 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_31.CPP @@ -0,0 +1,97 @@ +// Fig. 21.31: fig21_31.cpp +// Standard library search and sort algorithms. +#include + +using std::cout; +using std::endl; + +#include // algorithm definitions +#include // vector class-template definition + +bool greater10( int value ); // prototype + +int main() +{ + const int SIZE = 10; + int a[ SIZE ] = { 10, 2, 17, 5, 16, 8, 13, 11, 20, 7 }; + + std::vector< int > v( a, a + SIZE ); + std::ostream_iterator< int > output( cout, " " ); + + cout << "Vector v contains: "; + std::copy( v.begin(), v.end(), output ); + + // locate first occurrence of 16 in v + std::vector< int >::iterator location; + location = std::find( v.begin(), v.end(), 16 ); + + if ( location != v.end() ) + cout << "\n\nFound 16 at location " + << ( location - v.begin() ); + else + cout << "\n\n16 not found"; + + // locate first occurrence of 100 in v + location = std::find( v.begin(), v.end(), 100 ); + + if ( location != v.end() ) + cout << "\nFound 100 at location " + << ( location - v.begin() ); + else + cout << "\n100 not found"; + + // locate first occurrence of value greater than 10 in v + location = std::find_if( v.begin(), v.end(), greater10 ); + + if ( location != v.end() ) + cout << "\n\nThe first value greater than 10 is " + << *location << "\nfound at location " + << ( location - v.begin() ); + else + cout << "\n\nNo values greater than 10 were found"; + + // sort elements of v + std::sort( v.begin(), v.end() ); + + cout << "\n\nVector v after sort: "; + std::copy( v.begin(), v.end(), output ); + + // use binary_search to locate 13 in v + if ( std::binary_search( v.begin(), v.end(), 13 ) ) + cout << "\n\n13 was found in v"; + else + cout << "\n\n13 was not found in v"; + + // use binary_search to locate 100 in v + if ( std::binary_search( v.begin(), v.end(), 100 ) ) + cout << "\n100 was found in v"; + else + cout << "\n100 was not found in v"; + + cout << endl; + + return 0; + +} // end main + +// determine whether argument is greater than 10 +bool greater10( int value ) +{ + return value > 10; + +} // end function greater10 + +/************************************************************************** + * (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 diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_32.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_32.CPP new file mode 100644 index 0000000..439a8da --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_32.CPP @@ -0,0 +1,60 @@ +// Fig. 21.32: fig21_32.cpp +// Standard library algorithms iter_swap, swap and swap_ranges. +#include + +using std::cout; +using std::endl; + +#include // algorithm definitions + +int main() +{ + const int SIZE = 10; + int a[ SIZE ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + std::ostream_iterator< int > output( cout, " " ); + + cout << "Array a contains:\n "; + std::copy( a, a + SIZE, output ); + + // swap elements at locations 0 and 1 of array a + std::swap( a[ 0 ], a[ 1 ] ); + + cout << "\nArray a after swapping a[0] and a[1] " + << "using swap:\n "; + std::copy( a, a + SIZE, output ); + + // use iterators to swap elements at locations + // 0 and 1 of array a + std::iter_swap( &a[ 0 ], &a[ 1 ] ); + cout << "\nArray a after swapping a[0] and a[1] " + << "using iter_swap:\n "; + std::copy( a, a + SIZE, output ); + + // swap elements in first five elements of array a with + // elements in last five elements of array a + std::swap_ranges( a, a + 5, a + 5 ); + + cout << "\nArray a after swapping the first five elements\n" + << "with the last five elements:\n "; + std::copy( a, a + SIZE, output ); + + cout << endl; + + return 0; + +} // end main + +/************************************************************************** + * (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 diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_33.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_33.CPP new file mode 100644 index 0000000..f489c5e --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_33.CPP @@ -0,0 +1,79 @@ +// Fig. 21.33: fig21_33.cpp +// Standard library functions copy_backward, merge, +// unique and reverse. +#include + +using std::cout; +using std::endl; + +#include // algorithm definitions +#include // vector class-template definition + +int main() +{ + const int SIZE = 5; + int a1[ SIZE ] = { 1, 3, 5, 7, 9 }; + int a2[ SIZE ] = { 2, 4, 5, 7, 9 }; + + std::vector< int > v1( a1, a1 + SIZE ); + std::vector< int > v2( a2, a2 + SIZE ); + + std::ostream_iterator< int > output( cout, " " ); + + cout << "Vector v1 contains: "; + std::copy( v1.begin(), v1.end(), output ); + cout << "\nVector v2 contains: "; + std::copy( v2.begin(), v2.end(), output ); + + std::vector< int > results( v1.size() ); + + // place elements of v1 into results in reverse order + std::copy_backward( v1.begin(), v1.end(), results.end() ); + + cout << "\n\nAfter copy_backward, results contains: "; + std::copy( results.begin(), results.end(), output ); + + std::vector< int > results2( v1.size() + v2.size() ); + + // merge elements of v1 and v2 into results2 in sorted order + std::merge( v1.begin(), v1.end(), v2.begin(), v2.end(), + results2.begin() ); + + cout << "\n\nAfter merge of v1 and v2 results2 contains:\n"; + std::copy( results2.begin(), results2.end(), output ); + + // eliminate duplicate values from results2 + std::vector< int >::iterator endLocation; + endLocation = + std::unique( results2.begin(), results2.end() ); + + cout << "\n\nAfter unique results2 contains:\n"; + std::copy( results2.begin(), endLocation, output ); + + cout << "\n\nVector v1 after reverse: "; + + // reverse elements of v1 + std::reverse( v1.begin(), v1.end() ); + + std::copy( v1.begin(), v1.end(), output ); + + cout << endl; + + return 0; + +} // end main + +/************************************************************************** + * (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 diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_34.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_34.CPP new file mode 100644 index 0000000..e893200 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_34.CPP @@ -0,0 +1,69 @@ +// Fig. 21.34: fig21_34.cpp +// Standard library algorithms inplace_merge, +// reverse_copy and unique_copy. +#include + +using std::cout; +using std::endl; + +#include // algorithm definitions +#include // vector class-template definition +#include // back_inserter definition + +int main() +{ + const int SIZE = 10; + int a1[ SIZE ] = { 1, 3, 5, 7, 9, 1, 3, 5, 7, 9 }; + std::vector< int > v1( a1, a1 + SIZE ); + + std::ostream_iterator< int > output( cout, " " ); + + cout << "Vector v1 contains: "; + std::copy( v1.begin(), v1.end(), output ); + + // merge first half of v1 with second half of v1 such that + // v1 contains sorted set of elements after merge + std::inplace_merge( v1.begin(), v1.begin() + 5, v1.end() ); + + cout << "\nAfter inplace_merge, v1 contains: "; + std::copy( v1.begin(), v1.end(), output ); + + std::vector< int > results1; + + // copy only unique elemements of v1 into results1 + std::unique_copy( + v1.begin(), v1.end(), std::back_inserter( results1 ) ); + + cout << "\nAfter unique_copy results1 contains: "; + std::copy( results1.begin(), results1.end(), output ); + + std::vector< int > results2; + + cout << "\nAfter reverse_copy, results2 contains: "; + + // copy elements of v1 into results2 in reverse order + std::reverse_copy( + v1.begin(), v1.end(), std::back_inserter( results2 ) ); + + std::copy( results2.begin(), results2.end(), output ); + + cout << endl; + + return 0; + +} // end main + +/************************************************************************** + * (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 diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_35.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_35.CPP new file mode 100644 index 0000000..10df8f5 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_35.CPP @@ -0,0 +1,94 @@ +// Fig. 21.35: fig21_35.cpp +// Standard library algorithms includes, set_difference, +// set_intersection, set_symmetric_difference and set_union. +#include + +using std::cout; +using std::endl; + +#include // algorithm definitions + +int main() +{ + const int SIZE1 = 10, SIZE2 = 5, SIZE3 = 20; + int a1[ SIZE1 ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + int a2[ SIZE2 ] = { 4, 5, 6, 7, 8 }; + int a3[ SIZE2 ] = { 4, 5, 6, 11, 15 }; + std::ostream_iterator< int > output( cout, " " ); + + cout << "a1 contains: "; + std::copy( a1, a1 + SIZE1, output ); + cout << "\na2 contains: "; + std::copy( a2, a2 + SIZE2, output ); + cout << "\na3 contains: "; + std::copy( a3, a3 + SIZE2, output ); + + // determine whether set a2 is completely contained in a1 + if ( std::includes( a1, a1 + SIZE1, a2, a2 + SIZE2 ) ) + cout << "\n\na1 includes a2"; + else + cout << "\n\na1 does not include a2"; + + // determine whether set a3 is completely contained in a1 + if ( std::includes( a1, a1 + SIZE1, a3, a3 + SIZE2 ) ) + cout << "\na1 includes a3"; + else + cout << "\na1 does not include a3"; + + int difference[ SIZE1 ]; + + // determine elements of a1 not in a2 + int *ptr = std::set_difference( a1, a1 + SIZE1, + a2, a2 + SIZE2, difference ); + + cout << "\n\nset_difference of a1 and a2 is: "; + std::copy( difference, ptr, output ); + + int intersection[ SIZE1 ]; + + // determine elements in both a1 and a2 + ptr = std::set_intersection( a1, a1 + SIZE1, + a2, a2 + SIZE2, intersection ); + + cout << "\n\nset_intersection of a1 and a2 is: "; + std::copy( intersection, ptr, output ); + + int symmetric_difference[ SIZE1 ]; + + // determine elements of a1 that are not in a2 and + // elements of a2 that are not in a1 + ptr = std::set_symmetric_difference( a1, a1 + SIZE1, + a2, a2 + SIZE2, symmetric_difference ); + + cout << "\n\nset_symmetric_difference of a1 and a2 is: "; + std::copy( symmetric_difference, ptr, output ); + + int unionSet[ SIZE3 ]; + + // determine elements that are in either or both sets + ptr = std::set_union( a1, a1 + SIZE1, + a3, a3 + SIZE2, unionSet ); + + cout << "\n\nset_union of a1 and a3 is: "; + std::copy( unionSet, ptr, output ); + + cout << endl; + + return 0; + +} // end main + +/************************************************************************** + * (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 diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_36.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_36.CPP new file mode 100644 index 0000000..b742eec --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_36.CPP @@ -0,0 +1,96 @@ +// Fig. 21.36: fig21_36.cpp +// Standard library functions lower_bound, upper_bound and +// equal_range for a sorted sequence of values. +#include + +using std::cout; +using std::endl; + +#include // algorithm definitions +#include // vector class-template definition + +int main() +{ + const int SIZE = 10; + int a1[] = { 2, 2, 4, 4, 4, 6, 6, 6, 6, 8 }; + std::vector< int > v( a1, a1 + SIZE ); + std::ostream_iterator< int > output( cout, " " ); + + cout << "Vector v contains:\n"; + std::copy( v.begin(), v.end(), output ); + + // determine lower-bound insertion point for 6 in v + std::vector< int >::iterator lower; + lower = std::lower_bound( v.begin(), v.end(), 6 ); + + cout << "\n\nLower bound of 6 is element " + << ( lower - v.begin() ) << " of vector v"; + + // determine upper-bound insertion point for 6 in v + std::vector< int >::iterator upper; + upper = std::upper_bound( v.begin(), v.end(), 6 ); + + cout << "\nUpper bound of 6 is element " + << ( upper - v.begin() ) << " of vector v"; + + // use equal_range to determine both the lower- and + // upper-bound insertion points for 6 + std::pair< std::vector< int >::iterator, + std::vector< int >::iterator > eq; + eq = std::equal_range( v.begin(), v.end(), 6 ); + + cout << "\nUsing equal_range:\n" + << " Lower bound of 6 is element " + << ( eq.first - v.begin() ) << " of vector v"; + cout << "\n Upper bound of 6 is element " + << ( eq.second - v.begin() ) << " of vector v"; + + cout << "\n\nUse lower_bound to locate the first point\n" + << "at which 5 can be inserted in order"; + + // determine lower-bound insertion point for 5 in v + lower = std::lower_bound( v.begin(), v.end(), 5 ); + + cout << "\n Lower bound of 5 is element " + << ( lower - v.begin() ) << " of vector v"; + + cout << "\n\nUse upper_bound to locate the last point\n" + << "at which 7 can be inserted in order"; + + // determine upper-bound insertion point for 7 in v + upper = std::upper_bound( v.begin(), v.end(), 7 ); + + cout << "\n Upper bound of 7 is element " + << ( upper - v.begin() ) << " of vector v"; + + cout << "\n\nUse equal_range to locate the first and\n" + << "last point at which 5 can be inserted in order"; + + // use equal_range to determine both the lower- and + // upper-bound insertion points for 5 + eq = std::equal_range( v.begin(), v.end(), 5 ); + + cout << "\n Lower bound of 5 is element " + << ( eq.first - v.begin() ) << " of vector v"; + cout << "\n Upper bound of 5 is element " + << ( eq.second - v.begin() ) << " of vector v" + << endl; + + return 0; + +} // end main + +/************************************************************************** + * (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 diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_37.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_37.CPP new file mode 100644 index 0000000..496b449 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_37.CPP @@ -0,0 +1,79 @@ +// Fig. 21.37: fig21_37.cpp +// Standard library algorithms push_heap, pop_heap, +// make_heap and sort_heap. +#include + +using std::cout; +using std::endl; + +#include +#include + +int main() +{ + const int SIZE = 10; + int a[ SIZE ] = { 3, 100, 52, 77, 22, 31, 1, 98, 13, 40 }; + std::vector< int > v( a, a + SIZE ), v2; + std::ostream_iterator< int > output( cout, " " ); + + cout << "Vector v before make_heap:\n"; + std::copy( v.begin(), v.end(), output ); + + // create heap from vector v + std::make_heap( v.begin(), v.end() ); + + cout << "\nVector v after make_heap:\n"; + std::copy( v.begin(), v.end(), output ); + + // sort elements of v with sort_heap + std::sort_heap( v.begin(), v.end() ); + + cout << "\nVector v after sort_heap:\n"; + std::copy( v.begin(), v.end(), output ); + + // perform the heapsort with push_heap and pop_heap + cout << "\n\nArray a contains: "; + std::copy( a, a + SIZE, output ); + + cout << endl; + + // place elements of array a into v2 and + // maintain elements of v2 in heap + for ( int i = 0; i < SIZE; ++i ) { + v2.push_back( a[ i ] ); + std::push_heap( v2.begin(), v2.end() ); + cout << "\nv2 after push_heap(a[" << i << "]): "; + std::copy( v2.begin(), v2.end(), output ); + + } // end for + + cout << endl; + + // remove elements from heap in sorted order + for ( int j = 0; j < v2.size(); ++j ) { + cout << "\nv2 after " << v2[ 0 ] << " popped from heap\n"; + std::pop_heap( v2.begin(), v2.end() - j ); + std::copy( v2.begin(), v2.end(), output ); + + } // end for + + cout << endl; + + return 0; + +} // end main + +/************************************************************************** + * (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 diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_38.cpp b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_38.cpp new file mode 100644 index 0000000..907c83e --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_38.cpp @@ -0,0 +1,38 @@ +// Fig. 21.38: fig21_38.cpp +// Standard library algorithms min and max. +#include + +using std::cout; +using std::endl; + +#include + +int main() +{ + cout << "The minimum of 12 and 7 is: " + << std::min( 12, 7 ); + cout << "\nThe maximum of 12 and 7 is: " + << std::max( 12, 7 ); + cout << "\nThe minimum of 'G' and 'Z' is: " + << std::min( 'G', 'Z' ); + cout << "\nThe maximum of 'G' and 'Z' is: " + << std::max( 'G', 'Z' ) << endl; + + return 0; + +} // end main + +/************************************************************************** + * (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 diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_40.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_40.CPP new file mode 100644 index 0000000..e1d8a91 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_40.CPP @@ -0,0 +1,82 @@ +// Fig. 21.40: fig21_40.cpp +// Using a bitset to demonstrate the Sieve of Eratosthenes. +#include + +using std::cin; +using std::cout; +using std::endl; + +#include + +using std::setw; + +#include // bitset class definition +#include // sqrt prototype + +int main() +{ + const int size = 1024; + int value; + std::bitset< size > sieve; + + sieve.flip(); + + // perform Sieve of Eratosthenes + int finalBit = sqrt( sieve.size() ) + 1; + + for ( int i = 2; i < finalBit; ++i ) + + if ( sieve.test( i ) ) + + for ( int j = 2 * i; j < size; j += i ) + sieve.reset( j ); + + cout << "The prime numbers in the range 2 to 1023 are:\n"; + + // display prime numbers in range 2-1023 + for ( int k = 2, counter = 0; k < size; ++k ) + + if ( sieve.test( k ) ) { + cout << setw( 5 ) << k; + + if ( ++counter % 12 == 0 ) + cout << '\n'; + + } // end outer if + + cout << endl; + + // get value from user to determine whether value is prime + cout << "\nEnter a value from 1 to 1023 (-1 to end): "; + cin >> value; + + while ( value != -1 ) { + + if ( sieve[ value ] ) + cout << value << " is a prime number\n"; + else + cout << value << " is not a prime number\n"; + + cout << "\nEnter a value from 2 to 1023 (-1 to end): "; + cin >> value; + + } // end while + + return 0; + +} // end main + +/************************************************************************** + * (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 diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_42.cpp b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_42.cpp new file mode 100644 index 0000000..4dc79f8 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_42.cpp @@ -0,0 +1,86 @@ +// Fig. 21.42: fig21_42.cpp +// Demonstrating function objects. +#include + +using std::cout; +using std::endl; + +#include // vector class-template definition +#include // copy algorithm +#include // accumulate algorithm +#include // binary_function definition + +// binary function adds square of its second argument and +// running total in its first argument, then returns sum +int sumSquares( int total, int value ) +{ + return total + value * value; + +} // end function sumSquares + +// binary function class template defines overloaded operator() +// that adds suare of its second argument and running total in +// its first argument, then returns sum +template< class T > +class SumSquaresClass : public std::binary_function< T, T, T > { + +public: + + // add square of value to total and return result + const T operator()( const T &total, const T &value ) + { + return total + value * value; + + } // end function operator() + +}; // end class SumSquaresClass + +int main() +{ + const int SIZE = 10; + int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + + std::vector< int > integers( array, array + SIZE ); + + std::ostream_iterator< int > output( cout, " " ); + + int result = 0; + + cout << "vector v contains:\n"; + std::copy( integers.begin(), integers.end(), output ); + + // calculate sum of squares of elements of vector integers + // using binary function sumSquares + result = std::accumulate( integers.begin(), integers.end(), + 0, sumSquares ); + + cout << "\n\nSum of squares of elements in integers using " + << "binary\nfunction sumSquares: " << result; + + // calculate sum of squares of elements of vector integers + // using binary-function object + result = std::accumulate( integers.begin(), integers.end(), + 0, SumSquaresClass< int >() ); + + cout << "\n\nSum of squares of elements in integers using " + << "binary\nfunction object of type " + << "SumSquaresClass< int >: " << result << endl; + + return 0; + +} // end main + +/************************************************************************** + * (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 -- cgit v1.2.3