C++ Primer 英文版(第 5 版)

出版社:电子工业出版社
出版日期:2013-5
ISBN:9787121200380
作者:[美] Stanley B. Lippman,[美] Josée Lajoie,[美] Barbara E. Moo
页数:938页

章节摘录

版权页:   We start by initializing it to denote the first element in text.The loop continues until either we process every element in text or we find an element that is empty.So long as there are elements and we haven't seen an empty element,we print the current element.It is worth noting that beeause the loop reads but does not write to the elements in text,we use cbegin and cend to control the iteration.Some vector Operations Invalidate Iterators In 3.3.2 (p.101) we noted that there are implications of the fact that vectors can grow dynamically.We also noted that one such implication is that we cannot add elements to a vector inside a range for loop.Another implication is that any operation,such as push_back,that changes the size of a vector potentially invalidates all iterators into that vector.We'll explore how iterators become invalid in more detail in 9.3.6 (p.353). WARNING For now,it is important to realize that loops that use iterators should not add elements to the container to which the iterators refer. EXERCISES SECTION 3.4.1 Exercise 3.21: Redo the first exercise from 3.3.3 (p.105) using iterators. Exercise 3.22: Revise the loop that printed the first paragraph in text to instead change the elements in text that correspond to the first paragraph to all uppercase.After you've updated text,print its contents. Exercise 3.23: Write a program to create a vector with ten int elements.Using an iterator,assign each element a value that is twice its current value.Test your program by printing the vector.

前言

难以计数的程序员已经通过旧版的C++ Primer 学会了C++语言。而在这段时间中,C++本身又已成熟了许多:语言本身的关注点和程序设计社区的关注点都已大大开阔,已经从主要关注机器效率转变为更多地关注编程效率。2011 年,C++标准委员会发布了ISO C++标准的一个重要修订版。此修订版是C++进化过程中的最新一步,延续了前几个版本对编程效率的强调。新标准的主要目标是:使语言更为统一,更易于教学使标准库更简单、安全、使用更高效使编写高效率的抽象和库变得更简单因此,在这个版本的C++ Primer 中,我们进行了彻底的修改,使用了最新的C++标准。为了了解新标准是如何全面影响C++语言的,你可以看一下xxiii 页至xxv 页的新特性列表,其中列出了哪些章节涉及了C++的新特性。新标准增加的一些特性是具有普适性的,例如用于类型推断的auto。这些新特性使本书中的代码更易于阅读和理解。程序(以及程序员!)可以忽略类型的细节,从而更容易集中精力于程序逻辑上来。其他一些新特性,例如智能指针和允许移动的容器,允许我们编写更为复杂的类,而又不必与错综复杂的资源管理做斗争。因此,在本书中开始讲授如何编写自己的类,会比第4 版简单得多。旧标准中阻挡在我们前进路上的很多细节,你我都不必再担心了。对于本书中涉及新标准定义的新特性的那些部分,我们都已用一个特殊的图标标记出来了。我们希望这些提示标记对那些已经熟悉C++语言核心内容的读者是有帮助的,可以帮助他们决定将注意力投向哪里。对于那些可能尚不支持所有新特性的编译器,我们还希望这些图标能有助于解释这类编译器所给出的编译错误信息。这是因为虽然本书中几乎所有例子都已经用最新版本的GNU编译器编译通过,但我们知道一些读者可能尚未将编译器更新到最新版本。虽然新标准增加了大量新功能,但核心C++语言并未变化,这构成了本书的大部分内容。读者可以借助这些图标来判断哪些功能可能还没有被自己的编译器所支持。为什么选择这本书?现代C++语言可以看作是三部分组成的:低级语言,大部分继承自 C 语言。现代高级语言特性,允许我们定义自己的类型以及组织大规模程序和系统。标准库,它利用高级特性来提供有用的数据结构和算法。大多数 C++教材按照语言进化的顺序来组织其内容。首先讲授C++的C 子集,然后将C++的更为抽象的一些特性作为高级话题在书的最后进行介绍。这种方式存在两个问题:读者会陷入那些继承自低级程序设计的细节,从而由于挫折感而放弃;读者被强加学习一些坏习惯,随后又需要忘记这些内容。我们采用一种相反的方法:从一开始就介绍一些语言特性,能让程序员忽略那些继承自低级程序设计的细节。例如,在介绍和使用内置的算术和数组类型时,我们还连同介绍和使用标准库中的类型string 和vector。使用这些类型的程序更易写、易理解且更少出错。太多时候,标准库被当作一种“高级”话题来讲授。很多教材不使用标准库,而是使用基于字符数组指针和动态内存管理的低级程序设计技术。让使用这种低级技术的程序正确运行,要比编写相应的使用标准库的C++代码困难得多。贯穿 C++ Primer 全书,我们都在强调好的风格:我们想帮助读者直接养成好的习惯,而不是在获得很多很复杂的知识后再去忘掉那些坏习惯。我们特别强调那些棘手的问题,并对常见的错误想法和陷阱提出警告。我们还注意解释规则背后的基本原理——使读者不仅知其然,还能知其所以然。我们相信,通过体会程序的工作原理,读者会更快地巩固对语言的理解。虽然你不必为了学习本书而掌握 C 语言,但我们还是假定你了解足够多的程序设计知识,了解至少一门现代分程序结构语言,知道如何用这门语言编写、编译以及运行程序。特别是,我们假定你已经使用过变量,编写、调用过函数,也使用过编译器。第 5 版变化的内容这一版C++ Primer 的新特点是用边栏图标来帮助引导读者。C++是一种庞大的编程语言,它提供了一些为特定程序设计问题定制的功能。其中一些功能对大型项目团队有很重要的意义,但对于小型项目开发可能并无必要。因此,并非每个程序员都需要了解每个语言特性的所有细节。我们加入这些边栏图标来帮助读者弄清哪些内容可以随后再学习,而哪些主题是更为重要的。对于包含C++语言基础内容的章节,我们用一个小人正在读书的图标加以标记。用这个图标标记的那些章节,涵盖了构成语言核心部分的主题。每个人都应该阅读并理解这些章节的内容。对于那些涉及高级主题或特殊目的主题的章节,我们也进行了标记。在首次阅读时,这些章节可以跳过或快速浏览。我们用一叠书的图标标记这些章节,指出在这些地方,你可以放心地放下书本。快速浏览这些章节可能是一个好主意,这样你就可以知道有这些特性存在。但在真正需要在自己的程序中使用这些特性之前,没有必要花费时间仔细学习这些主题。为了进一步引导读者的注意力,我们还用放大镜图标标记了特别复杂的概念。我们希望读者对有这种标记的章节能多花费一些时间彻底理解其中的内容。在这些章节中,至少有一些,其主题的重要性可能不是那么明显;但我们认为,你会发现这些章节涉及的主题对理解C++语言原来至关重要。交叉引用的广泛使用,是本书采用的另外一种阅读帮助。我们希望这些引用能帮助读者容易地翻阅书中的内容,同时还能在后面的例子涉及到前面的内容时容易地跳回到前面。没有改变的是,C++ Primer 仍是一本清晰、正确、全面的C++入门教材。我们通过给出一系列复杂度逐步增加的例子来讲授这门语言,这些例子说明了语言特性,展示了如何充分用好C++语言。本书的结构我们首先在第I 部分和第II 部分中介绍了C++语言和标准库的基础内容。这两部分包含的内容足够你编写出有意义的程序,而不是只能写一些玩具程序。大部分程序员基本上都需要掌握本书这两部分所包含的所有内容。除了讲授 C++的基础内容,第I 部分和第II 部分还有另外一个重要目的:通过使用标准库中定义的抽象设施,使你更加适应高级程序设计技术。标准库设施本身是一组抽象数据类型,通常用C++编写。用来设计标准库的,就是任何C++程序员都可以使用的用来构造类的那些语言特性。我们讲授C++语言的一个经验是,在先学习了使用设计良好的抽象类型后,读者会发现理解如何构造自己的类型更容易了。只有在经过全面的标准库使用训练,并编写了各种标准库所支持的抽象程序后,我们才真正进入到那些允许你编写自己的抽象类型的C++特性中去。本书的第III 部分和第IV 部分介绍了如何编写类的形式的抽象类型。第III 部分包含基础内容,第IV 部分介绍更专门的语言特性。在第III 部分中,我们将介绍拷贝控制问题,以及其他一些使类能像内置类型一样容易使用的技术。类是面向对象编程和泛型编程的基础,第III 部分也会介绍这些内容。第IV 部分是C++ Primer 的结束部分,它介绍了一些在组织大型复杂系统时非常有用的语言特性。此外,我们将在附录A 中总结标准库算法。读者帮助本书的每一章均以一个总结和一个术语表结束,两者一起扼要回顾了这一章的大部分学习重点。读者应该将这些部分作为个人备忘录:如果你不理解某个术语,可以重新学习这一章的相应部分。在本书中我们还使用了其他一些学习辅助:重要的术语用加粗字体显示;我们假定读者已经熟悉的重要术语用粗斜体显示。每个术语都会列在章末尾的术语表中。贯穿全书,我们用高亮显示来提醒读者注意语言的重要部分,对常见的陷阱提出警告,建议好的程序设计习惯,以及提供一般性的使用提示。为了更好地理解语言特性间和概念间的联系,我们提供大量向前的和向后的交叉引用。对重要的概念和 C++新程序员常常觉得最困难的主题,我们提供边栏讨论。学习任何程序设计语言都需要编写程序。为此,贯穿全书我们提供大量程序示例。关于编译器的注意事项在撰写本书时(2012 年7 月),编译器提供商正在努力工作,升级编译器以匹配最新的ISO 标准。我们使用最多的编译器是GNU 编译器4.7.0。本书中只有一小部分特性在此编译器中尚未实现:继承构造函数、成员函数的引用限定符以及正则表达式库。致谢我们要特别感谢标准委员会几位现任和前任委员:Dave Abrahams、Andy Koenig、Stephan T. Lavavej、Jason Merrill、John Spicer 和Herb Sutter 在准备本书的过程中提供的帮助。在理解新标准的一些更微妙之处,他们为我们提供了宝贵的帮助。我们还要感谢很多致力于升级GNU 编译器以实现新标准的人们。与旧版C++ Primer 中一样,我们要感谢Bjarne Stroustrup 不知疲倦地为C++工作以及他和作者长时间的友谊。我们还要感谢Alex Stepanov 的非凡洞察力,催生了标准库核心的容器和算法。最后,我们要感谢C++标准委员会的所有委员,感谢他们这么多年来在净化、精炼和改进C++语言方面的辛苦工作。我们衷心感谢审稿人:Marshall Clow、Jon Kalb、Nevin Liber、Dr. C. L. Tondo、Daveed Vandevoorde 和Steve Vinoski,他们建设性的意见帮助我们对全书做出了大大小小的改进。本书是用 Latex 及其发行版本中的很多包来进行排版的,我们应该感谢Latex社区成员创造出如此强大的排版工具。最后,我们要感谢Addison-Wesley 公司的优秀员工,他们指导了本书的整个出版过程:Peter Gordon,我们的编辑,他给了我们动力再次修改C++ Primer;Kim Boedigheimer,保证了一切按计划进行;Barbara Wood,她在编辑过程中找到了大量编辑错误;还有Elizabeth Ryan,很高兴再次和她共同工作,她指导我们完成了整个设计和生产流程。

内容概要

Stanley B. Lippman目前是微软公司 Visual C++ 团队的架构师。他从1984年开始在贝尔实验室与C++的设计者Bjarne Stroustrup一起从事C++的设计与开发。他在迪士尼和梦工厂从事动画制作,还担任过JPL的高级顾问。
Josée Lajoie曾经是IBM加拿大研究中心C/C++编译器开发团队的成员,在ISO C++标准委员会工作了7年,担任过ISO核心语言工作组的主席和C++ Report杂志的专栏作家。
Barbara E. Moo是拥有25年软件经验的独立咨询顾问。在AT&T,她与Stroustrup、Lippman一起管理过复杂的C++开发项目。

书籍目录

Preface xxiii
Chapter 1 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Writing a Simple C++Program . . . . . . . . . . . . . . . . . . . . . . 2
1.1.1 Compiling and Executing Our Program . . . . . . . . . . . . . 3
1.2 AFirstLookat Input/Output . . . . . . . . . . . . . . . . . . . . . . . 5
1.3 AWordaboutComments . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.4 FlowofControl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.4.1 The whileStatement . . . . . . . . . . . . . . . . . . . . . . . 11
1.4.2 The forStatement . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.4.3 ReadinganUnknownNumberof Inputs . . . . . . . . . . . . 14
1.4.4 The ifStatement . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.5 IntroducingClasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.5.1 The Sales_itemClass . . . . . . . . . . . . . . . . . . . . . . 20
1.5.2 AFirstLookatMemberFunctions . . . . . . . . . . . . . . . . 23
1.6 TheBookstoreProgram. . . . . . . . . . . . . . . . . . . . . . . . . . . 24
ChapterSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
DefinedTerms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Part I The Basics 29
Chapter 2 Variables and Basic Types . . . . . . . . . . . . . . . . . . . . . 31
2.1 PrimitiveBuilt-inTypes . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.1.1 ArithmeticTypes . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.1.2 TypeConversions . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.1.3 Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
2.2 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
2.2.1 VariableDefinitions . . . . . . . . . . . . . . . . . . . . . . . . 41
2.2.2 VariableDeclarations andDefinitions . . . . . . . . . . . . . . 44
2.2.3 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
2.2.4 Scopeof aName . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2.3 CompoundTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
2.3.1 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
2.3.2 Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
vii
viii Contents
2.3.3 UnderstandingCompoundTypeDeclarations . . . . . . . . . 57
2.4 constQualifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
2.4.1 References to const . . . . . . . . . . . . . . . . . . . . . . . . 61
2.4.2 Pointers and const . . . . . . . . . . . . . . . . . . . . . . . . 62
2.4.3 Top-Level const . . . . . . . . . . . . . . . . . . . . . . . . . . 63
2.4.4 constexprandConstantExpressions . . . . . . . . . . . . . 65
2.5 DealingwithTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
2.5.1 TypeAliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
2.5.2 The autoTypeSpecifier . . . . . . . . . . . . . . . . . . . . . . 68
2.5.3 The decltypeTypeSpecifier . . . . . . . . . . . . . . . . . . . 70
2.6 DefiningOurOwnDataStructures . . . . . . . . . . . . . . . . . . . . 72
2.6.1 Defining the Sales_dataType . . . . . . . . . . . . . . . . . 72
2.6.2 Using the Sales_dataClass . . . . . . . . . . . . . . . . . . . 74
2.6.3 Writing Our Own Header Files . . . . . . . . . . . . . . . . . . 76
ChapterSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
DefinedTerms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Chapter 3 Strings, Vectors, and Arrays . . . . . . . . . . . . . . . . . . . . 81
3.1 Namespace usingDeclarations . . . . . . . . . . . . . . . . . . . . . . 82
3.2 Library stringType . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
3.2.1 Defining and Initializing strings . . . . . . . . . . . . . . . . 84
3.2.2 Operations on strings . . . . . . . . . . . . . . . . . . . . . . 85
3.2.3 Dealing with the Characters in a string . . . . . . . . . . . . 90
3.3 Library vectorType . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
3.3.1 Defining and Initializing vectors . . . . . . . . . . . . . . . . 97
3.3.2 Adding Elements to a vector . . . . . . . . . . . . . . . . . . 100
3.3.3 Other vectorOperations . . . . . . . . . . . . . . . . . . . . . 102
3.4 IntroducingIterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
3.4.1 UsingIterators . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
3.4.2 IteratorArithmetic . . . . . . . . . . . . . . . . . . . . . . . . . 111
3.5 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
3.5.1 DefiningandInitializingBuilt-inArrays . . . . . . . . . . . . 113
3.5.2 AccessingtheElementsof anArray . . . . . . . . . . . . . . . 116
3.5.3 Pointers andArrays . . . . . . . . . . . . . . . . . . . . . . . . 117
3.5.4 C-StyleCharacterStrings . . . . . . . . . . . . . . . . . . . . . 122
3.5.5 InterfacingtoOlderCode . . . . . . . . . . . . . . . . . . . . . 124
3.6 MultidimensionalArrays . . . . . . . . . . . . . . . . . . . . . . . . . 125
ChapterSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
DefinedTerms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Chapter 4 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
4.1 Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
4.1.1 BasicConcepts . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
4.1.2 PrecedenceandAssociativity . . . . . . . . . . . . . . . . . . . 136
4.1.3 OrderofEvaluation . . . . . . . . . . . . . . . . . . . . . . . . 137
4.2 ArithmeticOperators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
4.3 Logical andRelationalOperators . . . . . . . . . . . . . . . . . . . . . 141
Contents ix
4.4 AssignmentOperators . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
4.5 Increment andDecrementOperators . . . . . . . . . . . . . . . . . . . 147
4.6 TheMemberAccessOperators . . . . . . . . . . . . . . . . . . . . . . 150
4.7 TheConditionalOperator . . . . . . . . . . . . . . . . . . . . . . . . . 151
4.8 TheBitwiseOperators . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
4.9 The sizeofOperator . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
4.10 CommaOperator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
4.11 TypeConversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
4.11.1 TheArithmeticConversions . . . . . . . . . . . . . . . . . . . 159
4.11.2 Other ImplicitConversions . . . . . . . . . . . . . . . . . . . . 161
4.11.3 ExplicitConversions . . . . . . . . . . . . . . . . . . . . . . . . 162
4.12 OperatorPrecedenceTable . . . . . . . . . . . . . . . . . . . . . . . . . 166
ChapterSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
DefinedTerms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Chapter 5 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
5.1 Simple Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
5.2 StatementScope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
5.3 Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
5.3.1 The ifStatement . . . . . . . . . . . . . . . . . . . . . . . . . . 175
5.3.2 The switchStatement . . . . . . . . . . . . . . . . . . . . . . . 178
5.4 IterativeStatements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
5.4.1 The whileStatement . . . . . . . . . . . . . . . . . . . . . . . 183
5.4.2 Traditional forStatement . . . . . . . . . . . . . . . . . . . . . 185
5.4.3 Range forStatement . . . . . . . . . . . . . . . . . . . . . . . 187
5.4.4 The do whileStatement . . . . . . . . . . . . . . . . . . . . . 189
5.5 JumpStatements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
5.5.1 The breakStatement . . . . . . . . . . . . . . . . . . . . . . . 190
5.5.2 The continueStatement . . . . . . . . . . . . . . . . . . . . . 191
5.5.3 The gotoStatement . . . . . . . . . . . . . . . . . . . . . . . . 192
5.6 tryBlocks andExceptionHandling . . . . . . . . . . . . . . . . . . . 193
5.6.1 A throwExpression . . . . . . . . . . . . . . . . . . . . . . . . 193
5.6.2 The tryBlock . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
5.6.3 StandardExceptions . . . . . . . . . . . . . . . . . . . . . . . . 197
ChapterSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
DefinedTerms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Chapter 6 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
6.1 FunctionBasics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
6.1.1 LocalObjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
6.1.2 FunctionDeclarations . . . . . . . . . . . . . . . . . . . . . . . 206
6.1.3 SeparateCompilation . . . . . . . . . . . . . . . . . . . . . . . 207
6.2 ArgumentPassing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
6.2.1 PassingArgumentsbyValue . . . . . . . . . . . . . . . . . . . 209
6.2.2 PassingArgumentsbyReference . . . . . . . . . . . . . . . . . 210
6.2.3 constParametersandArguments . . . . . . . . . . . . . . . . 212
6.2.4 ArrayParameters . . . . . . . . . . . . . . . . . . . . . . . . . . 214
x Contents
6.2.5 main:HandlingCommand-LineOptions . . . . . . . . . . . . 218
6.2.6 FunctionswithVaryingParameters . . . . . . . . . . . . . . . 220
6.3 Return Types and the returnStatement . . . . . . . . . . . . . . . . 222
6.3.1 FunctionswithNoReturnValue . . . . . . . . . . . . . . . . . 223
6.3.2 FunctionsThatReturnaValue . . . . . . . . . . . . . . . . . . 223
6.3.3 ReturningaPointer toanArray . . . . . . . . . . . . . . . . . 228
6.4 OverloadedFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
6.4.1 OverloadingandScope . . . . . . . . . . . . . . . . . . . . . . 234
6.5 Features forSpecializedUses . . . . . . . . . . . . . . . . . . . . . . . 236
6.5.1 DefaultArguments . . . . . . . . . . . . . . . . . . . . . . . . . 236
6.5.2 Inline and constexprFunctions . . . . . . . . . . . . . . . . 238
6.5.3 Aids for Debugging . . . . . . . . . . . . . . . . . . . . . . . . 240
6.6 FunctionMatching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
6.6.1 ArgumentTypeConversions . . . . . . . . . . . . . . . . . . . 245
6.7 Pointers toFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
ChapterSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
DefinedTerms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Chapter 7 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
7.1 DefiningAbstractDataTypes . . . . . . . . . . . . . . . . . . . . . . . 254
7.1.1 Designing the Sales_dataClass . . . . . . . . . . . . . . . . 254
7.1.2 Defining the Revised Sales_dataClass . . . . . . . . . . . . 256
7.1.3 DefiningNonmemberClass-RelatedFunctions . . . . . . . . . 260
7.1.4 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
7.1.5 Copy,Assignment, andDestruction . . . . . . . . . . . . . . . 267
7.2 AccessControl andEncapsulation . . . . . . . . . . . . . . . . . . . . 268
7.2.1 Friends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
7.3 AdditionalClassFeatures . . . . . . . . . . . . . . . . . . . . . . . . . 271
7.3.1 ClassMembersRevisited . . . . . . . . . . . . . . . . . . . . . 271
7.3.2 Functions That Return *this . . . . . . . . . . . . . . . . . . . 275
7.3.3 ClassTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
7.3.4 FriendshipRevisited . . . . . . . . . . . . . . . . . . . . . . . . 279
7.4 ClassScope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
7.4.1 NameLookupandClassScope . . . . . . . . . . . . . . . . . . 283
7.5 ConstructorsRevisited . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
7.5.1 Constructor InitializerList . . . . . . . . . . . . . . . . . . . . . 288
7.5.2 DelegatingConstructors . . . . . . . . . . . . . . . . . . . . . . 291
7.5.3 TheRoleof theDefaultConstructor . . . . . . . . . . . . . . . 293
7.5.4 ImplicitClass-TypeConversions . . . . . . . . . . . . . . . . . 294
7.5.5 AggregateClasses . . . . . . . . . . . . . . . . . . . . . . . . . 298
7.5.6 LiteralClasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
7.6 staticClassMembers . . . . . . . . . . . . . . . . . . . . . . . . . . 300
ChapterSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
DefinedTerms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Contents xi
Part II The C++ Library 307
Chapter 8 The IO Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
8.1 The IOClasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
8.1.1 NoCopyorAssignfor IOObjects . . . . . . . . . . . . . . . . 311
8.1.2 ConditionStates . . . . . . . . . . . . . . . . . . . . . . . . . . 312
8.1.3 ManagingtheOutputBuffer . . . . . . . . . . . . . . . . . . . 314
8.2 File Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
8.2.1 Using File Stream Objects . . . . . . . . . . . . . . . . . . . . . 317
8.2.2 File Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
8.3 stringStreams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
8.3.1 Using an istringstream . . . . . . . . . . . . . . . . . . . . 321
8.3.2 Using ostringstreams . . . . . . . . . . . . . . . . . . . . . 323
ChapterSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
DefinedTerms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Chapter 9 Sequential Containers . . . . . . . . . . . . . . . . . . . . . . . 325
9.1 Overviewof the SequentialContainers . . . . . . . . . . . . . . . . . . 326
9.2 ContainerLibraryOverview . . . . . . . . . . . . . . . . . . . . . . . . 328
9.2.1 Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
9.2.2 ContainerTypeMembers . . . . . . . . . . . . . . . . . . . . . 332
9.2.3 begin and endMembers . . . . . . . . . . . . . . . . . . . . . 333
9.2.4 DefiningandInitializingaContainer . . . . . . . . . . . . . . 334
9.2.5 Assignment and swap . . . . . . . . . . . . . . . . . . . . . . . 337
9.2.6 ContainerSizeOperations . . . . . . . . . . . . . . . . . . . . . 340
9.2.7 RelationalOperators . . . . . . . . . . . . . . . . . . . . . . . . 340
9.3 SequentialContainerOperations . . . . . . . . . . . . . . . . . . . . . 341
9.3.1 AddingElements toaSequentialContainer . . . . . . . . . . . 341
9.3.2 AccessingElements . . . . . . . . . . . . . . . . . . . . . . . . . 346
9.3.3 ErasingElements . . . . . . . . . . . . . . . . . . . . . . . . . . 348
9.3.4 Specialized forward_listOperations . . . . . . . . . . . . . 350
9.3.5 ResizingaContainer . . . . . . . . . . . . . . . . . . . . . . . . 352
9.3.6 ContainerOperationsMayInvalidateIterators . . . . . . . . . 353
9.4 How a vectorGrows . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
9.5 Additional stringOperations . . . . . . . . . . . . . . . . . . . . . . 360
9.5.1 Other Ways to Construct strings . . . . . . . . . . . . . . . . 360
9.5.2 Other Ways to Change a string . . . . . . . . . . . . . . . . . 361
9.5.3 stringSearchOperations . . . . . . . . . . . . . . . . . . . . 364
9.5.4 The compareFunctions . . . . . . . . . . . . . . . . . . . . . . 366
9.5.5 NumericConversions . . . . . . . . . . . . . . . . . . . . . . . 367
9.6 ContainerAdaptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
ChapterSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
DefinedTerms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
xii Contents
Chapter 10 Generic Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . 375
10.1 Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
10.2 AFirstLookat theAlgorithms . . . . . . . . . . . . . . . . . . . . . . 378
10.2.1 Read-OnlyAlgorithms . . . . . . . . . . . . . . . . . . . . . . . 379
10.2.2 AlgorithmsThatWriteContainerElements . . . . . . . . . . . 380
10.2.3 AlgorithmsThatReorderContainerElements . . . . . . . . . 383
10.3 CustomizingOperations . . . . . . . . . . . . . . . . . . . . . . . . . . 385
10.3.1 PassingaFunctiontoanAlgorithm . . . . . . . . . . . . . . . 386
10.3.2 LambdaExpressions . . . . . . . . . . . . . . . . . . . . . . . . 387
10.3.3 LambdaCapturesandReturns . . . . . . . . . . . . . . . . . . 392
10.3.4 BindingArguments . . . . . . . . . . . . . . . . . . . . . . . . 397
10.4 Revisiting Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
10.4.1 Insert Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
10.4.2 iostream Iterators . . . . . . . . . . . . . . . . . . . . . . . . 403
10.4.3 Reverse Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . 407
10.5 StructureofGenericAlgorithms . . . . . . . . . . . . . . . . . . . . . 410
10.5.1 TheFive IteratorCategories . . . . . . . . . . . . . . . . . . . . 410
10.5.2 AlgorithmParameterPatterns . . . . . . . . . . . . . . . . . . 412
10.5.3 AlgorithmNamingConventions . . . . . . . . . . . . . . . . . 413
10.6 Container-SpecificAlgorithms . . . . . . . . . . . . . . . . . . . . . . . 415
ChapterSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
DefinedTerms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Chapter 11 Associative Containers . . . . . . . . . . . . . . . . . . . . . . . 419
11.1 UsinganAssociativeContainer . . . . . . . . . . . . . . . . . . . . . . 420
11.2 Overviewof theAssociativeContainers . . . . . . . . . . . . . . . . . 423
11.2.1 DefininganAssociativeContainer . . . . . . . . . . . . . . . . 423
11.2.2 Requirements onKeyType . . . . . . . . . . . . . . . . . . . . 424
11.2.3 The pairType . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
11.3 Operations onAssociativeContainers . . . . . . . . . . . . . . . . . . 428
11.3.1 AssociativeContainer Iterators . . . . . . . . . . . . . . . . . . 429
11.3.2 AddingElements . . . . . . . . . . . . . . . . . . . . . . . . . . 431
11.3.3 ErasingElements . . . . . . . . . . . . . . . . . . . . . . . . . . 434
11.3.4 Subscripting a map . . . . . . . . . . . . . . . . . . . . . . . . . 435
11.3.5 AccessingElements . . . . . . . . . . . . . . . . . . . . . . . . . 436
11.3.6 AWordTransformationMap . . . . . . . . . . . . . . . . . . . 440
11.4 TheUnorderedContainers . . . . . . . . . . . . . . . . . . . . . . . . . 443
ChapterSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
DefinedTerms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
Chapter 12 DynamicMemory . . . . . . . . . . . . . . . . . . . . . . . . . . 449
12.1 DynamicMemoryandSmartPointers . . . . . . . . . . . . . . . . . . 450
12.1.1 The shared_ptrClass . . . . . . . . . . . . . . . . . . . . . . 450
12.1.2 ManagingMemoryDirectly . . . . . . . . . . . . . . . . . . . . 458
12.1.3 Using shared_ptrs with new . . . . . . . . . . . . . . . . . . 464
12.1.4 SmartPointers andExceptions . . . . . . . . . . . . . . . . . . 467
12.1.5 unique_ptr . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
Contents xiii
12.1.6 weak_ptr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
12.2 DynamicArrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
12.2.1 newandArrays . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
12.2.2 The allocatorClass . . . . . . . . . . . . . . . . . . . . . . . 481
12.3 UsingtheLibrary:AText-QueryProgram . . . . . . . . . . . . . . . . 484
12.3.1 Designof theQueryProgram . . . . . . . . . . . . . . . . . . . 485
12.3.2 DefiningtheQueryProgramClasses . . . . . . . . . . . . . . . 487
ChapterSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
DefinedTerms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
Part III Tools for Class Authors 493
Chapter 13 Copy Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
13.1 Copy,Assign, andDestroy . . . . . . . . . . . . . . . . . . . . . . . . . 496
13.1.1 TheCopyConstructor . . . . . . . . . . . . . . . . . . . . . . . 496
13.1.2 TheCopy-AssignmentOperator . . . . . . . . . . . . . . . . . 500
13.1.3 TheDestructor . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
13.1.4 TheRuleofThree/Five . . . . . . . . . . . . . . . . . . . . . . 503
13.1.5 Using = default . . . . . . . . . . . . . . . . . . . . . . . . . . 506
13.1.6 PreventingCopies . . . . . . . . . . . . . . . . . . . . . . . . . 507
13.2 CopyControl andResourceManagement . . . . . . . . . . . . . . . . 510
13.2.1 ClassesThatActLikeValues . . . . . . . . . . . . . . . . . . . 511
13.2.2 DefiningClassesThatActLikePointers . . . . . . . . . . . . . 513
13.3 Swap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516
13.4 ACopy-ControlExample . . . . . . . . . . . . . . . . . . . . . . . . . 519
13.5 ClassesThatManageDynamicMemory . . . . . . . . . . . . . . . . . 524
13.6 MovingObjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531
13.6.1 RvalueReferences . . . . . . . . . . . . . . . . . . . . . . . . . 532
13.6.2 MoveConstructor andMoveAssignment . . . . . . . . . . . . 534
13.6.3 RvalueReferencesandMemberFunctions . . . . . . . . . . . 544
ChapterSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
DefinedTerms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
Chapter 14 Overloaded Operations and Conversions . . . . . . . . . . . . . 551
14.1 BasicConcepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552
14.2 Input andOutputOperators . . . . . . . . . . . . . . . . . . . . . . . . 556
14.2.1 Overloading the Output Operator <<. . . . . . . . . . . . . . 557
14.2.2 Overloading the Input Operator >>. . . . . . . . . . . . . . . 558
14.3 Arithmetic andRelationalOperators . . . . . . . . . . . . . . . . . . . 560
14.3.1 EqualityOperators . . . . . . . . . . . . . . . . . . . . . . . . . 561
14.3.2 RelationalOperators . . . . . . . . . . . . . . . . . . . . . . . . 562
14.4 AssignmentOperators . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
14.5 SubscriptOperator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564
14.6 Increment andDecrementOperators . . . . . . . . . . . . . . . . . . . 566
14.7 MemberAccessOperators . . . . . . . . . . . . . . . . . . . . . . . . . 569
14.8 Function-CallOperator . . . . . . . . . . . . . . . . . . . . . . . . . . . 571
xiv Contents
14.8.1 LambdasAreFunctionObjects . . . . . . . . . . . . . . . . . . 572
14.8.2 Library-DefinedFunctionObjects . . . . . . . . . . . . . . . . 574
14.8.3 Callable Objects and function . . . . . . . . . . . . . . . . . 576
14.9 Overloading,Conversions, andOperators . . . . . . . . . . . . . . . . 579
14.9.1 ConversionOperators . . . . . . . . . . . . . . . . . . . . . . . 580
14.9.2 AvoidingAmbiguousConversions . . . . . . . . . . . . . . . . 583
14.9.3 FunctionMatchingandOverloadedOperators . . . . . . . . . 587
ChapterSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
DefinedTerms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
Chapter 15 Object-Oriented Programming . . . . . . . . . . . . . . . . . . . 591
15.1 OOP:AnOverview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592
15.2 DefiningBaseandDerivedClasses . . . . . . . . . . . . . . . . . . . . 594
15.2.1 DefiningaBaseClass . . . . . . . . . . . . . . . . . . . . . . . . 594
15.2.2 DefiningaDerivedClass . . . . . . . . . . . . . . . . . . . . . 596
15.2.3 Conversions andInheritance . . . . . . . . . . . . . . . . . . . 601
15.3 VirtualFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
15.4 AbstractBaseClasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . 608
15.5 AccessControl andInheritance . . . . . . . . . . . . . . . . . . . . . . 611
15.6 ClassScopeunder Inheritance . . . . . . . . . . . . . . . . . . . . . . . 617
15.7 Constructors andCopyControl . . . . . . . . . . . . . . . . . . . . . . 622
15.7.1 VirtualDestructors . . . . . . . . . . . . . . . . . . . . . . . . . 622
15.7.2 SynthesizedCopyControl andInheritance . . . . . . . . . . . 623
15.7.3 Derived-ClassCopy-ControlMembers . . . . . . . . . . . . . 625
15.7.4 InheritedConstructors . . . . . . . . . . . . . . . . . . . . . . . 628
15.8 Containers andInheritance . . . . . . . . . . . . . . . . . . . . . . . . 630
15.8.1 Writing a BasketClass . . . . . . . . . . . . . . . . . . . . . . 631
15.9 TextQueriesRevisited . . . . . . . . . . . . . . . . . . . . . . . . . . . 634
15.9.1 AnObject-OrientedSolution . . . . . . . . . . . . . . . . . . . 636
15.9.2 The Query_base and QueryClasses . . . . . . . . . . . . . . 639
15.9.3 TheDerivedClasses . . . . . . . . . . . . . . . . . . . . . . . . 642
15.9.4 The evalFunctions . . . . . . . . . . . . . . . . . . . . . . . . 645
ChapterSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649
DefinedTerms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649
Chapter 16 Templates and Generic Programming . . . . . . . . . . . . . . . 651
16.1 DefiningaTemplate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652
16.1.1 FunctionTemplates . . . . . . . . . . . . . . . . . . . . . . . . . 652
16.1.2 ClassTemplates . . . . . . . . . . . . . . . . . . . . . . . . . . . 658
16.1.3 TemplateParameters . . . . . . . . . . . . . . . . . . . . . . . . 668
16.1.4 MemberTemplates . . . . . . . . . . . . . . . . . . . . . . . . . 672
16.1.5 Controlling Instantiations . . . . . . . . . . . . . . . . . . . . . 675
16.1.6 Efficiency and Flexibility . . . . . . . . . . . . . . . . . . . . . . 676
16.2 TemplateArgumentDeduction . . . . . . . . . . . . . . . . . . . . . . 678
16.2.1 Conversions andTemplateTypeParameters . . . . . . . . . . 679
16.2.2 Function-TemplateExplicitArguments . . . . . . . . . . . . . 681
16.2.3 Trailing Return Types and Type Transformation . . . . . . . . 683
Contents xv
16.2.4 FunctionPointers andArgumentDeduction . . . . . . . . . . 686
16.2.5 TemplateArgumentDeductionandReferences . . . . . . . . 687
16.2.6 Understanding std::move . . . . . . . . . . . . . . . . . . . . 690
16.2.7 Forwarding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692
16.3 OverloadingandTemplates . . . . . . . . . . . . . . . . . . . . . . . . 694
16.4 VariadicTemplates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699
16.4.1 WritingaVariadicFunctionTemplate . . . . . . . . . . . . . . 701
16.4.2 PackExpansion . . . . . . . . . . . . . . . . . . . . . . . . . . . 702
16.4.3 ForwardingParameterPacks . . . . . . . . . . . . . . . . . . . 704
16.5 Template Specializations . . . . . . . . . . . . . . . . . . . . . . . . . . 706
ChapterSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713
DefinedTerms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713
Part IV Advanced Topics 715
Chapter 17 Specialized Library Facilities . . . . . . . . . . . . . . . . . . . 717
17.1 The tupleType . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 718
17.1.1 Defining and Initializing tuples . . . . . . . . . . . . . . . . . 718
17.1.2 Using a tuple toReturnMultipleValues . . . . . . . . . . . . 721
17.2 The bitsetType . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723
17.2.1 Defining and Initializing bitsets . . . . . . . . . . . . . . . . 723
17.2.2 Operations on bitsets . . . . . . . . . . . . . . . . . . . . . . 725
17.3 RegularExpressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 728
17.3.1 UsingtheRegularExpressionLibrary . . . . . . . . . . . . . . 729
17.3.2 TheMatchandRegex IteratorTypes . . . . . . . . . . . . . . . 734
17.3.3 UsingSubexpressions . . . . . . . . . . . . . . . . . . . . . . . 738
17.3.4 Using regex_replace . . . . . . . . . . . . . . . . . . . . . . 741
17.4 RandomNumbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 745
17.4.1 Random-NumberEngines andDistribution . . . . . . . . . . . 745
17.4.2 OtherKinds ofDistributions . . . . . . . . . . . . . . . . . . . 749
17.5 The IOLibraryRevisited . . . . . . . . . . . . . . . . . . . . . . . . . . 752
17.5.1 FormattedInput andOutput . . . . . . . . . . . . . . . . . . . 753
17.5.2 UnformattedInput/OutputOperations . . . . . . . . . . . . . 761
17.5.3 RandomAccess toaStream . . . . . . . . . . . . . . . . . . . . 763
ChapterSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 769
DefinedTerms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 769
Chapter 18 Tools for Large Programs . . . . . . . . . . . . . . . . . . . . . . 771
18.1 ExceptionHandling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 772
18.1.1 ThrowinganException . . . . . . . . . . . . . . . . . . . . . . 772
18.1.2 CatchinganException . . . . . . . . . . . . . . . . . . . . . . . 775
18.1.3 Function tryBlocks andConstructors . . . . . . . . . . . . . 777
18.1.4 The noexceptExceptionSpecification . . . . . . . . . . . . . 779
18.1.5 ExceptionClassHierarchies . . . . . . . . . . . . . . . . . . . . 782
18.2 Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785
18.2.1 NamespaceDefinitions . . . . . . . . . . . . . . . . . . . . . . 785
xvi Contents
18.2.2 UsingNamespaceMembers . . . . . . . . . . . . . . . . . . . . 792
18.2.3 Classes,Namespaces,andScope . . . . . . . . . . . . . . . . . 796
18.2.4 OverloadingandNamespaces . . . . . . . . . . . . . . . . . . 800
18.3 Multiple andVirtual Inheritance . . . . . . . . . . . . . . . . . . . . . 802
18.3.1 Multiple Inheritance . . . . . . . . . . . . . . . . . . . . . . . . 803
18.3.2 Conversions andMultipleBaseClasses . . . . . . . . . . . . . 805
18.3.3 ClassScopeunderMultiple Inheritance . . . . . . . . . . . . . 807
18.3.4 Virtual Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . 810
18.3.5 Constructors andVirtual Inheritance . . . . . . . . . . . . . . . 813
ChapterSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816
DefinedTerms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816
Chapter 19 Specialized Tools and Techniques . . . . . . . . . . . . . . . . . 819
19.1 Controlling Memory Allocation . . . . . . . . . . . . . . . . . . . . . . 820
19.1.1 Overloading new and delete . . . . . . . . . . . . . . . . . . 820
19.1.2 Placement newExpressions . . . . . . . . . . . . . . . . . . . . 823
19.2 Run-TimeTypeIdentification . . . . . . . . . . . . . . . . . . . . . . . 825
19.2.1 The dynamic_castOperator . . . . . . . . . . . . . . . . . . 825
19.2.2 The typeidOperator . . . . . . . . . . . . . . . . . . . . . . . 826
19.2.3 UsingRTTI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 828
19.2.4 The type_infoClass . . . . . . . . . . . . . . . . . . . . . . . 831
19.3 Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 832
19.4 Pointer toClassMember . . . . . . . . . . . . . . . . . . . . . . . . . . 835
19.4.1 Pointers toDataMembers . . . . . . . . . . . . . . . . . . . . . 836
19.4.2 Pointers toMemberFunctions . . . . . . . . . . . . . . . . . . 838
19.4.3 UsingMemberFunctions asCallableObjects . . . . . . . . . . 841
19.5 NestedClasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843
19.6 union:ASpace-SavingClass . . . . . . . . . . . . . . . . . . . . . . . 847
19.7 LocalClasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 852
19.8 InherentlyNonportableFeatures . . . . . . . . . . . . . . . . . . . . . 854
19.8.1 Bit-fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 854
19.8.2 volatileQualifier . . . . . . . . . . . . . . . . . . . . . . . . 856
19.8.3 Linkage Directives: extern "C" . . . . . . . . . . . . . . . . . 857
ChapterSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 862
DefinedTerms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 862
Appendix A The Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . 865
A.1 LibraryNames andHeaders . . . . . . . . . . . . . . . . . . . . . . . . 866
A.2 ABriefTourof theAlgorithms . . . . . . . . . . . . . . . . . . . . . . 870
A.2.1 Algorithms toFindanObject . . . . . . . . . . . . . . . . . . . 871
A.2.2 OtherRead-OnlyAlgorithms . . . . . . . . . . . . . . . . . . . 872
A.2.3 BinarySearchAlgorithms . . . . . . . . . . . . . . . . . . . . . 873
A.2.4 AlgorithmsThatWriteContainerElements . . . . . . . . . . . 873
A.2.5 PartitioningandSortingAlgorithms . . . . . . . . . . . . . . . 875
A.2.6 GeneralReorderingOperations . . . . . . . . . . . . . . . . . . 877
A.2.7 PermutationAlgorithms . . . . . . . . . . . . . . . . . . . . . . 879
A.2.8 SetAlgorithms forSortedSequences . . . . . . . . . . . . . . . 880
Contents xvii
A.2.9 MinimumandMaximumValues . . . . . . . . . . . . . . . . . 880
A.2.10 NumericAlgorithms . . . . . . . . . . . . . . . . . . . . . . . . 881
A.3 RandomNumbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 882
A.3.1 RandomNumberDistributions . . . . . . . . . . . . . . . . . . 883
A.3.2 RandomNumberEngines . . . . . . . . . . . . . . . . . . . . . 884
Index
New Features in C++11
2.1.1 long longType . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.2.1 List Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . 43
2.3.2 nullptrLiteral . . . . . . . . . . . . . . . . . . . . . . . . . . 54
2.4.4 constexprVariables . . . . . . . . . . . . . . . . . . . . . . . 66
2.5.1 TypeAliasDeclarations . . . . . . . . . . . . . . . . . . . . . . 68
2.5.2 The autoTypeSpecifier . . . . . . . . . . . . . . . . . . . . . . 68
2.5.3 The decltypeTypeSpecifier . . . . . . . . . . . . . . . . . . . 70
2.6.1 In-Class Initializers . . . . . . . . . . . . . . . . . . . . . . . . . 73
3.2.2 Using auto or decltype forTypeAbbreviation . . . . . . . 88
3.2.3 Range forStatement . . . . . . . . . . . . . . . . . . . . . . . 91
3.3 Defining a vector of vectors . . . . . . . . . . . . . . . . . . 97
3.3.1 List Initialization for vectors . . . . . . . . . . . . . . . . . . 98
3.4.1 Container cbegin and cendFunctions . . . . . . . . . . . . . 109
3.5.3 Library begin and endFunctions . . . . . . . . . . . . . . . . 118
3.6 Using auto or decltype to SimplifyDeclarations . . . . . . 129
4.2 RoundingRules forDivision . . . . . . . . . . . . . . . . . . . 141
4.4 Assignment fromaBracedListofValues . . . . . . . . . . . . 145
4.9 sizeofAppliedtoaClassMember . . . . . . . . . . . . . . . 157
5.4.3 Range forStatement . . . . . . . . . . . . . . . . . . . . . . . 187
6.2.6 Library initializer_listClass . . . . . . . . . . . . . . . 220
6.3.2 List InitializingaReturnValue . . . . . . . . . . . . . . . . . . 226
6.3.3 Declaring a Trailing Return Type . . . . . . . . . . . . . . . . . 229
6.3.3 Using decltype to Simplify Return Type Declarations . . . . 230
6.5.2 constexprFunctions . . . . . . . . . . . . . . . . . . . . . . . 239
7.1.4 Using = default toGenerateaDefaultConstructor . . . . . 265
7.3.1 In-class Initializers forMembersofClassType . . . . . . . . . 274
7.5.2 DelegatingConstructors . . . . . . . . . . . . . . . . . . . . . . 291
7.5.6 constexprConstructors . . . . . . . . . . . . . . . . . . . . . 299
8.2.1 Using strings for File Names . . . . . . . . . . . . . . . . . . 317
9.1 The array and forward_listContainers . . . . . . . . . . 327
9.2.3 Container cbegin and cendFunctions . . . . . . . . . . . . . 334
9.2.4 List InitializationforContainers . . . . . . . . . . . . . . . . . 336
9.2.5 Container Nonmember swapFunctions . . . . . . . . . . . . . 339
9.3.1 Return Type for Container insertMembers . . . . . . . . . . 344
9.3.1 Container emplaceMembers . . . . . . . . . . . . . . . . . . 345
xix
xx New Features in C++11
9.4 shrink_to_fit . . . . . . . . . . . . . . . . . . . . . . . . . . 357
9.5.5 Numeric Conversion Functions for strings . . . . . . . . . . 367
10.3.2 LambdaExpressions . . . . . . . . . . . . . . . . . . . . . . . . 388
10.3.3 Trailing Return Type in Lambda Expressions . . . . . . . . . . 396
10.3.4 The Library bindFunction . . . . . . . . . . . . . . . . . . . . 397
11.2.1 List Initializationof anAssociativeContainer . . . . . . . . . . 423
11.2.3 List Initializing pairReturnType . . . . . . . . . . . . . . . . 427
11.3.2 List Initialization of a pair . . . . . . . . . . . . . . . . . . . . 431
11.4 TheUnorderedContainers . . . . . . . . . . . . . . . . . . . . 443
12.1 SmartPointers . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
12.1.1 The shared_ptrClass . . . . . . . . . . . . . . . . . . . . . . 450
12.1.2 List InitializationofDynamicallyAllocatedObjects . . . . . . 459
12.1.2 autoandDynamicAllocation . . . . . . . . . . . . . . . . . . 459
12.1.5 The unique_ptrClass . . . . . . . . . . . . . . . . . . . . . . 470
12.1.6 The weak_ptrClass . . . . . . . . . . . . . . . . . . . . . . . . 473
12.2.1 Range for Doesn’t Apply to Dynamically Allocated Arrays . 477
12.2.1 List InitializationofDynamicallyAllocatedArrays . . . . . . 478
12.2.1 autoCan’tBeUsedtoAllocateanArray . . . . . . . . . . . . 478
12.2.2 allocator::constructCanUseanyConstructor . . . . . 482
13.1.5 Using = default forCopy-ControlMembers . . . . . . . . . 506
13.1.6 Using = delete toPreventCopyingClassObjects . . . . . . 507
13.5 MovingInsteadofCopyingClassObjects . . . . . . . . . . . . 529
13.6.1 RvalueReferences . . . . . . . . . . . . . . . . . . . . . . . . . 532
13.6.1 The Library moveFunction . . . . . . . . . . . . . . . . . . . . 533
13.6.2 MoveConstructor andMoveAssignment . . . . . . . . . . . . 534
13.6.2 Move Constructors Usually Should Be noexcept . . . . . . . 535
13.6.2 MoveIterators . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
13.6.3 ReferenceQualifiedMemberFunctions . . . . . . . . . . . . . 546
14.8.3 The functionClassTemplate . . . . . . . . . . . . . . . . . . 577
14.9.1 explicitConversionOperators . . . . . . . . . . . . . . . . 582
15.2.2 overrideSpecifier forVirtualFunctions . . . . . . . . . . . . 596
15.2.2 Preventing Inheritance by Defining a Class as final . . . . . 600
15.3 override and final Specifiers for Virtual Functions . . . . 606
15.7.2 DeletedCopyControl andInheritance . . . . . . . . . . . . . . 624
15.7.4 InheritedConstructors . . . . . . . . . . . . . . . . . . . . . . . 628
16.1.2 DeclaringaTemplateTypeParameteras aFriend . . . . . . . 666
16.1.2 TemplateTypeAliases . . . . . . . . . . . . . . . . . . . . . . . 666
16.1.3 DefaultTemplateArguments forTemplateFunctions . . . . . 670
16.1.5 ExplicitControlof Instantiation. . . . . . . . . . . . . . . . . . 675
16.2.3 Template Functions and Trailing Return Types . . . . . . . . . 684
16.2.5 ReferenceCollapsingRules . . . . . . . . . . . . . . . . . . . . 688
16.2.6 static_cast fromanLvaluetoanRvalue . . . . . . . . . . 691
16.2.7 The Library forwardFunction . . . . . . . . . . . . . . . . . . 694
16.4 VariadicTemplates . . . . . . . . . . . . . . . . . . . . . . . . . 699
16.4 The sizeof...Operator . . . . . . . . . . . . . . . . . . . . . 700
16.4.3 VariadicTemplates andForwarding . . . . . . . . . . . . . . . 704
New Features in C++11 xxi
17.1 The Library TupleClassTemplate . . . . . . . . . . . . . . . . 718
17.2.2 New bitsetOperations . . . . . . . . . . . . . . . . . . . . . 726
17.3 TheRegularExpressionLibrary . . . . . . . . . . . . . . . . . 728
17.4 TheRandomNumberLibrary . . . . . . . . . . . . . . . . . . 745
17.5.1 Floating-Point FormatControl . . . . . . . . . . . . . . . . . . 757
18.1.4 The noexceptExceptionSpecifier . . . . . . . . . . . . . . . . 779
18.1.4 The noexceptOperator . . . . . . . . . . . . . . . . . . . . . 780
18.2.1 InlineNamespaces . . . . . . . . . . . . . . . . . . . . . . . . . 790
18.3.1 InheritedConstructors andMultiple Inheritance . . . . . . . . 804
19.3 Scoped enums . . . . . . . . . . . . . . . . . . . . . . . . . . . . 832
19.3 Specifying the Type Used to Hold an enum . . . . . . . . . . . 834
19.3 Forward Declarations for enums . . . . . . . . . . . . . . . . . 834
19.4.3 The Library mem_fnClassTemplate . . . . . . . . . . . . . . . 843
19.6 UnionMembersofClassTypes . . . . . . . . . . . . . . . . . . 848

编辑推荐

《C++ Primer英文版(第5版)》编辑推荐:最新标准C++11发布,距上一版本已10年;《C++ Primer英文版(第5版)》是唯一持续更新的全球顶级C++读本。多位深孚众望的大师组合堪称绝无仅有;经过前四个版本积累,第5版的体例堪称完美。这一版本作者历时3年完成,极力避免在原版上升级,而是将C++11的新特性真正融入各章节;更将所有代码示例用C++ 11的简化写法完成,而不是仅单独增加内容。

作者简介

这本久负盛名的C++经典教程,时隔八年之久,终迎来史无前例的重大升级。除令全球无数程序员从中受益,甚至为之迷醉的——C++大师Stanley B. Lippman的丰富实践经验,C++标准委员会原负责人Josée Lajoie对C++标准的深入理解,以及C++先驱Barbara E. Moo在C++教学方面的真知灼见外,更是基于全新的C++11标准进行了全面而彻底的内容更新。非常难能可贵的是,书中所有示例均全部采用C++11标准改写,这在经典升级版中极其罕见——充分体现了C++语言的重大进展极其全面实践。书中丰富的教学辅助内容、醒目的知识点提示,以及精心组织的编程示范,让这本书在C++领域的权威地位更加不可动摇。无论是初学者入门,或是中、高级程序员提升,本书均为不容置疑的首选。


 C++ Primer 英文版(第 5 版)下载 精选章节试读 更多精彩书评



发布书评

 
 


精彩书评 (总计2条)

  •     完整版参考答案:https://github.com/jieniyimiao/CppPrimer完整版参考答案:https://github.com/jieniyimiao/CppPrimer完整版参考答案:https://github.com/jieniyimiao/CppPrimer完整版参考答案:https://github.com/jieniyimiao/CppPrimer完整版参考答案:https://github.com/jieniyimiao/CppPrimer
  •     有阵子没摸C++,也不太了解C++11,就买了这本经典,大概一个月的茶余饭后读了一遍,确实好。这本书讲的很细致,比如container, algorithm-iterato的关系,条理清楚。如前面评论所述,很多C++入门书籍是先讲C,再加入C++的内容。这本书把过时的东西放到边边角角,直接讲C++,有助于养成正确的思考习惯。个人体会,相比C#等GC的“懒人语言”,C++语言更为严谨,概念上更细分、精确,从而要求更多的思考。这本书在copy control方面,解释的很详细。C++11的主要新特点是加入了rValue reference,连同move, ref collapsing, forwarding一起。还有一个实用的smart pointer。这本书都讲到了。文中前后引用也很全,很适合这种知识点繁多的书,值得称赞。唯一要提的意见,是对于concurrency方面似乎没讲。

精彩短评 (总计50条)

  •     不愧是C++经典书籍,太棒了,仔细阅读
  •     C++11 大法好。template 是黑魔法。rvalue reference 平添多少烦恼……
  •     读英文版比中文版慢好多,但是理解更深刻。
  •     在看,书很好,纸也不错,值得购买。
  •     awesome, especially C++11 features is well introduced.
  •     水太深,呛数口,喝饱
  •     挺厚实-------------
  •     面面俱到,但是需要经常翻翻
  •     经典教材。
  •     书本是正版,材质还好。
  •     自己曾经把这本书的电子版打印出来了,不过看到第五版被引进了,果断买之了作者把C++11的特性也做了具体的介绍,对于新特性也作了一些改动。重新温习了一下C++,突然发现自己好多都不知道或者以前知道的都忘了。这本书和国内的书有很大的不同,国内的c++都是按C讲的,而这本书是先讲解类的一些特性,强调使用c++的标准库,避免使用c的指针及数组之类的,上c++课,我就觉得我们老师讲的不是c++,而是c。模板,异常,标准库都没有讲,所以导致我现在需要用的时候不得不去自学,这本书给了我很大的启发!
  •     C++ 入门必读
  •     第一次啃下了一本英文技术书
  •     C++的语义无甚出奇之处,但是继承自C的语法已经被其搞的很奇葩了。
  •     并不适合作为初学者的入门书
  •     在读:这么厚 要我命 读过:果然要了我命,看这书的过程我呆过的城市包括奥兰多、迈阿密、许昌、成都、纽约和坎昆
  •     详实,需多读
  •     实习的时候浏览过第4版,实习结束随时就忘得差不多了。这次硬着头皮想把第5版的英文版好好读读,但是最后又是浏览了一遍,这书实在是太厚了。不过尽管是浏览,也感觉又理解到一些新东西,看来有机会的话要把不少经典书反复读读了。
  •     书很不错,是正品。以最新的c++11标准写的,大爱。
  •     改写了挺多细节,话说point那章真是改了又改。。。我司何时才能用上C++ 11呢
  •     排版满意,纸张还行。
  •     屌爆了有没有...什么叫做深入浅出啊我草
  •     深入细节,解释得也很到位。
  •     很好的书,电工出版质量好
  •     书的外观有破损,正品
  •     傲慢与偏见 Lippman 这个在无穷维度无视template、multithread、atomic instruction的行为注定会激怒很多人。 如果你是靠此书初学C++,那么你以后也很可能带着Lippman的偏见驰骋沙场了= =
  •     温故而知新,一直零零散散阅读了不少c++0x的特性,这次终于系统的看了一下了。
  •     又看了一遍英文版的,收获颇丰,真是常看常新,每次都会有不同的收获
  •     C++ Primer, 5th ed. is a great book for an intermediate level C++ programmer. I would NOT recommend this book to a novice, but if you went through an introductory book or two and have some basic programming experience, this book will take you to the next level.The authors introduce STL material from the beginning. So, this book more or less shares pedagogical philosophy with another excellent introductory book "Accelerated C++" by Koenig & Moo (who is a co-author of this book). IMO, this is a superior approach, compared to a more traditional, part1-C-part2-C++ type of appro...ach.What I like about this book, in particular, is the authors' attention to detail and their pursuit of "completeness". Not only does the book cover all the basic building blocks of the language, it also describes subtleties and nuances in the language that can easily be missed or misunderstood by showing you lots of easy-to-understand examples. In this sense, I would say that this book contains most of the materials covered in topical books such as "Effective C++: 55 ways..." by S. Meyers. Althought Meyers' book is a decent one on its own, I feel like you wouldn't really need to read Meyers' book if you go through this book patiently.Well, what I described so far doesn't differ much from what you can find in other reviews for the previous editions. However, newly added materal on the new C++11 extension certainly justifies new edition. The authors give clear explantion of new addtions (such as auto type, decltype, list initialization, rvalue reference, move operator, lambda expression, shared/unique pointers, just to name a few). These new materials are repeatedly used throughout the book, so you will feel very comfortable with these by the time you finish the book. This C++11 extension alone, in my opinion, justifies the price of the book.The book is in its fifth edition, and this shows in the book's clean, organic structure. Fonts and spaces are perfect for a programming book, and cross referencing is very nicely documented so that you can remind yourself of your previous reading 2 weeks ago. Examples are succint and easy to understand, although they tend to be independent snippets of codes rather than parts of a grand scheme.All in all, this is a great intermediate level book on C++. It is the kind of book that you want to keep in your shelf as a reference. I highly recommend this book to those who want to learn C++. 阅读更多 ›
  •     书不错,经典教材。。。非常棒
  •     适合所有想学C++的准程序员。哪本书的大体内容都是一样的,但是编排的方式各有特色,就会让初学者学习起来难易程度不一样。第五版是根据C++11标准编写的,且在内容安排上尽可能让初学者有很好的编码规范,而不是穷尽知识条目,这一点非常的好。比如摒弃数组,在没有开篇模版,STL和动态内存情况下介绍向量容器,就能够看出作者的用心良苦。
  •     终于在C++14推出之后读过介绍C++11的第五版了
  •     这本书把c++11新标准融汇进去了,写的不错
  •     第二本看过的英文版书籍
  •     c++这个语言真是槽点无数。BS想在c的基础上扩展一个直接面向硬件又能zero-overhead abstraction的语言,这种想法无可避免导致了c++是一个细节琐碎,写法啰嗦,容易跑飞的语言。PS这本书章节编排有问题,读起来不够流畅,不过仍然是本挺好的学c++的书
  •     不论是作为基础入门,还是深入理解,还是作为参考手册,都是非常好的选择。
  •     血泪史啊,为什么没有早一点读完这本书???这本书才是真正的XX天入门C++!!!
  •     Part I&III,C++ 真复杂
  •     本书系统性强,且有深度。
  •     要学c++从这儿开始,可以少走弯路。要学现代c++,这本书是完全重新编排的,c++11特性融入各个章节,而不是挂羊头卖狗肉一章到最后给你提点c++11。有了这本书,c++入门书籍任何半本都是多余。
  •     我买的计算机专业的第一本书。还是大一的时候买的,最近半个月终于把他彻底的看了一边,少量章节跳过了,如io,stl。感觉前面部分讲的非常好,但15章面向对象编程这章感觉不尽如人意,有点乱,不容易看懂,所以要配合一些其他书一起来看,这也是字典级别书的通病,知识点很全,但是讲的不够透彻,不过说过来,我觉得这本书还是很值得一读的,但看完这本书完全不够,还要看如深入探索c++模型,effective c++等,此时可以回头查这本字典,这样效果会非常好。
  •     大一时就学的C++,不过那时有些把C++当C来学了。前几天在看C++ primer第四版(陈硕评注版),越读越发现自己对C++的认识很是浅薄。这个学期在学编译原理课程,看此书时比较关注编译器做了哪些事情,而链接器又做了哪些事情。这些东西不知道的话,就不会明白声明与定义到底是什么区别,为什么类的定义可以写在头文件中;也不会懂为什么程序可以分开编译,为什么要写Makefile。前几天看到这本书的第五版影印版已经引进,果断出手入了一本。第五版和第四版的主要区别是加进了C++11的新内容以及新的标准库,比如智能指针,lambda表达式,正则表达式等。今天粗略翻了一下之前看的第五版中关于头文件的部分,没有看到第四版中强调的const variable定义为什么可以写在头文件中的内容,有点奇怪。不知道是第五版书把内容精简了,还是我看漏掉了。无论如何,这部书是一部极好的C++语言书籍,绝对的经典著作。
  •     经典C++
  •     我也买了中文版,觉得译得还行,如果你像我一样是非计算机专业的,只是想学会使用C++,那么中文版应该足够,且更节省阅读时间。
  •     完整版参考答案:https://github.com/jieniyimiao/CppPrimer
  •     说起来这本书内容很多,每个都比较浅显,许多细节要自己去摸索,目前只粗略看了正文,附录好像也有很多内容,希望有时间再看
  •     第一遍读,感觉对我这样的初学者很有帮助,实例也很多
  •     看过中文的第四版,现在看看英文版,感觉很不错
  •     有点厚
  •     可曾想过我居然会读这本书...
 

外国儿童文学,篆刻,百科,生物科学,科普,初中通用,育儿亲子,美容护肤PDF图书下载,。 零度图书网 

零度图书网 @ 2024