Download presentation
Presentation is loading. Please wait.
Published byFrancine Woods Modified over 5 years ago
1
Instructor: Dr. Michael Geiger Spring 2019 Lecture 6: Strings
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019 Lecture 6: Strings
2
Announcements/reminders
Program 1 due Friday, 2/8 All programs to be submitted via Blackboard Submit a single .zip file containing all files for this assignment Additional instructor support for course Grader: Shubham Tikare Office hours: Tues 9-11 AM, Ball 301E (ECE Conf Rm) Tutor: Felipe Loera Hours available on CLASS site (link also on home page) 6/12/2019 Data Structures: Lecture 6
3
Data Structures: Lecture 6
Lecture outline Review Output manipulators Other input methods C++ strings 6/12/2019 Data Structures: Lecture 6
4
Review: Output manipulators
setprecision: change number of digits displayed to the right of the decimal point fixed: fixed format (no scientific notation) showpoint: force decimal point (and trailing zeros) to be shown Default behavior (without showpoint) is to not show trailing zeros Default precision (with showpoint) is 6 Reset with noshowpoint 6/12/2019 Data Structures: Lecture 6
5
Clarifying manipulators
showpoint does not imply fixed format Could be fixed point, could be scientific notation Scientific notation used if precision < # digits before decimal point Default behavior: precision = sig. figures After showpoint: precision = digits after point Clearing fixed flag: defaultfloat There aren’t just two options … … but we’re not going to discuss the other ones 6/12/2019 Data Structures: Lecture 6
6
Review: Characters and input
Input (cin) streams Use cin to read values into variables E.g., cin >> x; Skips whitespace characters Input value must be compatible with type of x Reading n characters: cin.get(buffer, n); Reading 1 character: cin.get(ch); Reading an entire line (at most m characters): cin.getline(buffer, m) May need cin.ignore(x) to skip characters 6/12/2019 Data Structures: Lecture 6
7
Standard Library Class string
Header <string>, namespace std Basic uses: Initialization: string s1( "hi" ); Input/output (as in cout << s1) Assignment: s1 = "hi"; Can also use: Relational operators: ==, !=, >=, >, <=, < Perform char-by-char comparison using ASCII values Concatenation: += E.g.: s1 += “lly” s1 = “hilly” 6/12/2019 Data Structures: Lecture 6
8
Standard Library Class string (Cont.)
Substring member function substr s1.substr( 0, 14 ); Starts at location 0, gets 14 characters s1.substr( 15 ); Substring beginning at location 15, to the end Overloaded [] Access one character No range checking (if subscript invalid) Member function at Accesses one character Example s1.at( 10 ); Has bounds checking, throws an exception if subscript is invalid Member function empty Returns true if no characters in string Member function length Returns number of characters in string string s = “Hello”; s.length() = 5 6/12/2019 Data Structures: Lecture 6
9
Example: Strings & functions
int main() { string s1( "happy" ); string s2( " birthday" ); string s3; // test overloaded equality and relational operators cout << "s1 is \"" << s1 << "\"; s2 is \"" << s2 << "\"; s3 is \"" << s3 << '\"' << "\n\nThe results of comparing s2 and s1:" << "\ns2 == s1 yields " << ( s2 == s1 ? "true" : "false" ) << "\ns2 != s1 yields " << ( s2 != s1 ? "true" : "false" ) << "\ns2 > s1 yields " << ( s2 > s1 ? "true" : "false" ) << "\ns2 < s1 yields " << ( s2 < s1 ? "true" : "false" ) << "\ns2 >= s1 yields " << ( s2 >= s1 ? "true" : "false" ) << "\ns2 <= s1 yields " << ( s2 <= s1 ? "true" : "false" ); 6/12/2019 Data Structures: Lecture 6
10
Data Structures: Lecture 6
Example (cont.) Output from previous slide: s1 is “happy”; s2 is “ birthday”; s3 is “” The results of comparing s1 and s2: s2 == s1 yields false s2 != s1 yields true s2 > s1 yields false s2 < s1 yields true s2 >= s1 yields false s2 <= s1 yields true 6/12/2019 Data Structures: Lecture 6
11
Data Structures: Lecture 6
Example (cont.) // test string member function empty cout << "\n\nTesting s3.empty():" << endl; if ( s3.empty() ) { cout << "s3 is empty; assigning s1 to s3;" << endl; s3 = s1; // assign s1 to s3 cout << "s3 is \"" << s3 << "\""; } // end if // test overloaded string concatenation operator cout << "\n\ns1 += s2 yields s1 = "; s1 += s2; // test overloaded concatenation cout << s1; // test concatenation operator with C-style string cout << "\n\ns1 += \" to you\" yields" << endl; s1 += " to you"; cout << "s1 = " << s1 << "\n\n"; 6/12/2019 Data Structures: Lecture 6
12
Data Structures: Lecture 6
Example (cont.) Output from previous slide: Testing s3.empty(): s3 is empty; assigning s1 to s3; s3 is “happy” s1 += s2 yields s1 = happy birthday s1 += “ to you” yields s1 = happy birthday to you 6/12/2019 Data Structures: Lecture 6
13
Data Structures: Lecture 6
Example (cont.) // test string member function substr cout << "The substring of s1 starting at location 0 for\n" << "14 characters, s1.substr(0, 14), is:\n" << s1.substr( 0, 14 ) << "\n\n"; // test substr "to-end-of-string" option cout << "The substring of s1 starting at\n" << "location 15, s1.substr(15), is:\n" << s1.substr( 15 ) << endl; // test using subscript operator to create lvalue s1[ 0 ] = 'H'; s1[ 6 ] = 'B'; cout << "\ns1 after s1[0] = 'H' and s1[6] = 'B' is: " << s1 << "\n\n"; // test subscript out of range with string member function "at" cout << "Attempt to assign 'd' to s1.at( 30 ) yields:" << endl; s1.at( 30 ) = 'd'; // ERROR: subscript out of range return 0; } // end main 6/12/2019 Data Structures: Lecture 6
14
Data Structures: Lecture 6
Example (cont.) Output from previous slide: The substring of s1 starting at location 0 for 14 characters, s1.substr(0, 14), is: happy birthday The substring of s1 starting at location 15, s1.substr(15), is: to you s1 after s1[0] = ‘H’ and s1[6] = ‘B’ is: Happy Birthday to you Attempt to assign ‘d’ to s1.at(30) yields abnormal program completion 6/12/2019 Data Structures: Lecture 6
15
Data Structures: Lecture 6
Final notes Next time Algorithmic complexity intro Reminders: Program 1 due Friday, 2/8 All programs to be submitted via Blackboard Submit a single .zip file containing all files for this assignment Additional instructor support for course Grader: Shubham Tikare Office hours: Tues 9-11 AM, Ball 301E (ECE Conf Rm) Tutor: Felipe Loera Hours available on CLASS site (link also on home page) 6/12/2019 Data Structures: Lecture 6
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.