1 / 32

Chapter 19 - Class string and Stream Processing

Chapter 19 - Class string and Stream Processing. Outline 19.1 Introduction 19.2 string Assignment and Concatenation 19.3 Comparing strings 19.4 Substrings 19.5 Swapping string s 19.6 string Characteristics 19.7 Finding Characters in a string

helia
Download Presentation

Chapter 19 - Class string and Stream Processing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 19 - Class string and Stream Processing Outline 19.1 Introduction 19.2 string Assignment and Concatenation 19.3 Comparing strings 19.4 Substrings 19.5 Swapping strings 19.6 string Characteristics 19.7 Finding Characters in a string 19.8 Replacing Characters in a string 19.9 Inserting Characters into a string 19.10 Conversion to C-Style char * Strings 19.11 Iterators 19.12 String Stream Processing

  2. 19.1 Introduction • template class basic_string • typedef basic_string< char > string; • creates alias string for basic_string< char > which we use • include <string> • Initialization • string s1( "Hi"); • string s2( 8, 'x'); • string name = "Jim"; • not truly assignment, calls copy constructor WRONG: string s1 = 'c'; string s2( 'u' ); string s3 = 34; string s4( 8 ); • no automatic conversion between int or char to string

  3. 19.1 Introduction (II) • strings do not always end in '\0' • use the [] operator to access elements • first subscript 0 • string not a pointer. &string1[0] != string1 • String length - s1.size() or s1.length() • Inputting strings string myString; cin >> myString; • delimited by whitespace characters string myString; getline(cin, myString); • overloaded getline function • delimited by newline ('\n')

  4. 19.2 string Assignment and Concatenation • String assignment • = operator: s2 = s1; • assign(): s2.assign(s1); • copies s1 into s2 • s2.assign(s1, startSubscript, numberOfChars); • copies numberOfChars characters in s1 to s2, starting from startSubscript • s2[0] = 'm'; • sets first character of s2 to 'm'

  5. 19.2 string Assignment and Concatenation (II) • More string operations • s2.at(2); • range checked access • s2 += "hi" • concatenates s2 and "hi" • s2.append("_again"); • concatenates "_again" and s2 • s2.append(s1, startSubscript, numberOfChars); • concatenates numberOfChars characters in s1 with s2, starting from startSubscript

  6. 1 // Fig. 19.1: fig19_01.cpp 2 // Demonstrating string assignment and concatenation 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 #include <string> 9 10 using std::string; 11 12 int main() 13 { 14 string s1( "cat" ), s2, s3; 15 16 s2 = s1; // assign s1 to s2 with = 17 s3.assign( s1 ); // assign s1 to s3 with assign() 18 cout << "s1: " << s1 << "\ns2: " << s2 << "\ns3: " 19 << s3 << "\n\n"; 20 21 // modify s2 and s3 22 s2[ 0 ] = s3[ 2 ] = 'r'; 23 24 cout << "After modification of s2 and s3:\n" 25 << "s1: " << s1 << "\ns2: " << s2 << "\ns3: "; 26 27 // demonstrating member function at() 28 int len = s3.length(); 29 for ( int x = 0; x < len; ++x ) 30 cout << s3.at( x ); 31 32 // concatenation 1. Initialize strings 2. Modify strings 3. Print strings s1: cat s2: cat s3: cat After modification of s2 and s3: s1: cat s2: rat s3: car

  7. 33 string s4( s1 + "apult" ), s5; // declare s4 and s5 34 35 // overloaded += 36 s3 += "pet"; // create "carpet" 37 s1.append( "acomb" ); // create "catacomb" 38 39 // append subscript locations 4 thru the end of s1 to 40 // create the string "comb" (s5 was initially empty) 41 s5.append( s1, 4, s1.size() ); 42 43 cout << "\n\nAfter concatenation:\n" << "s1: " << s1 44 << "\ns2: " << s2 << "\ns3: " << s3 << "\ns4: " << s4 45 << "\ns5: " << s5 << endl; 46 47 return 0; 48 } 3. Print strings Program Output After concatenation: s1: catacomb s2: rat s3: carpet s4: catapult s5: comb s1: cat s2: cat s3: cat After modification of s2 and s3: s1: cat s2: rat s3: car After concatenation: s1: catacomb s2: rat s3: carpet s4: catapult s5: comb

  8. 19.3 Comparing strings • overloaded ==, !=, <, >, <= and >= operators • for string comparisons • return bool values • result = s1.compare(s2); • lexicographical comparison • if s1 > s2, result > 0 • if s1 < s2, result < 0 • if s1 == s2, result = 0 s1.compare(sub1, sub2, s2, sub3, sub4) compares s1 from subscript sub1 to sub2 with s2 from subscript sub3 to sub4. s1.compare(sub1, number, s2) compares number characters in s1 starting from subscript sub1 with s2.

  9. 1 // Fig. 19.2: fig19_02.cpp 2 // Demonstrating string comparison capabilities 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 #include <string> 9 10 using std::string; 11 12 int main() 13 { 14 string s1( "Testing the comparison functions." ), 15 s2("Hello" ), s3( "stinger" ), z1( s2 ); 16 17 cout << "s1: " << s1 << "\ns2: " << s2 18 << "\ns3: " << s3 << "\nz1: " << z1 << "\n\n"; 19 20 // comparing s1 and z1 21 if ( s1 == z1 ) 22 cout << "s1 == z1\n"; 23 else { // s1 != z1 24 if ( s1 > z1 ) 25 cout << "s1 > z1\n"; 26 else// s1 < z1 27 cout << "s1 < z1\n"; 28 } 29 30 // comparing s1 and s2 31 int f = s1.compare( s2 ); 32 33 if ( f == 0) 1. Initialize strings 2. Compare strings s1: Testing the comparison functions. s2: Hello s3: stinger z1: Hello s1 > z1

  10. 34 cout << "s1.compare( s2 ) == 0\n"; 35 else if ( f > 0 ) 36 cout << "s1.compare( s2 ) > 0\n"; 37 else// f < 0 38 cout << "s1.compare( s2 ) < 0\n"; 39 40 // comparing s1 (elements 2 - 5) and s3 (elements 0 - 5) 41 f = s1.compare( 2, 5, s3, 0, 5 ); 42 43 if ( f == 0 ) 44 cout << "s1.compare( 2, 5, s3, 0, 5 ) == 0\n"; 45 else if ( f > 0 ) 46 cout << "s1.compare( 2, 5, s3, 0, 5 ) > 0\n"; 47 else// f < 0 48 cout << "s1.compare( 2, 5, s3, 0, 5 ) < 0\n"; 49 50 // comparing s2 and z1 51 f = z1.compare( 0, s2.size(), s2 ); 52 53 if ( f == 0 ) 54 cout << "z1.compare( 0, s2.size(), s2 ) == 0" << endl; 55 else if ( f > 0 ) 56 cout << "z1.compare( 0, s2.size(), s2 ) > 0" << endl; 57 else// f < 0 58 cout << "z1.compare( 0, s2.size(), s2 ) < 0" << endl; 59 60 return 0; 61 } s1.compare( s2 ) > 0 2. Compare strings s1.compare( 2, 5, s3, 0, 5 ) == 0 z1.compare( 0, s2.size(), s2 ) == 0

  11. s1: Testing the comparison functions. s2: Hello s3: stinger z1: Hello s1 > z1 s1.compare( s2 ) > 0 s1.compare( 2, 5, s3, 0, 5 ) == 0 z1.compare( 0, s2.size(), s2 ) == 0 Program Output

  12. 19.4 Substrings • Function substr retrieves a substring from a string. s1.substr( startSubScript, number ); • startSubscript - starting subscript • number - number of characters to extract • returns the substring

  13. 19.5 Swapping strings • Function swap swaps the contents of two strings. s1.swap(s2); • switches s1 and s2

  14. 19.6 string Characteristics • s1.size() and s1.length() • current number of characters in string • s1.capacity() • number of elements that can be stored without reallocation • s1.max_size() • maximum possible string size • str.empty() • if no characters, returnstrue • s1.resize(newlength) • resizes string to newlength

  15. 1 // Fig. 19.5: fig19_05.cpp 2 // Demonstrating functions related to size and capacity 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 using std::cin; 8 9 #include <string> 10 11 using std::string; 12 13 void printStats( const string & ); 14 15 int main() 16 { 17 string s; 18 19 cout << "Stats before input:\n"; 20 printStats( s ); 21 22 cout << "\n\nEnter a string: "; 23 cin >> s; // delimited by whitespace 24 cout << "The string entered was: " << s; 25 26 cout << "\nStats after input:\n"; 27 printStats( s ); 28 29 s.resize( s.length() + 10 ); 30 cout << "\n\nStats after resizing by (length + 10):\n"; 31 printStats( s ); 32 33 cout << endl; 1. Initialize string 2. Function calls 3. Output Stats before input: capacity: 0 max size: 4294967293 size: 0 length: 0 empty: true Stats after input: capacity: 31 max size: 4294967293 size: 6 length: 6 empty: false Enter a string: tomato soup The string entered was: tomato Stats after resizing by (length + 10): capacity: 31 max size: 4294967293 size: 16 length: 16 empty: false

  16. 34 return 0; 35 } 36 37 void printStats( const string &str ) 38 { 39 cout << "capacity: " << str.capacity() 40 << "\nmax size: " << str.max_size() 41 << "\nsize: " << str.size() 42 << "\nlength: " << str.length() 43 << "\nempty: " << ( str.empty() ? "true": "false" ); 44 } 3.1 Function definition Program Output Stats before input: capacity: 0 max size: 4294967293 size: 0 length: 0 empty: true Enter a string: tomato soup The string entered was: tomato Stats after input: capacity: 31 max size: 4294967293 size: 6 length: 6 empty: false Stats after resizing by (length + 10): capacity: 31 max size: 4294967293 size: 16 length: 16 empty: false

  17. 19.7 Finding Characters in a string • Class string find functions: • if found, subscript of match returned • if not found, value of string::nposis returned • s1.find(string) - searches s1 for string • s1.rfind(string) - like find, but searches s1 for string backwards. • s1.find_first_of(string) - finds first occurrence in s1 of any character in string • s1.find_last_of(string)- as above, but last occurrence. Searches s1 backwards. • s1.find_first_not_of(string)- finds first character in s1 NOT in s2. • s1.find_last_not_of(string) - as above, but last character.Searches s1 backwards.

  18. 1 // Fig. 19.6: fig19_06.cpp 2 // Demonstrating the string find functions 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 #include <string> 9 10 using std::string; 11 12 int main() 13 { 14 // compiler concatenates all parts into one string literal 15 string s( "The values in any left subtree" 16 "\nare less than the value in the" 17 "\nparent node and the values in" 18 "\nany right subtree are greater" 19 "\nthan the value in the parent node" ); 20 21 // find "subtree" at locations 23 and 102 22 cout << "Original string:\n" << s 23 << "\n\n(find) \"subtree\" was found at: " 24 << s.find( "subtree" ) 25 << "\n(rfind) \"subtree\" was found at: " 26 << s.rfind( "subtree" ); 27 28 // find 'p' in parent at locations 62 and 144 29 cout << "\n(find_first_of) character from \"qpxz\" at: " 30 << s.find_first_of( "qpxz" ) 31 << "\n(find_last_of) character from \"qpxz\" at: " 32 << s.find_last_of( "qpxz" ); 33 Original string: The values in any left subtree are less than the value in the parent node and the values in any right subtree are greater than the value in the parent node 1. Initialize string 2. Function calls 3. Output (find) "subtree" was found at: 23 (rfind) "subtree" was found at: 102 (find_first_of) character from "qpxz" at: 62 (find_last_of) character from "qpxz" at: 144

  19. 34 // find 'b' at location 25 35 cout << "\n(find_first_not_of) first character not\n" 36 << " contained in \"heTv lusinodrpayft\": " 37 << s.find_first_not_of( "heTv lusinodrpayft" ); 38 39 // find '\n' at location 121 40 cout << "\n(find_last_not_of) first character not\n" 41 << " contained in \"heTv lusinodrpayft\": " 42 << s.find_last_not_of( "heTv lusinodrpayft" ) << endl; 43 44 return 0; 45 } 3. Output Program Output (find_first_not_of) first character not contained in "heTv lusinodrpayft": 25 (find_last_not_of) first character not contained in "heTv lusinodrpayft": 121 Original string: The values in any left subtree are less than the value in the parent node and the values in any right subtree are greater than the value in the parent node (find) "subtree" was found at: 23 (rfind) "subtree" was found at: 102 (find_first_of) character from "qpxz" at: 62 (find_last_of) character from "qpxz" at: 144 (find_first_not_of) first character not contained in "heTv lusinodrpayft": 25 (find_last_not_of) first character not contained in "heTv lusinodrpayft": 121

  20. 19.8 Replacing Characters in a string • s1.erase(number); • erases everything after element number to the end • s1.replace(sub1, numberToReplace, replacementString, sub2, numberToUse); • sub1 - subscript in s1 where replacement occurs • numberToReplace - number of characters being replaced ins1, starting from sub1 • replacementString - string containing replacement characters • sub2 - beginning subscript of replacement characters in replacementString • numberToUse - number of replacement characters to use in replacementString, starting with sub2

  21. 1 // Fig. 19.7: fig19_07.cpp 2 // Demonstrating functions erase and replace 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; Remove all characters from location 62 (the 63rd element) to the end. 7 8 #include <string> 9 10 using std::string; 11 12 int main() 13 { 14 // compiler concatenates all parts into one string 15 string s( "The values in any left subtree" 16 "\nare less than the value in the" 17 "\nparent node and the values in" 18 "\nany right subtree are greater" 19 "\nthan the value in the parent node" ); 20 21 // remove all characters from location 62 22 // through the end of s 23 s.erase( 62 ); 24 25 // output the new string 26 cout << "Original string after erase:\n" << s 27 << "\n\nAfter first replacement:\n"; 28 29 // replace all spaces with a period 30 int x = s.find( " " ); 31 while ( x < string::npos ) { 32 s.replace( x, 1, "." ); 33 x = s.find( " ", x + 1 ); 1. Initialize string 2. Function calls 3. Output Original string after erase: The values in any left subtree are less than the value in the After first replacement: The.values.in.any.left.subtree are.less.than.the.value.in.the

  22. 34 } 35 36 cout << s << "\n\nAfter second replacement:\n"; 37 38 // replace all periods with two semicolons 39 // NOTE: this will overwrite characters 40 x = s.find( "." ); 41 while ( x < string::npos ) { 42 s.replace( x, 2, "xxxxx;;yyy", 5, 2 ); 43 x = s.find( ".", x + 1 ); 44 } 45 46 cout << s << endl; 47 return 0; 48 } 3. Output Program Output After second replacement: The;;alues;;n;;ny;;eft;;ubtree are;;ess;;han;;he;;alue;;n;;he Original string after erase: The values in any left subtree are less than the value in the After first replacement: The.values.in.any.left.subtree are.less.than.the.value.in.the After second replacement: The;;alues;;n;;ny;;eft;;ubtree are;;ess;;han;;he;;alue;;n;;he

  23. 19.9 Inserting Characters into a string • s1.insert(sub1, string) • inserts string before element sub1 in s1 • s1.insert(sub1, string, sub2, numChars); • selects numChars characters, starting from sub2 in string,and inserts them into s1.

  24. 19.10 Conversion to C-Style char * Strings • strings can be converted to C-style char * strings • s1.data() - conversion to const char * string, not null terminated • s1.c_str() - conversion to const char * string, null terminated (ends with '\0')

  25. 1 // Fig. 19.9: fig19_09.cpp 2 // Converting to C-style strings. Notice the format for inserting strings. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 #include <string> 9 10 using std::string; 11 12 int main() 13 { 14 string s( "STRINGS" ); 15 const char *ptr1 = 0; 16 int len = s.length(); 17 char *ptr2 = new char[ len + 1 ]; // including null 18 19 // copy characters out of string into allocated memory 20 s.copy( ptr2, len, 0 ); 21 ptr2[ len ] = 0; // add null terminator 22 23 // output 24 cout << "string s is " << s 25 << "\ns converted to a C-Style string is " 26 << s.c_str() << "\nptr1 is "; 27 28 // Assign to pointer ptr1 the const char * returned by 29 // function data(). NOTE: this is a potentially dangerous 30 // assignment. If the string is modified, the pointer 31 // ptr1 can become invalid. 32 ptr1 = s.data(); 33 1. Initialize strings 2. Insert strings 3. Print results. string s is STRINGS s converted to a C-Style string is STRINGS ptr1 is STRINGS

  26. 34 for ( int k = 0; k < len; ++k ) 35 cout << *( ptr1 + k ); // use pointer arithmetic 36 37 cout << "\nptr2 is " << ptr2 << endl; 38 delete [] ptr2; 39 return 0; 40 } 3. Print results. Program Output ptr2 is STRINGS string s is STRINGS s converted to a C-Style string is STRINGS ptr1 is STRINGS ptr2 is STRINGS

  27. 19.11 Iterators • iterators • forwards and backwards traversal of strings • access to individual characters • similar to pointer operations • Basic coverage here - more next chapter • type const_iterator - iterator that cannot modify the string • s.begin() - returns iterator to front of string • s.end() - returns iterator to end • *myIterator - dereferences iterator (like pointer) • ++myIterator - advances iterator one position (character)

  28. 19.12 String Stream Processing • in-memory I/O (string stream processing) • headers <sstream> and <iostream> • Inputting strings to memory • create objects of class istringstream istringstream myInputString; • works the same as input from a file • can feed input string into other strings • Output to a string • objects of class ostringstream ostringstream myOutputString; - myOutputString.str() - outputs the string

  29. 1 // Fig. 19.11: fig19_11.cpp 2 // Using a dynamically allocated ostringstream object. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 output strings to outputString 8 #include <string> 9 10 using std::string; 11 Add more characters to outputString 12 #include <sstream> 13 14 using std::ostringstream; 15 16 int main() 17 { 18 ostringstream outputString; 19 string s1( "Output of several data types " ), 20 s2( "to an ostringstream object:" ), 21 s3( "\n double: " ), 22 s4( "\n int: " ), 23 s5( "\naddress of int: " ); 24 double d = 123.4567; 25 int i = 22; 26 27 outputString << s1 << s2 << s3 << d << s4 << i << s5 << &i; 28 cout << "outputString contains:\n" << outputString.str(); 29 30 outputString << "\nmore characters added"; 1. Initialize ostringstream object 1.1 Initialize strings 1.2 Initialize variables 2. Print outputString 2.1 Modify outputString outputString contains: Output of several data types to an ostringstream object: double: 123.457 int: 22 address of int: 0068FD0C

  30. 31 cout << "\n\nafter additional stream insertions,\n" 32 << "outputString contains:\n" << outputString.str() 33 << endl; 34 35 return 0; 36 } 2.2 Print outputString Program Output after additional stream insertions, outputString contains: Output of several data types to an ostringstream object: double: 123.457 int: 22 address of int: 0068FD0C more characters added outputString contains: Output of several data types to an ostringstream object: double: 123.457 int: 22 address of int: 0068FD0C after additional stream insertions, outputString contains: Output of several data types to an ostringstream object: double: 123.457 int: 22 address of int: 0068FD0C more characters added

  31. 1 // Fig. 19.12: fig19_12.cpp 2 // Demonstrating input from an istringstream object. 3 #include <iostream> 4 5 using std::cout; Notice how inputString inputs data to string1 and string2 (similar format as cin) 6 using std::endl; 7 8 #include <string> 9 10 using std::string; 11 12 #include <sstream> 13 14 using std::istringstream; 15 16 int main() 17 { 18 string input( "Input test 123 4.7 A" ); 19 istringstream inputString( input ); 20 string string1, string2; 21 int i; 22 double d; 23 char c; 24 25 inputString >> string1 >> string2 >> i >> d >> c; 26 27 cout << "The following items were extracted\n" 28 << "from the istringstream object:" 29 << "\nstring: " << string1 30 << "\nstring: " << string2 31 << "\n int: " << i 32 << "\ndouble: " << d 33 << "\n char: " << c; 1. Initialize objects 2. Input data into string1 and string2 The following items were extracted from the istringstream object: String: Input String: test int: 123 double: 4.7 char: A

  32. 34 35 // attempt to read from empty stream 36 long x; 37 38 inputString >> x; 39 40 if ( inputString.good() ) 41 cout << "\n\nlong value is: " << x << endl; 42 else 43 cout << "\n\ninputString is empty" << endl; 44 45 return 0; 46 } 2. Function calls Program Output inputString is empty The following items were extracted from the istringstream object: String: Input String: test int: 123 double: 4.7 char: A inputString is empty

More Related