Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

35
Data Structures Using C++ 1 Chapter 4 Standard Template Library (STL)

Transcript of Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Page 1: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 1

Chapter 4

Standard Template Library (STL)

Page 2: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

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

Page 3: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 3

Components of the STL

• Containers

• Iterators

• Algorithms

Page 4: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 4

Container Types

• Sequence containers (sequential containers)

• Associative containers

• Container adapters

Page 5: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 5

Sequence Containers

• Vector

• Deque

• list

Page 6: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 6

Ways to Declare and Initialize a Vector Container

Page 7: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 7

Operations to Access the Elements of a Vector Container

Page 8: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 8

Operations on a Vector Container

Page 9: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 9

Functions to Determine the Size of a Vector Container

Page 10: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 10

Member Functions Common to All Containers

Page 11: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 11

Member Functions Common to All Containers

Page 12: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 12

Member Functions Common to All Sequence Containers

Page 13: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 13

Prototype of function template copy

template<class inputIterator, class outputIterator>

outputItr copy(inputIterator first1, inputIterator last,

outputIterator first2);

Page 14: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 14

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>

Page 15: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 15

Ways to Declare a deq Object

Page 16: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 16

Operations that Can Be Performed on a deq Object

Page 17: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 17

Types of Iteration

• Input Iterators

• Output Iterators

• Forward Iterators

• Bidirectional Iterators

• Random Access Iterators

Page 18: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 18

Operations on an Input Iterator

Page 19: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 19

Operations on an Output Iterator

Page 20: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 20

Operations on a Forward Iterator

Page 21: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 21

Operations on a Bidirectional Iterator

Page 22: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 22

Operations on a Random Access Iterator

Page 23: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 23

Iterator Hierarchy

Page 24: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 24

typedefs Common to All Containers

Page 25: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 25

istream_iterator

• Used to input data into a program from an input stream

• General syntax:istream_iterator<Type> isIdentifier(istream&);

Page 26: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 26

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

Page 27: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 27

Programming Example: Grade Report (Input)

A sample input file follows:

345Lisa Miller 890238 Y 4 Mathematics MTH345 4 APhysics PHY357 3 BComputerSci CSC478 3 BHistory 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.

Page 28: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 28

Programming Example: Grade Report (Output)

The desired output for each student is of the following form:

Student Name: Lisa MillerStudent ID: 890238Number of courses enrolled: 4

Course No Course Name Credits Grade CSC478 ComputerSci 3 BHIS356 History 3 AMTH345 Mathematics 4 APHY357 Physics 3 B

Page 29: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 29

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

Page 30: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 30

UML Diagram of the class courseType

Page 31: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 31

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

Page 32: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 32

UML Diagram of the class studentType and the Inheritance

Hierarchy

Page 33: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 33

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

Page 34: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 34

Chapter Summary

• Components of the STL

• Types of Containers

• Operations on Containers

• Sequence Containers– Deque– Vector

Page 35: Data Structures Using C++1 Chapter 4 Standard Template Library (STL)

Data Structures Using C++ 35

Chapter Summary

• Iterators

• Types of iteration

• Operations on iterators

• Programming example