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_15.CPP | 86 ++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_15.CPP (limited to 'Bachelor/Prog2/Codebeispiele/7_ch21/fig21_15.CPP') 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 -- cgit v1.2.3