Title: Standard Template Library STL
1Chapter 4
- Standard Template Library (STL)
2Chapter 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
3Components of the STL
- Containers
- Iterators
- Algorithms
4Container Types
- Sequence containers (sequential containers)
- Associative containers
- Container adapters
5Sequence Containers
6Ways to Declare and Initialize a Vector Container
7Operations to Access the Elements of a Vector
Container
8Operations on a Vector Container
9Functions to Determine the Size of a Vector
Container
10Member Functions Common to All Containers
11Member Functions Common to All Containers
12Member Functions Common to All Sequence Containers
13Prototype of function template copy
- templateltclass inputIterator, class
outputIteratorgt - outputItr copy(inputIterator first1,
inputIterator last, - outputIterator first2)
14Sequence 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 ltdequegt
15Ways to Declare a deq Object
16Operations that Can Be Performed on a deq Object
17Types of Iteration
- Input Iterators
- Output Iterators
- Forward Iterators
- Bidirectional Iterators
- Random Access Iterators
18Operations on an Input Iterator
19Operations on an Output Iterator
20Operations on a Forward Iterator
21Operations on a Bidirectional Iterator
22Operations on a Random Access Iterator
23Iterator Hierarchy
24typedefs Common to All Containers
25istream_iterator
- Used to input data into a program from an input
stream - General syntax
- istream_iteratorltTypegt isIdentifier(istream)
26ostream_iterator
- Contains the definition of an output stream
iterator - General syntax
- ostream_iteratorltTypegt osIdentifier(ostream)
- or
- ostream_iteratorltTypegt osIdentifier(ostream,
char deLimit)
27Programming 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.
28Programming 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
29Programming Example Grade Report
- Course Component
- Set the course information
- Print the course information
- Show the credit hours
- Show the course number
- Show the grade
30UML Diagram of the class courseType
31Programming 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
32UML Diagram of the class studentType and the
Inheritance Hierarchy
33Programming 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
34Chapter Summary
- Components of the STL
- Types of Containers
- Operations on Containers
- Sequence Containers
- Deque
- Vector
35Chapter Summary
- Iterators
- Types of iteration
- Operations on iterators
- Programming example