Post on 03-Jan-2016
description
C++ Programming:Program Design Including Data Structures, Second Edition
Chapter 22: Standard Template Library (STL)
C++ Programming: Program Design Including Data Structures, Second Edition 2
Objectives
In this chapter you will:• Learn about the Standard Template Library
(STL)• Become familiar with the basic components of
the STL: containers, iterators, and algorithms• Explore how various containers are used to
manipulate data in a program• Discover the use of iterators • Learn about various generic algorithms
C++ Programming: Program Design Including Data Structures, Second Edition 3
Introduction
• ANSI/ISO Standard C++ is equipped with a Standard Template Library (STL)
• The STL provides class templates to process lists, stacks, and queues
• This chapter discusses many important features of the STL and shows how to use its tools
C++ Programming: Program Design Including Data Structures, Second Edition 4
Components of the STL
• Components of the STL:− Containers
− Iterators
− Algorithms
• Containers and iterators are class templates• Iterators are used to step through the
elements of a container• Algorithms are used to manipulate data
C++ Programming: Program Design Including Data Structures, Second Edition 5
Container Types
• Containers are used to manage objects of a given type
• Three categories:
− Sequence (sequential) containers
− Associative containers
− Container adapters
C++ Programming: Program Design Including Data Structures, Second Edition 6
Sequence Containers
• Every object has a specific position
• Three predefined sequence containers:
− Vector
− Deque
− List
C++ Programming: Program Design Including Data Structures, Second Edition 7
Sequence Container: Vector
• A vector container stores and manages its objects in a dynamic array
• To use a vector container in a program, the program must #include <vector>
• The class vector contains several constructors, including the default constructor
C++ Programming: Program Design Including Data Structures, Second Edition 9
Sequence Container: vector (continued)
• Basic vector operations
− Item insertion
− Item deletion
− Stepping through the elements
C++ Programming: Program Design Including Data Structures, Second Edition 10
Sequence Container: vector (continued)
• vector elements can be accessed using the operations below:
C++ Programming: Program Design Including Data Structures, Second Edition 11
Declaring an Iterator to a Vector Container
• vector contains a typedef iterator
• For example, the statement
vector<int>::iterator intVecIter;
declares intVecIter to be an iterator into a vector container of the type int
C++ Programming: Program Design Including Data Structures, Second Edition 12
Container and Functions begin and end
• Every container contains the member function begin and end
− begin returns the position of the first element
− end returns the position of the last element
• Both functions have no parameters
C++ Programming: Program Design Including Data Structures, Second Edition 13
Operations of the vector Class
• Some vector member functions:
C++ Programming: Program Design Including Data Structures, Second Edition 17
The copy Algorithm
• Function copy: convenient way to output the elements of a container
• Can be used with any container type• Allows you to copy the elements from one
place to another− Can output the elements of a vector
− Can copy the elements of one vector into another
C++ Programming: Program Design Including Data Structures, Second Edition 18
The ostream Iterator and the Function copy
• One way to output the contents of a container is to use a for loop, along with begin (initialize) and end (loop limit)
• copy can output a container; an iterator of the type ostream specifies the destination
• When you create an iterator of the type ostream, specify the type of element that the iterator will output
C++ Programming: Program Design Including Data Structures, Second Edition 19
Sequence Container: deque
• deque stands for double ended queue
• Implemented as dynamic arrays
• Elements can be inserted at both ends
• A deque can expand in either direction
• Elements are also inserted in the middle
C++ Programming: Program Design Including Data Structures, Second Edition 22
Sequence Container: list
• Lists are implemented as doubly linked lists
• Every element in a list points to both its immediate predecessor and its immediate successor (except the first and last element)
• The list is not a random access data structure
C++ Programming: Program Design Including Data Structures, Second Edition 26
Iterators
• An iterator points to the elements of a container (sequence or associative)
• Iterators provide access to each element
• The most common operations on iterators are ++ (increment), and * (dereference)
C++ Programming: Program Design Including Data Structures, Second Edition 27
Types of Iterators
• Five types of iterators:
− Input iterators
− Output iterators
− Forward iterators
− Bidirectional iterators
− Random access iterators
C++ Programming: Program Design Including Data Structures, Second Edition 28
Input Iterators
• Input iterators, with read access, step forward element-by-element return the values element-by-element
• Input iterators are provided for reading data from an input stream
C++ Programming: Program Design Including Data Structures, Second Edition 30
Output Iterators
• Output iterators, with write access, step forward element-by-element
• Output iterators are provided for writing data to an output stream
C++ Programming: Program Design Including Data Structures, Second Edition 32
Forward Iterators
• Forward iterators combine all of the functionality of input iterators and almost all of the functionality of output iterators
C++ Programming: Program Design Including Data Structures, Second Edition 34
Bidirectional Iterators
• Bidirectional iterators are forward iterators that can also iterate backward over the elements
• The operations defined for forward iterators apply to bidirectional iterators
• Use the decrement operator to step backward
C++ Programming: Program Design Including Data Structures, Second Edition 35
Random Access Iterators
• Random access iterators are bidirectional iterators that can randomly process the elements of a container
• Can be used with containers of the types vector, deque, string, as well as arrays
• Operations defined for bidirectional iterators apply to random access iterators
C++ Programming: Program Design Including Data Structures, Second Edition 37
typedef iterator
• Every container contains a typedef iterator
• The statement
vector<int>::iterator intVecIter;
declares intVecIter to be an iterator into a vector container of the type int
C++ Programming: Program Design Including Data Structures, Second Edition 38
typedef const_iterator
• With the help of an iterator into a container and the dereference operator, *, you can modify the elements of the container
• If the container is declared const, then we must prevent the iterator from modifying the elements
• Every container contains typedef const_iterator to handle these situations
C++ Programming: Program Design Including Data Structures, Second Edition 39
Stream Iterators
• istream_iterator
− Used to input data into a program from an input stream
• ostream_iterator
− Used to output data from a program into an output stream
C++ Programming: Program Design Including Data Structures, Second Edition 40
Associative Containers
• Elements in associative container are automatically sorted according to some ordering criteria
• The predefined associative containers in the STL are:− Sets
− Multisets
− Maps
− Multimaps
C++ Programming: Program Design Including Data Structures, Second Edition 41
Associative Containers: set and multiset
• Associative containers set and multiset automatically sort their elements
• multiset allows duplicates, set does not
• The default sorting criterion is the relational operator <(less than); that is, the elements are arranged in ascending order
C++ Programming: Program Design Including Data Structures, Second Edition 44
Container Adapters
• The STL provides containers to accommodate special situations called container adapters
• The three container adapters are:− Stacks
− Queues
− Priority Queues
• Container adapters do not support any type of iterator
C++ Programming: Program Design Including Data Structures, Second Edition 45
Stack
• The STL provides a stack class
C++ Programming: Program Design Including Data Structures, Second Edition 46
Queue
• The STL provides a queue class
C++ Programming: Program Design Including Data Structures, Second Edition 48
Algorithms
• Operations such as find, sort, and merge are common to all containers and are provided as generic algorithms
• STL algorithms can be classified as follows
− Nonmodifying algorithms
− Modifying algorithms
− Numeric algorithms
− Heap algorithms
C++ Programming: Program Design Including Data Structures, Second Edition 49
Nonmodifying Algorithms
• Nonmodifying algorithms do not modify the elements of the container
C++ Programming: Program Design Including Data Structures, Second Edition 50
Modifying Algorithms
C++ Programming: Program Design Including Data Structures, Second Edition 51
Numeric Algorithms
• Numeric algorithms perform numeric calculations on the elements of a container
• Numeric algorithms:
− accumulate
− inner_product
− adjacent_difference
− partial_sum
C++ Programming: Program Design Including Data Structures, Second Edition 52
Heap Algorithms
• Heap sort algorithm sorts array data
• The array containing the data is viewed as a binary tree
• Heap algorithms:
− make_heap
− push_heap
− pop_heap
− sort_heap
C++ Programming: Program Design Including Data Structures, Second Edition 53
Function Objects
• A function object contains a function that can be treated as a function using the function call operator, ()
• A function object is a class template that overloads the function call operator, ()
• Predicates are special types of function objects that return Boolean values
• Predicates are typically used to specify searching or sorting criteria
C++ Programming: Program Design Including Data Structures, Second Edition 55
Insert Iterators
• The STL provides three iterators, called insert iterators, to insert the elements at the destination:
− back_inserter
− front_inserter
− inserter
C++ Programming: Program Design Including Data Structures, Second Edition 56
STL Algorithms
• STL algorithms include documentation with the function prototypes
• The parameter types indicate for which type of container the algorithm is applicable
• fill: fills a container with elements
• fill_n: fills in the next n elements; the element that is used as a filling element is passed as a parameter
C++ Programming: Program Design Including Data Structures, Second Edition 57
STL Algorithms (continued)
• generate and generate_n: generate elements and fill a sequence
• find, find_if, find_end, and find_first_of: find elements in a given range
• remove: removes certain elements from a sequence
• remove_if: removes elements from a sequence by using some criteria
C++ Programming: Program Design Including Data Structures, Second Edition 58
STL Algorithms (continued)
• remove_copy, remove_copy_if : copies the elements of a sequence into another sequence by excluding certain elements of the first sequence
• swap, iter_swap, and swap_ranges: swap elements
• search, search_n, sort, and binary_search: search and sort elements, and described in the header file algorithm
C++ Programming: Program Design Including Data Structures, Second Edition 59
STL Algorithms (continued)
• Function replace is used to replace all occurrences, within a given range, of a given element with a new value
• Function replace_if is used to replace the values of the elements, within a given range, satisfying certain criteria with a new value
• The function replace_copy is a combination of replace and copy
• The function replace_copy_if is a combination of replace_if and copy
C++ Programming: Program Design Including Data Structures, Second Edition 60
STL Algorithms (continued)
• adjacent_find: finds the first occurrence of consecutive elements that meet criteria
• merge: merges the sorted lists. Both lists must be sorted according to the same criteria, for example, both must be in ascending order
• inplace_merge: combines sorted sequences
C++ Programming: Program Design Including Data Structures, Second Edition 61
STL Algorithms (continued)
• reverse: reverses the order of the elements in a given range
• reverse_copy: reverses the elements of a given range while copying into a destination range; the source is not modified
• rotate: rotates the elements of a given range• rotate_copy: combination of rotate and copy.
Elements of the source are copied at the destination in a rotated order; the source is not modified
C++ Programming: Program Design Including Data Structures, Second Edition 62
STL Algorithms (continued)
• count: counts the occurrence of a given item in a given range; returns the number of times the value specified by the parameter occurs
• count_if: counts occurrences of a given value in a given range satisfying a certain criterion
• min: determines the minimum of two values
C++ Programming: Program Design Including Data Structures, Second Edition 63
STL Algorithms (continued)
• max_element: determines the largest element in a given range
• max: determines the maximum of two values• min_element: determines the smallest element
in a given range• for_each: access and process each element in
a given range by applying a function• transform: creates a sequence of elements at
the destination by applying the unary operation to each element in the range
C++ Programming: Program Design Including Data Structures, Second Edition 64
STL Algorithms (continued)
• set_intersection, set_union, set_difference, and set_symmetric_difference: assume that the elements within each range are already sorted
• includes: determines whether the elements in one range appear in another range
• set_intersection: find the elements that are common to two ranges of elements
C++ Programming: Program Design Including Data Structures, Second Edition 65
STL Algorithms (continued)
• set_union: find the elements that are contained in two ranges of elements
• set_difference: finds the elements in one range that do not appear in another
• set_symmetric_difference: creates a sequence of sorted elements that are in one sorted range but not in another
C++ Programming: Program Design Including Data Structures, Second Edition 66
STL Algorithms (continued)
• accumulate: finds the sum of all the elements in a given range
• adjacent_difference: returns an iterator positioned one past the last element copied at the destination
• inner_product: manipulates the elements of two ranges
C++ Programming: Program Design Including Data Structures, Second Edition 67
Summary
• STL consists of:
− Containers: class templates
− Iterators: step through the elements of a container
− Algorithms: manipulate the elements in a container
C++ Programming: Program Design Including Data Structures, Second Edition 68
Summary
• Containers
− Sequence: vector, deque, and list
− Associative: sets, multisets, maps, and multimaps
− Container adapters: stacks, queues, and priority queues
C++ Programming: Program Design Including Data Structures, Second Edition 69
Summary
• Iterators: input, output, forward, bidirectional, and random access iterator
• Predicates: Boolean function objects
• Algorithms: nonmodifying, modifying, numerical, and heap
• Algorithms are overloaded for flexibility