diff options
| author | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
|---|---|---|
| committer | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
| commit | 33613a85afc4b1481367fbe92a17ee59c240250b (patch) | |
| tree | 670b842326116b376b505ec2263878912fca97e2 /Bachelor/Prog2/Codebeispiele/3_ch15 | |
| download | Studium-master.tar.gz Studium-master.tar.bz2 | |
Diffstat (limited to 'Bachelor/Prog2/Codebeispiele/3_ch15')
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_01.CPP | 68 | ||||
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_02.CPP | 97 | ||||
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_03.CPP | 38 | ||||
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_04.CPP | 43 | ||||
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_05.CPP | 75 | ||||
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_06.CPP | 71 | ||||
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_07.CPP | 69 | ||||
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_08.CPP | 50 | ||||
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_09.CPP | 57 | ||||
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_10.CPP | 44 | ||||
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/3_ch15/Fig15_11.cpp | 59 | ||||
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/3_ch15/Fig15_12.cpp | 65 |
12 files changed, 736 insertions, 0 deletions
diff --git a/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_01.CPP b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_01.CPP new file mode 100644 index 0000000..2c69128 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_01.CPP @@ -0,0 +1,68 @@ +// Fig. 16.1: fig16_01.cpp
+// Demonstrating string assignment and concatenation.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include <string>
+
+using std::string;
+
+int main()
+{
+ string string1( "cat" );
+ string string2;
+ string string3;
+
+ string2 = string1; // assign string1 to string2
+ string3.assign( string1 ); // assign string1 to string3
+ cout << "string1: " << string1 << "\nstring2: " << string2
+ << "\nstring3: " << string3 << "\n\n";
+
+ // modify string2 and string3
+ string2[ 0 ] = string3[ 2 ] = 'r';
+
+ cout << "After modification of string2 and string3:\n"
+ << "string1: " << string1 << "\nstring2: " << string2
+ << "\nstring3: ";
+
+ // demonstrating member function at
+ for ( int i = 0; i < string3.length(); i++ )
+ cout << string3.at( i );
+
+ // declare string4 and string5
+ string string4( string1 + "apult" );
+ string string5;
+
+ // overloaded +=
+ string3 += "pet"; // create "carpet"
+ string1.append( "acomb" ); // create "catacomb"
+
+ // append subscript locations 4 through end of string1 to
+ // create string "comb" (string5 was initially empty)
+ string5.append( string1, 4, string1.length() );
+
+ cout << "\n\nAfter concatenation:\nstring1: " << string1
+ << "\nstring2: " << string2 << "\nstring3: "
+ << string3 << "\nstring4: " << string4
+ << "\nstring5: " << string5 << 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/3_ch15/FIG15_02.CPP b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_02.CPP new file mode 100644 index 0000000..37fa669 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_02.CPP @@ -0,0 +1,97 @@ +// Fig. 16.2: fig16_02.cpp
+// Demonstrating string comparison capabilities.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include <string>
+
+using std::string;
+
+int main()
+{
+ string string1( "Testing the comparison functions." );
+ string string2( "Hello" );
+ string string3( "stinger" );
+ string string4( string2 );
+
+ cout << "string1: " << string1 << "\nstring2: " << string2
+ << "\nstring3: " << string3 << "\nstring4: " << string4
+ << "\n\n";
+
+ // comparing string1 and string4
+ if ( string1 == string4 )
+ cout << "string1 == string4\n";
+ else { // string1 != string4
+ if ( string1 > string4 )
+ cout << "string1 > string4\n";
+ else // string1 < string4
+ cout << "string1 < string4\n";
+ }
+
+ // comparing string1 and string2
+ int result = string1.compare( string2 );
+
+ if ( result == 0 )
+ cout << "string1.compare( string2 ) == 0\n";
+ else // result != 0
+ if ( result > 0 )
+ cout << "string1.compare( string2 ) > 0\n";
+ else // result < 0
+ cout << "string1.compare( string2 ) < 0\n";
+
+ // comparing string1 (elements 2-5) and string3 (elements 0-5)
+ result = string1.compare( 2, 5, string3, 0, 5 );
+
+ if ( result == 0 )
+ cout << "string1.compare( 2, 5, string3, 0, 5 ) == 0\n";
+ else // result != 0
+ if ( result > 0 )
+ cout << "string1.compare( 2, 5, string3, 0, 5 ) > 0\n";
+ else // result < 0
+ cout << "string1.compare( 2, 5, string3, 0, 5 ) < 0\n";
+
+ // comparing string2 and string4
+ result = string4.compare( 0, string2.length(), string2 );
+
+ if ( result == 0 )
+ cout << "string4.compare( 0, string2.length(), "
+ << "string2 ) == 0" << endl;
+ else // result != 0
+ if ( result > 0 )
+ cout << "string4.compare( 0, string2.length(), "
+ << "string2 ) > 0" << endl;
+ else // result < 0
+ cout << "string4.compare( 0, string2.length(), "
+ << "string2 ) < 0" << endl;
+
+ // comparing string2 and string4
+ result = string2.compare( 0, 3, string4 );
+
+ if ( result == 0 )
+ cout << "string2.compare( 0, 3, string4 ) == 0" << endl;
+ else // result != 0
+ if ( result > 0 )
+ cout << "string2.compare( 0, 3, string4 ) > 0" << endl;
+ else // result < 0
+ cout << "string2.compare( 0, 3, string4 ) < 0" << 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/3_ch15/FIG15_03.CPP b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_03.CPP new file mode 100644 index 0000000..40fd024 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_03.CPP @@ -0,0 +1,38 @@ +// Fig. 16.3: fig16_03.cpp
+// Demonstrating string member function substr.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include <string>
+
+using std::string;
+
+int main()
+{
+ string string1( "The airplane landed on time." );
+
+ // retrieve substring "plane" which
+ // begins at subscript 7 and consists of 5 elements
+ cout << string1.substr( 7, 5 ) << 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/3_ch15/FIG15_04.CPP b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_04.CPP new file mode 100644 index 0000000..0747743 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_04.CPP @@ -0,0 +1,43 @@ +// Fig. 16.4: fig16_04.cpp
+// Using the swap function to swap two strings.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include <string>
+
+using std::string;
+
+int main()
+{
+ string first( "one" );
+ string second( "two" );
+
+ // output strings
+ cout << "Before swap:\n first: " << first
+ << "\nsecond: " << second;
+
+ first.swap( second ); // swap strings
+
+ cout << "\n\nAfter swap:\n first: " << first
+ << "\nsecond: " << second << 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/3_ch15/FIG15_05.CPP b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_05.CPP new file mode 100644 index 0000000..6fc20c4 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_05.CPP @@ -0,0 +1,75 @@ +// Fig. 16.5: fig16_05.cpp
+// Demonstrating member functions related to size and capacity.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+using std::cin;
+using std::boolalpha;
+
+#include <string>
+
+using std::string;
+
+void printStatistics( const string & );
+
+int main()
+{
+ string string1;
+
+ cout << "Statistics before input:\n" << boolalpha;
+ printStatistics( string1 );
+
+ // read in "tomato"
+ cout << "\n\nEnter a string: ";
+ cin >> string1; // delimited by whitespace
+ cout << "The string entered was: " << string1;
+
+ cout << "\nStatistics after input:\n";
+ printStatistics( string1 );
+
+ // read in "soup"
+ cin >> string1; // delimited by whitespace
+ cout << "\n\nThe remaining string is: " << string1 << endl;
+ printStatistics( string1 );
+
+ // append 46 characters to string1
+ string1 += "1234567890abcdefghijklmnopqrstuvwxyz1234567890";
+ cout << "\n\nstring1 is now: " << string1 << endl;
+ printStatistics( string1 );
+
+ // add 10 elements to string1
+ string1.resize( string1.length() + 10 );
+ cout << "\n\nStats after resizing by (length + 10):\n";
+ printStatistics( string1 );
+
+ cout << endl;
+ return 0;
+
+} // end main
+
+// display string statistics
+void printStatistics( const string &stringRef )
+{
+ cout << "capacity: " << stringRef.capacity()
+ << "\nmax size: " << stringRef.max_size()
+ << "\nsize: " << stringRef.size()
+ << "\nlength: " << stringRef.length()
+ << "\nempty: " << stringRef.empty();
+
+} // end printStatistics
+
+/**************************************************************************
+ * (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/3_ch15/FIG15_06.CPP b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_06.CPP new file mode 100644 index 0000000..10a73aa --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_06.CPP @@ -0,0 +1,71 @@ +// Fig. 16.6: fig16_06.cpp
+// Demonstrating the string find member functions.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include <string>
+
+using std::string;
+
+int main()
+{
+ string string1( "noon is 12 p.m." );
+ int location;
+
+ // find "is" at location 5
+ cout << "Original string:\n" << string1
+ << "\n\n(find) \"is\" was found at: "
+ << string1.find( "is" )
+ << "\n(rfind) \"is\" was found at: "
+ << string1.rfind( "is" );
+
+ // find 'o' at location 1
+ location = string1.find_first_of( "misop" );
+
+ cout << "\n\n(find_first_of) found '" << string1[ location ]
+ << "' from the group \"misop\" at: "
+ << location;
+
+ // find 'm' at location 13
+ location = string1.find_last_of( "misop" );
+ cout << "\n\n(find_last_of) found '" << string1[ location ]
+ << "' from the group \"misop\" at: "
+ << location;
+
+ // find '1' at location 8
+ location = string1.find_first_not_of( "noi spm" );
+ cout << "\n\n(find_first_not_of) '" << string1[ location ]
+ << "' is not contained in \"noi spm\" and was found at:"
+ << location;
+
+ // find '.' at location 12
+ location = string1.find_first_not_of( "12noi spm" );
+ cout << "\n\n(find_first_not_of) '" << string1[ location ]
+ << "' is not contained in \"12noi spm\" and was "
+ << "found at:" << location << endl;
+
+ // search for characters not in string1
+ location = string1.find_first_not_of( "noon is 12 p.m." );
+ cout << "\nfind_first_not_of(\"noon is 12 p.m.\")"
+ << " returned: " << location << 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/3_ch15/FIG15_07.CPP b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_07.CPP new file mode 100644 index 0000000..c09a04f --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_07.CPP @@ -0,0 +1,69 @@ +// Fig. 16.7: fig16_07.cpp
+// Demonstrating string member functions erase and replace.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include <string>
+
+using std::string;
+
+int main()
+{
+ // compiler concatenates all parts into one string
+ string string1( "The values in any left subtree"
+ "\nare less than the value in the"
+ "\nparent node and the values in"
+ "\nany right subtree are greater"
+ "\nthan the value in the parent node" );
+
+ cout << "Original string:\n" << string1 << endl << endl;
+
+ // remove all characters from (and including) location 62
+ // through the end of string1
+ string1.erase( 62 );
+
+ // output new string
+ cout << "Original string after erase:\n" << string1
+ << "\n\nAfter first replacement:\n";
+
+ // replace all spaces with period
+ int position = string1.find( " " );
+
+ while ( position != string::npos ) {
+ string1.replace( position, 1, "." );
+ position = string1.find( " ", position + 1 );
+ } // end while
+
+ cout << string1 << "\n\nAfter second replacement:\n";
+
+ // replace all periods with two semicolons
+ // NOTE: this will overwrite characters
+ position = string1.find( "." );
+
+ while ( position != string::npos ) {
+ string1.replace( position, 2, "xxxxx;;yyy", 5, 2 );
+ position = string1.find( ".", position + 1 );
+ } // end while
+
+ cout << string1 << 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/3_ch15/FIG15_08.CPP b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_08.CPP new file mode 100644 index 0000000..98bc3d9 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_08.CPP @@ -0,0 +1,50 @@ +// Fig. 16.8: fig16_08.cpp
+// Demonstrating class string insert member functions.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include <string>
+
+using std::string;
+
+int main()
+{
+ string string1( "beginning end" );
+ string string2( "middle " );
+ string string3( "12345678" );
+ string string4( "xx" );
+
+ cout << "Initial strings:\nstring1: " << string1
+ << "\nstring2: " << string2 << "\nstring3: " << string3
+ << "\nstring4: " << string4 << "\n\n";
+
+ // insert "middle" at location 10 in string1
+ string1.insert( 10, string2 );
+
+ // insert "xx" at location 3 in string3
+ string3.insert( 3, string4, 0, string::npos );
+
+ cout << "Strings after insert:\nstring1: " << string1
+ << "\nstring2: " << string2 << "\nstring3: " << string3
+ << "\nstring4: " << string4 << 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/3_ch15/FIG15_09.CPP b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_09.CPP new file mode 100644 index 0000000..d94ff92 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_09.CPP @@ -0,0 +1,57 @@ +// Fig. 16.9: fig16_09.cpp
+// Converting to C-style strings.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include <string>
+
+using std::string;
+
+int main()
+{
+ string string1( "STRINGS" );
+ const char *ptr1 = 0;
+ int length = string1.length();
+ char *ptr2 = new char[ length + 1 ]; // including null
+
+ // copy characters from string1 into allocated memory
+ string1.copy( ptr2, length, 0 );
+ ptr2[ length ] = '\0'; // add null terminator
+
+ // output
+ cout << "string s is " << string1
+ << "\nstring1 converted to a C-Style string is "
+ << string1.c_str() << "\nptr1 is ";
+
+ // Assign to pointer ptr1 the const char * returned by
+ // function data(). NOTE: this is a potentially dangerous
+ // assignment. If string1 is modified, pointer ptr1 can
+ // become invalid.
+ ptr1 = string1.data();
+
+ // output each character using pointer
+ for ( int i = 0; i < length; i++ )
+ cout << *( ptr1 + i ); // use pointer arithmetic
+
+ cout << "\nptr2 is " << ptr2 << endl;
+ delete [] ptr2;
+ 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/3_ch15/FIG15_10.CPP b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_10.CPP new file mode 100644 index 0000000..c65136e --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_10.CPP @@ -0,0 +1,44 @@ +// Fig. 16.10: fig16_10.cpp
+// Using an iterator to output a string.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include <string>
+
+using std::string;
+
+int main()
+{
+ string string1( "Testing iterators" );
+ string::const_iterator iterator1 = string1.begin();
+
+ cout << "string1 = " << string1
+ << "\n(Using iterator iterator1) string1 is: ";
+
+ // iterate through string
+ while ( iterator1 != string1.end() ) {
+ cout << *iterator1; // dereference iterator to get char
+ ++iterator1; // advance iterator to next char
+ } // 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. *
+ *************************************************************************/
diff --git a/Bachelor/Prog2/Codebeispiele/3_ch15/Fig15_11.cpp b/Bachelor/Prog2/Codebeispiele/3_ch15/Fig15_11.cpp new file mode 100644 index 0000000..1b55f71 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/3_ch15/Fig15_11.cpp @@ -0,0 +1,59 @@ +// Fig. 16.11: fig16_11.cpp
+// Using a dynamically allocated ostringstream object.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include <string>
+
+using std::string;
+
+#include <sstream>
+
+using std::ostringstream;
+
+int main()
+{
+ ostringstream outputString; // create ostringstream instance
+
+ string string1( "Output of several data types " );
+ string string2( "to an ostringstream object:" );
+ string string3( "\n double: " );
+ string string4( "\n int: " );
+ string string5( "\naddress of int: " );
+
+ double double1 = 123.4567;
+ int integer = 22;
+
+ // output strings, double and int to outputString
+ outputString << string1 << string2 << string3 << double1
+ << string4 << integer << string5 << &integer;
+
+ // call str to output contents
+ cout << "outputString contains:\n" << outputString.str();
+
+ // add additional characters and call str to output string
+ outputString << "\nmore characters added";
+ cout << "\n\nafter additional stream insertions,\n"
+ << "outputString contains:\n" << outputString.str()
+ << 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/3_ch15/Fig15_12.cpp b/Bachelor/Prog2/Codebeispiele/3_ch15/Fig15_12.cpp new file mode 100644 index 0000000..93bb65b --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/3_ch15/Fig15_12.cpp @@ -0,0 +1,65 @@ +// Fig. 16.12: fig16_12.cpp
+// Demonstrating input from an istringstream object.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include <string>
+
+using std::string;
+
+#include <sstream>
+
+using std::istringstream;
+
+int main()
+{
+ string input( "Input test 123 4.7 A" );
+ istringstream inputString( input );
+ string string1;
+ string string2;
+ int integer;
+ double double1;
+ char character;
+
+ inputString >> string1 >> string2 >> integer >> double1
+ >> character;
+
+ cout << "The following items were extracted\n"
+ << "from the istringstream object:"
+ << "\nstring: " << string1
+ << "\nstring: " << string2
+ << "\n int: " << integer
+ << "\ndouble: " << double1
+ << "\n char: " << character;
+
+ // attempt to read from empty stream
+ long value;
+
+ inputString >> value;
+
+ // test stream results
+ if ( inputString.good() )
+ cout << "\n\nlong value is: " << value << endl;
+ else
+ cout << "\n\ninputString is empty" << 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. *
+ *************************************************************************/
|
