Chapter 9 Templates. Generic Programming. and STL

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当读者有一定c/c++基础 推荐的阅读顺序: level 1 从<>开始,短小精悍,可以对c++能进一步了解其特性 以<>作字典和课外读物,因为太厚不可能一口气看完 level 2 然后从<>开始转职,这是圣经,请遵守10诫,要经常看,没事就拿来翻翻 接着是<>,个人认为Herb Sutter主席大人的语言表达能力不及Scott Meyers总是在教育第一线的好 顺下来就是<>和<>,请熟读并牢记各条款 当你读到这里,应该会有一股升级的冲动了 level 3 <>看过后如一缕清风扫去一直以来你对语言的疑惑,你终于能明白compiler到底都背着你做了些什么了,这本书要细细回味,比较难啃,最好反复看几遍,加深印象 看完上一本之后,这本<>会重演一次当年C++他爹在设计整个语言过程中的历程 level 4 <>是stl的字典,要什么都可以查得到 学c++不能不学stl,那么首先是<>,它和圣经一样是你日常行为的规范 <>让你从oo向gp转变 光用不行,我们还有必要了解stl的工作原理,那么<>会解决你所有的困惑 level 5 对于c++无非是oo和gp,想进一步提升oo,<>是一本主席这么多年的经验之谈,是很长esp的 一位stl高手是不能不去了解template的,<>是一本百科全书,足够你看完后对于gp游刃有余 <>是太过聪明的人写给明眼人看的 好书有很多,不能一一列举 以上我的读书经历,供各位参考。接下来的无非就是打怪练级,多听多写多看;boost、stl、loki这些都是利器,斩妖除魔,奉劝各位别再土法练钢了。 at last,无他,唯手熟尔。 忘了一本《thinking in C++》 也是经典系列之一 <>这本圣经的作者Scott Meyesr在给<>序言的时候高度的赞赏了Andrei同志的工作:C++社群对template的理解即将经历一次巨大的变化,我对它所说的任何事情,也许很快就会被认为是陈旧的、肤浅的、甚至是完全错的。 就我所知,template的世界还在变化,速度之快就像我1995年回避写它的时候一样。从发展的速度来看,我可能永远不会写有关template的技术书籍。幸运的是一些人比我勇敢,Andrei就是这样一位先锋。我想你会从此书得到很多收获。我自己就得到了很多——Scott Meyers September2000。 并且,Scott Meyers 在最近的Top5系列文章中,评价C++历史里面最重要5本书中、把Modern C++ Design列入其中,另外四本是它自己的effective c++、以及C++ Programming Language、甚至包括《设计模式》和《C++标准文档》。 显然,Scott Meyers已经作为一个顶尖大师的角度承认了<>的价值。 并且调侃地说,可以把是否使用其中模板方法定义为,现代C++使用者和非现代C++使用者,并且检讨了自己在早期版本Effective对模板的忽视,最后重申在新版本Effective第七章节加入大量对模板程序设计的段落,作为对这次失误的补偿。 并且,在这里要明确的是<>并不是一本泛型编成的书,也不是一本模板手册。其中提出了基于策略的设计方法,有计划和目的的使用了模板、面向对象和设计模式。虽然Andrei本人对模板的研究世界无人能敌,但对其他领域的作为也令人赞叹。 任何做游戏的人都不能忽视OpenAL把,你在开发者的名单里能看到Loki的名字:) 最近很忙,无时间写文章,小奉献一下书籍下载地址。虽然经过验证,但是不感肯定各位一定能下: 中文 http://www.itepub.net/html/ebookcn/2006/0523/40146.html 英文 http://dl.njfiw.gov.cn/books/C/Essential%20C
内容简介   许多程序员可能并不知道,C++不仅是一个面向对象程序语言, 它还适用于泛型编程(generic programming)。这项技术可以大大增强你的能力,协助你写出高效率并可重复运用的软件组件(software components)。   本书由知名的C++专家Matthew H.Austern执笔,引导你进入泛型编程思维模型,并将你带往此一模型的最重要成品:C++ Standard Template Library(STL)。本书揭示STL的奥秘,告诉你STL不仅仅是一组方便运用的容器类(container classes)。对于泛型组件和可交互作用的组件而言,STL是一个具备扩充能力的框架(framework)、《泛型编程与STL》阐述了泛型编程的中心思想:concepts、modeling、refinement,并为你展示这些思想如何导出STL的基础概念:iterators、containersfunction objects。循此路线,你可以把STL想像为一个由concepts(而非明确之functions或classes)组成的程序库:、你将学习其正式结构并因此获得其潜在威力所带来的完整优势。本书使你能够:   ●以你自己的“可移植组件”及“可交互作用之泛型组件”扩充STL;   ●产生一些算法,让它们和它们所处理之型别(types)及数据结构彻底划清界线;   ●撰写更精致、更高效、更有效力的代码,可跨平台重复使用。 -------------------------------------------------------------------------------- 媒体推荐 书评 《泛型编程与STL》阐述了泛型编程的中心思想:concepts、modeling、refinement,并为你展示这些思想如何导出STL的基础概念:iterators、containersfunction objects。循此路线,你可以把STL想像为一个由来的完整优势…… -------------------------------------------------------------------------------- 编辑推荐 《泛型编程与STL》阐述了泛型编程的中心思想:concepts、modeling、refinement,并为你展示这些思想如何导出STL的基础概念:iterators、containersfunction objects。循此路线,你可以把STL想像为一个由来的完整优势…… -------------------------------------------------------------------------------- 目录 译序(侯捷) 前言 第一篇 泛型编程导入 第1章 STL巡礼 1.1 一个简单的例子 ……

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值