1 / 38

Chapter 4

Chapter 4. Standard Template Library (STL) - Use Vector and Deque. Chapter Objectives. Learn about the Standard Template Library (STL) Become familiar with the three basic components of the STL: containers, iterators, and algorithms

abel
Download Presentation

Chapter 4

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 4 Standard Template Library (STL) - Use Vector and Deque Data Structures Using C++

  2. Chapter Objectives • Learn about the Standard Template Library (STL) • Become familiar with the three basic components of the STL: containers, iterators, and algorithms • Explore how vector and deque containers are used to manipulate data in a program • Discover the use of iterators Data Structures Using C++

  3. Components of the STL • Containers • Iterators • Algorithms Data Structures Using C++

  4. Container Types • Sequence containers (sequential containers) • Associative containers • Container adapters Data Structures Using C++

  5. Sequence Containers • Vector • Deque • list Data Structures Using C++

  6. Ways to Declare and Initialize a Vector Container Data Structures Using C++

  7. Operations to Access the Elements of a Vector Container Data Structures Using C++

  8. Operations on a Vector Container Data Structures Using C++

  9. Functions to Determine the Size of a Vector Container Data Structures Using C++

  10. Member Functions Common to All Containers Data Structures Using C++

  11. Member Functions Common to All Containers Data Structures Using C++

  12. Member Functions Common to All Sequence Containers Data Structures Using C++

  13. Use of Vector • Syntax: Vector declaration vector <elemenetType> name(size); Array declaration elementType name[size]; • #include <vector> Program Example_use_Vector Demo in class - Data Structures Using C++

  14. Passing Vector as parameter • Vector can be passed to functions by either reference or value. • Class Demo program • chapter4 program exercise 2,3 and 4. • Homework project is “Sequential Search by using Vector” - described in Exercise 5. Data Structures Using C++

  15. Prototype of function template copy template<class inputIterator, class outputIterator> outputItr copy(inputIterator first1, inputIterator last, outputIterator first2); Data Structures Using C++

  16. Sequence Container: deque • Deque: double-ended queue • Implemented as dynamic arrays • Elements can be inserted at both ends • To use deque container in a program include statement: #include <deque> Data Structures Using C++

  17. Ways to Declare a deq Object Data Structures Using C++

  18. Operations that Can Be Performed on a deq Object Data Structures Using C++

  19. Use of Deque • Syntax: deque declaration - deque <elementType> name(size); - Table 4-7 shows various ways to declare a deque object. - #include <deque> Program Example_use_Deque Demo in class - Data Structures Using C++

  20. Types of Iteration • Input Iterators • Output Iterators • Forward Iterators • Bidirectional Iterators • Random Access Iterators Data Structures Using C++

  21. Operations on an Input Iterator Data Structures Using C++

  22. Operations on an Output Iterator Data Structures Using C++

  23. Operations on a Forward Iterator Data Structures Using C++

  24. Operations on a Bidirectional Iterator Data Structures Using C++

  25. Operations on a Random Access Iterator Data Structures Using C++

  26. Iterator Hierarchy Data Structures Using C++

  27. typedefs Common to All Containers Data Structures Using C++

  28. istream_iterator • Used to input data into a program from an input stream • General syntax: istream_iterator<Type> isIdentifier(istream&); Data Structures Using C++

  29. ostream_iterator • Contains the definition of an output stream iterator • General syntax: ostream_iterator<Type> osIdentifier(ostream&); or ostream_iterator<Type> osIdentifier(ostream&, char* deLimit); Data Structures Using C++

  30. Programming Example: Grade Report (Input) A sample input file follows: 345 Lisa Miller 890238 Y 4 Mathematics MTH345 4 A Physics PHY357 3 B ComputerSci CSC478 3 B History HIS356 3 A . . . Input A file containing the data in the form shown previously. Assume the name of the input file is ch4_GradeData.txt and is on floppy disk A. Data Structures Using C++

  31. Programming Example: Grade Report (Output) The desired output for each student is of the following form: Student Name: Lisa Miller Student ID: 890238 Number of courses enrolled: 4 Course No Course Name Credits Grade CSC478 ComputerSci 3 B HIS356 History 3 A MTH345 Mathematics 4 A PHY357 Physics 3 B Data Structures Using C++

  32. Programming Example: Grade Report • Course Component • Set the course information • Print the course information • Show the credit hours • Show the course number • Show the grade Data Structures Using C++

  33. UML Diagram of the class courseType Data Structures Using C++

  34. Programming Example: Grade Report • Student Component • Set the student information • Print the student information • Calculate the number of credit hours taken • Calculate the GPA • Calculate the billing amount • Because the grade report will print the courses in ascending order, sort the courses according to the course number Data Structures Using C++

  35. UML Diagram of the class studentType and the Inheritance Hierarchy Data Structures Using C++

  36. Programming Example: Grade Report (Main Algorithm) • Declare the variables • Open the input file • If the input file does not exist, exit the program • Open the output file • Get the tuition rate • Load the students’ data • Print the grade reports Data Structures Using C++

  37. Chapter Summary • Components of the STL • Types of Containers • Operations on Containers • Sequence Containers • Deque • Vector Data Structures Using C++

  38. Chapter Summary • Iterators • Types of iteration • Operations on iterators • Programming example Data Structures Using C++

More Related