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

Slides:



Advertisements
Similar presentations
Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.
Advertisements

CSE Lecture 12 – Linked Lists …
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
ECE 264 Object-Oriented Software Development
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 21: Strings (cont.)
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Today’s Class Class string –The basics –Assignment –Concatenation –Compare & swap –Find –Conversion to C-style char * strings –Iterators.
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
More on the STL vector list stack queue priority_queue.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 15: Class diagrams; class relationships.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
1 Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack Stacks.
Data Structures Using C++ 2E
Containers Overview and Class Vector
1 Today’s Objectives  Announcements Turn in Homework #1 Homework #2 is posted and it is due on 21-Jun  Review Quiz #1  Pointers and C-style strings.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 6: Continuing with output formatting.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 4: Continuing with C++ I/O Basics.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 3: Requirements Specification, C++ Basics.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
ECE 264 Object-Oriented Software Development Instructor: Dr. Michael Geiger Spring 2009 Lecture 2: Basic C++ Programs.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
C++ STL CSCI 3110.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Fall 2012 Lecture 8: File I/O; Introduction to classes.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan and A. Ranade.
Lecture 5: Part 2 Classes and Data Abstraction. Objects Models of things in the real world Defined in classes  Class name is the object name Example:
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
String Class. C-style and C++ string Classes C-style strings, called C-strings, consist of characters stored in an array ( we’ll look at them later) C++
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 31: Operator overloading examples, inheritance intro.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
Lecture 7 : Intro. to STL (Standard Template Library)
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 22: Pointers.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 11: Class diagrams; class relationships.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 29: Operator overloading.
1 STL Containers Copyright Kip Irvine, All rights reserved. Only students enrolled in a class at Florida International University may copy or print.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with output formatting.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Lecture 7.  There are 2 types of libraries used by standard C++ The C standard library (math.h) and C++ The C++ standard template library  Allows us.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 4 Ming Li Department of Computer.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 16: Destructors, Copy Constructors and Exam 2 Review.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 19: Abstract Classes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 17: Operator overloading and inheritance intro.
1 The Standard Template Library The STL is a collection of Container classes These are class templates for containers. A container is an object that stores.
ECE 264 Object-Oriented Software Development
Standard Template Library (STL)
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Vectors.
Operator Overloading; String and Array Objects
Operator Overloading; String and Array Objects
7. 11 Introduction to C++ Standard Library Class Template vector (Cont
Engineering Problem Solving with C++, Etter
Today’s Objectives 28-Jun-2006 Announcements
Instructor: Dr. Michael Geiger Spring 2019 Lecture 6: Strings
Presentation transcript:

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

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

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

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

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

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 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

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

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

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

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

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

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

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

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