Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.

Similar presentations


Presentation on theme: "ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings."— Presentation transcript:

1 ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings

2 Lecture outline Announcements / reminders  Project groups posted  Lab 6 posted  Microsoft Visio Manual for UML modeling posted Today  Review: Arrays & vectors  Other container classes  Strings 2/26/2016 ECE 264: Lecture 20 2

3 Review: Arrays Constant size list of items of same type Can initialize using comma-separated list:  int n[] = {10, 20, 30, 40, 50}; Can access individual elements using []  cout << n[1]; would print 20 Can pass arrays to functions  void printArray(int arr[], int size); Pitfalls  Indexing past array boundaries  Array name is a pointer  passed by reference 2/26/2016 ECE 264: Lecture 20 3

4 Review: Vectors Vectors allow programmer to create “arrays” that:  Are dynamically resizable  Can be assigned to one another  Can be compared for equality  Contain easier generic boundary checking Can access vectors like arrays: v[0]  Can also use vector functions Examples: vector list; //empty vector vector wordList(n); //capacity:n strings //vector of 8 integers, each initialized to 0 vector intList(8,0); 2/26/2016 ECE 264: Lecture 20 4

5 Review: Vector methods Common member functions:  bool empty(): true if vector contains no values  void pop_back(): deletes last element in vector Does not actually return the element Gives an error if vector is empty  void push_back(element): add element to end of vector  void resize(int): changes the size of vector  size_t size(): returns the size of vector  at( ): allows you to insert element in vector, but also provides boundary checking : type of elements stored in the vector (e.g. int, double)  void clear(): removes all elements from vector 2/26/2016 ECE 264: Lecture 20 5

6 Additional container classes C++ standard template library contains other useful containers  list : doubly-linked list Allows for efficient element insertion Can also push/pop front of list; sort, merge, reverse lists  queue : FIFO queue Has simple push/pop operations  priority_queue : sorted queue with largest element first  stack : LIFO stack Can access top of stack 2/26/2016 ECE 264: Lecture 20 6

7 7 Standard Library Class string Class string  Header, namespace std  We’ve seen: 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” 2/26/2016 ECE 264: Lecture 20

8 Standard Library Class string (Cont.) 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 82/26/2016 ECE 264: Lecture 20

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" ) s1 yields " s1 ? "true" : "false" ) << "\ns2 < s1 yields " << ( s2 < s1 ? "true" : "false" ) = s1 yields " = s1 ? "true" : "false" ) << "\ns2 <= s1 yields " << ( s2 <= s1 ? "true" : "false" ); 2/26/2016 ECE 264: Lecture 20 9

10 Example (cont.) 2/26/2016 ECE 264: Lecture 20 10 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

11 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"; 2/26/2016 ECE 264: Lecture 20 11

12 Example (cont.) 2/26/2016 ECE 264: Lecture 20 12 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

13 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 2/26/2016 ECE 264: Lecture 20 13

14 Example (cont.) 2/26/2016 ECE 264: Lecture 20 14 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

15 Final notes Next time  Finish strings Acknowledgements: this lecture borrows heavily from lecture slides provided with the following texts: Deitel & Deitel, C++ How to Program, 8 th ed. Etter & Ingber, Engineering Problem Solving with C++, 2 nd ed. 2/26/2016 ECE 264: Lecture 20 15


Download ppt "ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings."

Similar presentations


Ads by Google