您的位置:首页 > 大数据 > 人工智能

Chapter 9 Templates. Generic Programming. and STL

2008-03-25 19:18 447 查看
Chapter 9 Templates. Generic
Programming. and STL
iragodnPLTmS.• 9.1 Template Class stack
• 9.2 Function Templates
• 9.2.1 Signature Matching and
Overloading
• 9.3 Class Templates
• 9.3.1 Friends
• 9.3.2 Static Members
• 9.3.3 Class Template Arguments
• 9.4 Parameterizing the Class vector
• 9.5 Parameterizing quicksort
()
• 9.6 Parameterized Binary Search
Tree
• 9.7 STL
• 9.8 Containers
• 9.8.1 Sequence Containers
• 9.8.2 Associative Containers
• 9.8.3 Container Adaptors
• 9.9 Iterators
• 9.9.1 The istream-iterator
and
ostream-iterator
• 9.9.2 Iterator Adaptors
• 9.10 Algorithms
• 9.10.1 Sorting Algorithms
• 9.10.2 Non mutating Sequence
Algorithms
• 9.10.3 Mutating Sequence
Algorithms
• 9.10.4 Numerical Algorithms
• 9.11 Functions
• 9.12 Function Adaptors
• 9.13 Pragmatics
• Summary
• Exercises
Chapter 9 Templates. Generic
Programming. and STL
iragodnPLTmS.• C++ uses the keyword temp1ate
to provide
parametric polymorphism,
p a r a m e t r i c p o l y m o r p h i s m ,
which allows the same
code to be used with respect to different types,
where the type is a parameter of the code body.
• Template class definitions and template function
definitions allow us to reuse code in a simple,
type-safe manner that lets the compiler automate
the process of type instantiation;
i n s t a n t i a t i o n ;
which is when
an actual type replaces a type parameter that
appeared in the template code.
9.1 Template Class stack
• The syntax of the class declaration is prefaced by:
template <c1ass identifien
i d e n t i f i e n
>
• Demo on page 278: stack.cpp
– This identifier is a template argument that essentially
stands for an arbitrary type.
– The template argument can be used as a type name.
– A
template declaration usually
– has global or namespace scope. It can be a member of a
class or it can be declared within another template class.
9.1 Template Class stack
9.1 Template Class stack
• This mechanism saves us rewriting class
declarations where the only variation would
be the type declarations.
9.1 Template Class stack
• Outside definition
9.2 Function Templates
• Simple macro
• Function template
• Demo on page 281:
– copy1.cpp
– Copy2.cpp
9.2.1 Signature Matching and
Overloading
• A generic routine often cannot work for
special cases.
9.2.1 Signature Matching and
Overloading
9.2.1 Signature Matching and
Overloading
• With this specialized case added, an exact match of
this nontemplate version to the signature of a swap()
invocation takes precedence over the exact match
found by a template substitution.
• Overloaded Function Selection Algorithm
– 1. Exact match with some trivial conversions allowed on a
nontemplate function.
– 2. Exact match using a function template.
– 3. Ordinary argument resolution on a nontemplate function.
9.3 Class Templates
• In this section, we wish to discuss various
special features of parameterizing classes.
9.3.1 Friends
• Template classes can contain friends.
• A
friend function that does not use a
template specification is universally a friend
of all instantiations of the template class.
• A friend function that incorporates template
arguments is specifically a friend of its
instantiated class:
9.3.1 Friends
9.3.2 Static Members
• Static members are not universal, but are
specific to each instantiation:
9.3.3 Class Template Arguments
• Both classes and functions can have several
class template arguments.
• Demo on page 284: coerce.cpp
9.3.3 Class Template Arguments
• Other template arguments include constant
expressions, function names, and character
strings.
9.4 Parameterizing the Class vector
9.4 Parameterizing the Class vector
9.4 Parameterizing the Class vector
9.4 Parameterizing the Class vector
9.4 Parameterizing the Class vector
• Demo on page 288: vect_it.cpp
9.5 Parameterizing quicksort
itscokqur
()
• Demo on page 289: quicksort.cpp
9.5 Parameterizing quicksort
itscokqur
()
9.5 Parameterizing quicksort
itscokqur
()
9.6 Parameterized Binary Search
Tree
• Demo on page 291: gentree1.cpp
9.6 Parameterized Binary Search
Tree
9.6 Parameterized Binary Search
Tree
9.6 Parameterized Binary Search
Tree
9.6 Parameterized Binary Search
Tree
9.6 Parameterized Binary Search
Tree
• Demo on page 294:
gentree1.cpp
9.7 STL
• The standard template library (STL) is the
C++ standard library providing generic
programming for many standard data
structures and algorithms.
• Demo on page 295: stl_cont.cpp
9.7 STL
9.7 STL
9.8 Containers
• Containers come in two major families:
sequence containers and associative
containers.
• Sequence containers include vectors, lists,
and deques. These containers are ordered by
having a sequence of elements.
• Associative containers include sets,
multisets, maps, and multimaps, and have
keys for looking up elements.
9.8 Containers
• Demo on page 297: stl_deq.cpp
9.8 Containers
9.8 Containers
9.8.1 Sequence Containers
• The sequence containers are vector, list, and
deque. They have a sequence of accessible
elements.
• Demo on page stl_vect.cpp
9.8.1 Sequence Containers
• Demo on page stl_vect.cpp
9.8.1 Sequence Containers
• Demo on page stl_vect.cpp
9.8.2 Associative Containers
• The associative containers are set, map,
multiset, and multimap.
• Demo on page 301: stl_age.cpp
9.8.2 Associative Containers
9.8.2 Associative Containers
9.8.2 Associative Containers
• Sample code
9.8.3 Container Adaptors
• Container adaptor classes are container
classes that modify existing containers to
produce different public behaviors based on
an existing implementation. Three provided
container adaptors are stack, queue, and
priority-queue.
• The stack can be adapted from vector, 1 i st,
and deque.
9.8.3 Container Adaptors
9.8.3 Container Adaptors
9.9 Iterators
• Navigation over containers is by iterator.
Iterators can be thought of as an enhanced
pointer type.
• They are templates that are instantiated as to
container class type they iterate over.
• There are five iterator types: input, output,
forward, bidirectional, and random access.
9.9 Iterators
• Input iterators support equality operations,
dereferencing, and autoincrement.
• Output iterators support dereferencing
restricted to the left-hand side of assignment
and autoincrement.
9.9 Iterators
• Forward iterators support all input/output
iterator operations as well as unrestricted
use of assignment.
• Bidirectional iterators support all forward
iterator operations as well as both
autoincrement and autodecrement.
9.9 Iterators
• Random access iterators support all
bidirectional iterator operations as well as
address arithmetic operations such as
indexing.
• Container classes and algorithms dictate the
category of iterator available or needed, so
vector containers allow random access
iterators, but 1ists do not.
9.9.1 The istream-iterator
tseraotrm
and
ostream-iterator
teraotsrm
• An istream_iterator is derived from an input
iterator to work specifically with reading
from streams. An ostream_i terator is
derived from an output iterator to work
specifically with writing to streams.
• Demo on page stl_io.cpp
9.9.1 The istream_iterator
aoitrm
and
ostream_iterator
aoitsrm
9.9.2 Iterator Adaptors
• Iterators can be adapted to provide
backward traversal and provide traversal
with insertion.
• Demo on page 306: stl_iadp.cpp
9.9.2 Iterator Adaptors
9.10 Algorithms
• The STL
algorithms library contains the
following four categories.
Categories of STL Algorithms Library
iltseragoySbhLTACmftror
• Sorting algorithms
• Nonmutating sequence algorithms
• Mutating sequence algorithms
• Numerical algorithms
9.10.1 Sorting Algorithms
• Sorting algorithms include general sorting,
merges, lexicographic comparison,
permutation, binary search, and selected
similar operations.
• Demo on page 308: stl_sort.cpp
9.10.1 Sorting Algorithms
• More algorithm prototypes may be found in
Section E.3.1 “Sorting Algorithms.” on page
498.
9.10.2 Non mutating Sequence
Algorithms
• Nonmutating algorithms do not modify the
contents of the containers they work on.
9.10.2 Non mutating Sequence
Algorithms
• Demo on page 309: stl_find.cpp
• More mutating function prototypes are
given in section E3.2 “Nonmuatateing
Sequence Algorighms” on page 501.
9.10.3 Mutating Sequence
Algorithms
• Mutating algorithms can modify the
contents of the containers they work on.
• Demo on page 310: stl_revr.cpp
9.10.3 Mutating Sequence
Algorithms
9.10.3 Mutating Sequence
Algorithms
• More algorithms are given in Section E 3.3
“Mutating Sequence Algrithms” on page
502.
9.10.4 Numerical Algorithms
• Numerical algorithms include sums, inner
product, and adjacent difference
• Demo on page 311: stl_numr.cpp
9.10.4 Numerical Algorithms
9.10.4 Numerical Algorithms
• More algorithms are give in Section E.3.4
“Numerical Algorithms” on page 505.
9.11 Functions
• Function objects are useful to further
leverage the STL
library.
• Demo on page 312: stl_func.cpp
9.11 Functions
• There are three defined function object
classes as shown in the following list.
• Defined Function Object Classes
– Arithmetic objects
– Comparison objects
– Logical objects
9.11 Functions
• Expanded tables of these three types are
given in Section E.4 “Functions” on page
506.
9.12 Function Adaptors
• Function adaptors allow for the creation of
function objects using adaption.
• Function Adaptors .
– Negators for negating predicate objects
– Binders for binding a function argument
– Adaptors for pointer to function
• Demo on page 314: stl_adap.cpp
9.12 Function Adaptors
9.12 Function Adaptors
• Allocators are discussed in Section E.5
“Allocators” on page 509.
9.13 Pragmatics
• Many current C++ template
implementations make a distinction
between what can be a template parameter
for functions versus what can be a template
parameter for classes.
• Functions allow only class arguments.
Furthermore, these class arguments must
occur in the template function as part of the
type description of at least one of the
function parameters.
9.13 Pragmatics
Summary
• 1. C++ uses templates to provide parametric
polymorphism.
• 2.
Both classes and functions can have several
class template arguments.
• 3.
A nontemplate, specialized version of a
function may be needed when the generic routine
will not work.
• Overloading Function Selection Algorithm
– Exact match on a nontemplate function.
– Exact match using a function template.
– Ordinary argument resolution on a nontemplste
function.
Summary
• 4. The standard template library (STL) is
the C++ standard library providing generic
programming for many standard data
structures and algorithms.
• 5. Containers come in two major families:
sequence and associative containers.
• 6. Container adaptor classes are container
classes that modify existing containers to
produce different public behaviors based on
an existing implementation.
Summary
• 7.
Iterators can be thought of as an
enhanced pointer type.
• 8. The STL algorithms library has the
following four categories:
– Sorting algorithms
– Nonmutating sequence algorithms
– Mutating sequence algorithms
– Numerical algorithms
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息