240 likes | 328 Views
28-Jun-2006. Today’s Objectives. Announcements Midterm Exam answers will be posted on the discussion group page Review Quiz #2 and Midterm Exam Class string and String Stream Processing (Ch. 18) String assignment and concatenation Comparing strings Substrings
E N D
28-Jun-2006 Today’s Objectives • Announcements • Midterm Exam answers will be posted on the discussion group page • Review Quiz #2 and Midterm Exam • Class string and String Stream Processing (Ch. 18) • String assignment and concatenation • Comparing strings • Substrings • Finding and replacing characters in a string • Conversion to C-Style strings • String stream processing • Getting input from the keyboard with embedded white space • Demo – A Simple Library Application (Continued)
Progress Review So far, we have learned… • Object-Oriented Programming • Basics of C++ • Functions and argument passing • Arrays • Pointers and dynamic memory allocation • C++ classes and objects • Some C++ standard libraries and their namespace • The STL vector class • Programs that use objects to solve problems • Debugging techniques • Object-Oriented Design • A simple software process • Pseudocode algorithms • UML class diagrams and use case diagrams
Progress Review Next… • The string class and the stringstream class • Operator overloading • Inheritance • Polymorphism • Templates • Stream I/O • Exception handling • File processing • Linked lists • The g++ compiler
Class string andString Stream Processing Chapter 18
Class string and String Stream Processing (Deitel, 884–906) Class string • C++ string class is an object-oriented approach to strings • #include <string>using std::string; • Creating a string object string s1; string s2("Test"); string s3 = "Test"; • Finding the length int len = s2.length(); //len is 4
Class string and String Stream Processing (Deitel, 884–906) String Assignment • Assigning string s1, s2 = "Test"; s1 = s2; s1.assign(s2); //Same as s1 = s2 • Accessing individual chars s2[0] = 'R'; //Changed to "Rest" s2.at(0) = 'B'; //Changed to "Best"
Class string and String Stream Processing (Deitel, 884–906) String Concatenation • Use operator + with strings and chars string s1 = "Hello"; string s2 = "World"; s1 += ", "; string s3 = s1 + s2 + '!'; • Use the append function string s4; s4.append( s3, 0, 5 ); cout << s4; //Prints "Hello"
Class string and String Stream Processing (Deitel, 884–906) Comparing strings • C++ strings can be compared with ==, !=, <, >, <=, and >= string s1 = "Hello", s2 = "World"; if( s1 == s2 ) cout << "equal"; • Also with the compare function int result = s1.compare(s2); • result is 0 if s1 is equal to s2 • result is a positive number if s1 is lexicographically greater than s2 • result is a negative number if s1 is lexicographically less than s2 • Comparing substrings int result = s1.compare( 0, 1, s2, 0, 1 ); • This example compares the first char of s1 to the first char of s2
Class string and String Stream Processing (Deitel, 884–906) Substrings • Use the substr function to extract a substring string s1 = "Hello, World!"; string s2 = s1.substr( 0, 5 ); Index of the starting char Length of the substring
Class string and String Stream Processing (Deitel, 884–906) Finding and Replacing chars • Using find() string s1 = "It was the worst of times."; int pos = s1.find("worst"); if( pos == string::npos ) cout << "Not found"; else cout << "Found at position " << pos; • Using replace() s1.replace( pos, 5, "best" ); Replacement string Index of the starting char Number of chars to replace
Class string and String Stream Processing (Deitel, 884–906) Conversion to C-Style Strings • Use the c_str function string s1 = "Hello"; const char *ps1 = s1.c_str();
Class string and String Stream Processing (Deitel, 884–906) String Stream Processing • Input from strings – ostringstream class • Output to strings – istringstream class • Both input and output – stringstream class • #include <sstream>using namespace std; • Member function that returns a string stringstream ss; ss.str();
Class string and String Stream Processing (Deitel, 884–906) Applications of String Streams • Use << to insert data to be printed to an ostringstream object, and then print it later, Fig. 18.11 page 903 • Example: convert int or double data to a string int i = 42; stringstream ss; ss << i; string s = ss.str();
Using cin (Lippman) Get a String Containing Whitespace cout << "Enter your first and last name: "; const int BUFFSIZE = 1024; char buffer[BUFFSIZE]; cin.getline(buffer,BUFFSIZE); cout << buffer << endl; //Or create a string string s = string(buffer); Gets input from the keyboard up to the \n, and puts this input into the char array, including the whitespace
Using cin Potential Problem char input; cout << "Enter your selection: "; cin >> input; cin.ignore();//Sometimes needed if you use cin first cout << "Enter the title: "; const int BUFFSIZE = 1024; char buffer[BUFFSIZE]; cin.getline( buffer, BUFFSIZE ); string title = string(buffer);
Checking for errors with cin Problem:User Enters Wrong Datatype int input; cout << "Enter the number of copies: "; cin >> input; while( !cin ){ cin.clear(); cin.ignore(); cout << "That was not an integer, try again: "; cin >> input; } If the user enters a char here, then the value of “input” will be unusable
Checking for errors with cin Problem:User Enters Wrong Datatype int input; cout << "Enter the number of copies: "; cin >> input; while( !cin ){ cin.clear(); cin.ignore(); cout << "That was not an integer, try again: "; cin >> input; } We test whether the user entered the wrong datatype, with “!cin”. If “!cin” is true, then we have to clear the cin object and ignore the next byte.
Demo A Simple Library Application(Continued)
Library Demo • Progress review • Implemented two classes in the first version • Book class • BookList class • TODO • Implement the Library class
References Deitel, H. M., and P. J. Deitel, C++ How to Program, Fifth Edition. Upper Saddle River, NJ: Prentice Hall, 2005.