本文导航(全文17000字左右,按正常阅读速度,大概需要35-55分钟)
Notes to the Readers 写给读者
When the terrian disagrees with the map 当实际地形与地图标定不符时
trust the terrian. 相信地形 1
——Swiss army proverb 瑞士军队格言
This chapter is a grab bag of information; it aims to give you an idea of what to expect from the rest of the book.
这一章是个信息大杂烩,是为了让你了解本书的全貌。
Please skim through it and read what you find interesting.
你可以快速略读并找到你感兴趣的地方仔细阅读。
A teacher will find most parts immediately useful.
老师可以快速找到重点内容。
If you are reading this book without the benefit of a good teacher, please don’t try to read and understand everything in this chapter; just look at “The structure of this book” and the first part of the “A philosophy of teaching and learning” sections.
如果你没有老师的辅助,那么不要试图去读和理解这章的全部内容;只需要看「本书结构」和「教学和学习的哲学的第一部分」。
You may want to return and reread this chapter once you feel comfortable writing and executing small programs.
等到你能轻松地编写并执行一些小程序之后可以再回来重读本章。
0.1 The structure of this book 本书结构
This book consists of four parts and a collection of appendices:
本书分成4个部分和一系列附录:
- Part I, “The Basics,” presents the fundamental concepts and techniques of programming together with the C++ language and library facilities needed to get started writing code. This includes the type system, arithmetic operations, control structures, error handling, and the design, implementation, and use of functions and user-defined types.
- 第一部分,「基础」, 介绍了编程的基本概念和方法和编程所需的C++语言和库的特性。这部分内容包括类型系统,算术运算,控制结构,错误处理和设计,实现以及函数的使用和用户自定义类型。
- Part II, “Input and Output,” describes how to get numeric and text data from the keyboard and from files, and how to produce corresponding output to the screen and to files. Then, it shows how to present numeric data, text, and geometric shapes as graphical output, and how to get input into a program from a graphical user interface (GUI).
- 第二部分,「输入和输出」, 介绍了如何从键盘和文件获取输入,如何向屏幕和文件进行输出。怎样表示数值类型的数据,文本和几何图形,如何通过GUI获取输入。
- Part III, “Data and Algorithms,” focuses on the C++ standard library’s containers and algorithms framework (the STL, standard template library). It shows how containers (such as
vector
,list
, andmap
) are implemented (using pointers, arrays, dynamic memory, exceptions, and templates) and used. It also demonstrates* the design and use of standard library algorithms (such assort
,find
, andinner_product
). - 第三部分,「数据和算法」,介绍C++标准库容器和算法框架(标准库,标准模板库)。展示容器(例如
vector
,list
和map
)是怎样通过各种方式(指针,数组,动态内存,exceptions和模板)实现的。还阐明了标准库算法的设计与使用方法(比如sort
,find
和inner_product
) - Part IV, “Broadening the View,” offers a perspective on programming through a discussion of ideals and history, through examples (such as matrix computation, text manipulation, testing, and embedded systems programming), and through a brief description of the C language.
- 第四部分,「拓展内容」, 从编程思想和历史这个不同的角度来介绍编程,通过举例(矩阵计算,文本处理,测试,嵌入式编程),并简要地介绍C语言。
- Appendices provide useful information that doesn’t fit into a tutorial presentation, such as surveys of C++ language and standard library facilities, and descriptions of how to get started with an integrated development environment (IDE) and a graphical user interface (GUI) library.
- 附录中包含了一些入门课程不涵盖的内容,比如C++语言和标准库,以及如何在IDE中开始编程和GUI库介绍。
Unfortunately, the world of programming doesn’t really fall into four cleanly separated parts
然而,真实的编程并不是能清晰地分出几个独立的部分的。
Therefore, the “parts” of this book provide only a coarse classification of topics.
所以本书只是提供一个粗略的分类。
We consider it a useful classification (obviously, or we wouldn’t have used it), but reality has a way of escaping neat classifications.
我们想要好好分类,但很显然,复杂的现实情况总会有一种方式不符合分类。
For example, we need to use input operations far sooner than we can give a thorough explanation of C++ standard I/O streams (input/output streams).
比如,我们需要在详细介绍C++的I/O流之前就要用到一些输入操作。
Where the set of topics needed to present an idea conflicts with the overall classification, we explain the minimum needed for a good presentation, rather than just referring to the complete explanation elsewhere.
当有类似这种需求与严格遵守分类相互矛盾的情况出现时,我们会先满足最小的使用需求而介绍一点点儿,而非等到后面再给出完整的解释。
Rigid classifications work much better for manuals than for tutorials.
粗略的分类比起手册式的按部就班,更适合作为教程。
The order of topics is determined by programming techniques, rather than programming language features; see §0.2. For a presentation organized around language features, see Appendix A.
章节内容安排是基于编程方法,而非语言特性(0.2节);如果想看与特性,请看附录A。
To ease review and to help you if you miss a key point during a first reading where you have yet to discover which kind of information is crucial, we place three kinds of “alert markers” in the margin:
- Blue: concepts and techniques (this paragraph is an example of that)
- Green: advice
- Red: warning
为了当你忘记一个重点可以方便回来查询,我会在页边使用三种「警告标识」:
- 蓝色:概念和方法(这一段就是)
- 绿色:建议
- 红色:警告
0.0.1 General Approach 一般方法
In this book, we address* you directly. That is simpler and clearer than the conventional “professional” indirect form of address, as found in most scientific papers.
在本书中,我们会直接给你讲解内容,这样会比传统的,「专业」的,间接的,常见于学术论文中的讲解方式更加简洁明了。
By “you” we mean “you, the reader,” and by “we” we refer either to “ourselves, the author and teachers,” or to you and us working together through a problem, as we might have done had we been in the same room.
我说的「给你讲解」的「你」是指「正在读书的你」,而「我们」,要么是指「作为作者的我和老师」,要么是指「共同解决问题的你我」。
This book is designed to be read chapter by chapter from the beginning to the end.
这本书被设计成从头到尾一章一章地读。
Often, you’ll want to go back to look at something a second or a third time.
有时候,你可能会前后翻翻去回看一些内容。
In fact, that’s the only sensible approach, as you’ll always dash past some details that you don’t yet see the point in. In such cases, you’ll eventually go back again.
事实上,这才符合现实情况,你经常会读得很快,错过一些细节。这时你总要往回翻书。
However, despite the index and the cross-references, this is not a book that you can open to any page and start reading with any expectation of success.
但除了索引和交叉引用那几页,这并不是那种你随便翻到哪页都可以开始的书。
Each section and each chapter assume understanding of what came before.
每个部分,每一章节都是和之前内容环环相扣的。
Each chapter is a reasonably self-contained unit, meant to be read in “one sitting” (logically, if not always feasible on a student’s tight schedule). That’s one major criterion for separating the text into chapters.
每一章节在逻辑上都是一个完善的单元,应该「坐下来一口气读完」(当然了,如果日程安排比较紧张,做到这点可能比较难)。这是把正文分成章节的一种标准。
Other criteria include that a chapter is a suitable unit for drills and exercises and that each chapter presents some specific concept, idea, or technique.
另一种标准则是每个章节介绍特定的概念,思想和方法,配合练习题组成一个供学生深入研究的单元。
This plurality of criteria has left a few chapters uncomfortably long, so please don’t take “in one sitting” too literally.
这种标准会导致几个章节内容过于繁多,所以不要「一口气读完」。
In particular, once you have thought about the review questions, done the drill, and worked on a few exercises, you’ll often find that you have to go back to reread a few sections and that several days have gone by.
还有一种情况,当你做到复习题,思考题和编程练习时,你总会发现要回顾一下几天之前读到的上一个部分的内容。
We have clustered the chapters into “parts” focused on a major topic, such as input/output. These parts make good units of review.
对于这种情况,我们把同一类的各个章节放在一起组成一个主题,比如输入/输出,这样方便你做复习。
Common praise for a textbook is “It answered all my questions just as I thought of them!” That’s an ideal for minor technical questions, and early readers have observed the phenomenon with this book. However, that cannot be the whole ideal.
人们总是会夸一本书「啊对对对!我就是这么想的,我想得和答案一样」。这对次要的技术问题是一个理想的状态,初学者在读这本书或多或少都有此现象。但这并不是完全理想的。
We raise questions that a novice would probably not t