[翻译]《Programming - Principles and Practice Using C++, Second Edition》- Chapter 0

本文导航(全文17000字左右,按正常阅读速度,大概需要35-55分钟)

  • Notes to the Readers 写给读者
    • 0.1 The structure of this book 本书结构
      • 0.0.1 General Approach 一般方法
      • 0.1.2 Drills, exercises, etc. 练习,编程题,其它
      • 0.1.3 What comes after this book? 学完这本书接下来该学什么?
    • 0.2 A philosophy of teaching and learning 教与学的哲学
      • 0.2.1 The order of topics 专题的顺序
      • 0.2.2 Programming and programming language 编程和编程语言
      • 0.2.3 Portability 可移植性
    • 0.3 Programming and computer science 编程和计算机科学
    • 0.4 Creativity and problem solving 创造力和问题解决能力
    • 0.5 Request for feedback 反馈
    • 0.6 References 引用
    • 0.7 Biographies 作者简介

书籍封面

Notes to the Readers 写给读者

When the terrian disagrees with the map 当实际地形与地图标定不符时
trust the terrian. 相信地形 1
——Swiss army proverb 瑞士军队格言
[翻译]《Programming - Principles and Practice Using C++, Second Edition》- Chapter 0_第1张图片

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, and map) are implemented (using pointers, arrays, dynamic memory, exceptions, and templates) and used. It also demonstrates* the design and use of standard library algorithms (such as sort, find, and inner_product).
  • 第三部分,「数据和算法」,介绍C++标准库容器和算法框架(标准库,标准模板库)。展示容器(例如vectorlistmap)是怎样通过各种方式(指针,数组,动态内存,exceptions和模板)实现的。还阐明了标准库算法的设计与使用方法(比如sortfindinner_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 think of. We aim to ask and answer questions that you need to consider when writing quality software for the use of others.
我们会提出初学者可能很难想出来的问题。我们意在让学生必须意识到要做出对他人有用的高质量软件,才能回答我们提出的问题。

Learning to ask the right (often hard) questions is an essential part of learning to think as a programmer. Asking only the easy and obvious questions would make you feel good, but it wouldn’t help make you a programmer.
学会去问正确的(往往也是困难)的问题是学会像程序员一样思考的本质。只问一些简单明显的问题可能会让学生觉得舒服,但无助于学生成为好程序员。


We try to respect your intelligence and to be considerate about your time.
我们会尽力去照顾你的智商并考虑你拥有多少时间。

In our presentation, we aim for professionalism rather than cuteness, and we’d rather understate a point than hype it.
我们的表达会做到专业而非戏言,对重点的讲解也追求通俗易懂而非拔高晦涩。

We try not to exaggerate the importance of a programming technique or a language feature, but please don’t underestimate a simple statement like “This is often useful.”
我们不会过分夸大一种编程方法和语言特性的重要性,但你也不要过分低估被评价为「这玩意儿很有用」的简单语句。

If we quietly emphasize that something is important, we mean that you’ll sooner or later waste days if you don’t master it
如果我们强调一些东西很重要,我们的意思是如果你不能掌握它,你将会卡住好几天。

Our use of humor is more limited than we would have preferred, but experience shows that people’s ideas of what is funny differ dramatically and that a failed attempt at humor can be confusing.
尽管我们想把书写得幽默,但我会控制抖机灵的次数,因为经验告诉我,如果玩笑开得好,学生印象会很深刻,但一旦拉了,学生会很困惑。


We do not pretend that our ideas or the tools offered are perfect.
我们不会撒谎我们传授的思想和方法是完美无误的。

No tool, library, language, or technique is “the solution” to all of the many challenges facing a programmer. At best, it can help you to develop and express your solution.
反之,没有任何一种工具,库,语言和技术可以完美适合解决程序员遇到的各种难题。它们只能帮助你改进和表达出你的解法。

We try hard to avoid “white lies”; that is, we refrain from oversimplified explanations that are clear and easy to understand, but not true in the context of real languages and real problems.
我们也避免撒善意的谎言;也就是说,我们不会对清楚明了的简单概念进行过度的二次解释,因为过于简化根本不适用于真实的语言和问题的复杂度。

On the other hand, this book is not a reference; for more precise and complete descriptions of C++, see Bjarne Stroustrup, The C++ Programming Language, Fourth Edition (Addison-Wesley, 2013), and the ISO C++ standard.
这本书并不是一本参考手册,如果你需要更精确和完整的C++描述,请看我的另一本书:Bjarne Stroustrup, The C++ Programming Language, Fourth Edition (Addison-Wesley, 2013) 和 ISO的C++标准。

[翻译]《Programming - Principles and Practice Using C++, Second Edition》- Chapter 0_第2张图片

0.1.2 Drills, exercises, etc. 练习,编程题,其它

Programming is not just an intellectual activity, so writing programs is necessary to master programming skills. We provide two levels of programming practice:
编程并不只是智力活动,还需付出体力进行大量的编程练习,我们提供两个水平编程练习:

  • Drills 2 : A drill is a very simple exercise devised to develop practical, almost mechanical skills. A drill usually consists of a sequence of modifications of a single program. You should do every drill. A drill is not asking for deep understanding, cleverness, or initiative. We consider the drills part of the basic fabric of the book. If you haven’t done the drills, you have not “done” the book.
  • Drills:drill是为了开发实践,机械性的小练习。通常包含程序改错题。你应该把每道drill都做一遍。drill不需要深入理解,不需要调动太多智商。我们把drills看做本书的一种基本组成。如果你不做drill,那么相当于你没看过这本书。
  • Exercises: Some exercises are trivial and others are very hard, but most are intended to leave some scope for initiative and imagination. If you are serious, you’ll do quite a few exercises. At least do enough to know which are difficult for you. Then do a few more of those. That’s how you’ll learn the most. The exercises are meant to be manageable without exceptional cleverness, rather than to be tricky puzzles. However, we hope that we have provided exercises that are hard enough to challenge anybody and enough exercises to exhaust even the best student’s available time. We do not expect you to do them all, but feel free to try.
  • Exercises:一些exercise难度一般,一些则非常难,但是大多数都有铺垫并调动想像。如果你想认真对待学习,你可以做大部分练习题。至少知道哪些题对于你来说是难的,然后也尝试做一点儿难题。这样你才可以学到更多。这些练习被设计成不需要穷尽智商就可以解的难度,而不是一些嘎咕的偏题怪题。然而,我们希望通过练习让任何学生得到挑战,所以题量足以榨干最好学生的所有时间。我们不要求你把所有题都做完,但你要尽力去尝试。

In addition, we recommend that you (every student) take part in a small project (and more if time allows for it).
除了练习之外 ,我们还推荐每个学生都要做一个小项目(如果时间允许,尽量扩大项目的规模)。

A project is intended to produce a complete useful program. Ideally, a project is done by a small group of people (e.g., three people) working together for about a month while working through the chapters in Part III.
一个项目的目标是产出有用的程序。最理想的是,开始学第三部分之后,一个项目一群人合作(最好3个人)以一个月为期限。

Most people find the projects the most fun and what ties everything together.
大部分人都会发现项目很有趣。


Some people like to put the book aside and try some examples before reading to the end of a chapter; others prefer to read ahead to the end before trying to get code to run.
一些人习惯不看书直接上手尝试;而一些人喜欢先把书读完在去动手编码。

To support readers with the former preference, we provide simple suggestions for practical work labeled “Try this” at natural breaks in the text.
对于前者,我们在文中某处会提供一些标记为「Try this」的小实践。

A Try this is generally in the nature of a drill focused narrowly on the topic that precedes it.
Try this实际上就是一种drill,不过只限于将要讲的主题。

If you pass a Try this without trying — maybe because you are not near a computer or you find the text riveting — do return to it when you do the chapter drill; a Try this either complements the chapter drill or is a part of it.
如果你跳过了Try this——也许当时你没在电脑前——那么当你读完本章时,你要把它当作drill做完。


At the end of each chapter you’ll find a set of review questions. They are intended to point you to the key ideas explained in the chapter
在每章章末都有一些复习题。它们帮助你回顾本章的重点概念。

One way to look at the review questions is as a complement to the exercises: the exercises focus on
the practical aspects of programming, whereas the review questions try to help you articulate the ideas and concepts. In that, they resemble good interview questions.
可以把复习题看做是exercise的补充:exercise着重于编程的实践,而复习题着重于巩固概念和思想。它们放在一起就是很好的面试题。


The “Terms” section at the end of each chapter presents the basic vocabulary of programming and of C++. If you want to understand what people say about programming topics and to articulate your own ideas, you should know what each means.
每章的「术语」提供了C++编程的一些基本词汇。如果你想要知道别人在说什么,你需要掌握这些词汇的含义。


Learning involves repetition. Our ideal is to make every important point at least twice and to reinforce it with exercises
学习需要重复。我们每个重点都会重复两遍,并且通过练习来巩固。

0.1.3 What comes after this book? 学完这本书接下来该学什么?

At the end of this book, will you be an expert at programming and at C++? Of course not!
读完这本书,你能成为C++专家么?当然不能!

When done well, programming is a subtle, deep, and highly skilled art building on a variety of technical skills. You should no more expect to be an expert at programming in four months than you should expect to be an expert in biology, in math, in a natural language (such as Chinese, English, or Danish), or at playing the violin in four months — or in half a year, or a year
编程是一项精密,深奥,需要很高构建的水平,需要很多技术的一项工作。你不能指望在四个月内学会编程,就像你不能在四个月里学会生物,数学,一门语言或者小提琴那样,半年不行,甚至一年都不行。

What you should hope for, and what you can expect if you approach this book seriously, is to have a really good start that allows you to write relatively simple useful programs, to be able to read more complex programs, and to have a good conceptual and practical background for further work.
但不要觉得灰心,好好跟着这本书学,对你的编程事业是一个好的开始,起初可能你只会写一些简单的实用程序,但慢慢你可以读更复杂的程序,这本书将为你未来的工作打下理论和实践的基础。

The best follow-up to this initial course is to work on a real project developing code to be used by someone else.
这门课最好的后续就是参与一个实际项目的开发。

After that, or (even better) in parallel with a real project, read either a professional-level general textbook (such as Stroustrup, The C++ Programming Language), a more specialized book relating to the needs of your project (such as Qt for GUI, or ACE for distributed programming), or a textbook
focusing on a particular aspect of C++ (such as Koenig and Moo, Accelerated C++; Sutter’s Exceptional C++; or Gamma et al., Design Patterns).
开发项目的同时,你可以读一些专业水平的教材,比如Stroustrup, The C++ Programming Language,或者根据你的项目的专题书籍(比如讲Qt GUI的,讲ACE分布式编程),或者特定主题的教材,比如Koenig and Moo, Accelerated C++,Sutter’s Exceptional C++,Gamma et al., Design Patterns

[翻译]《Programming - Principles and Practice Using C++, Second Edition》- Chapter 0_第3张图片
For more references, see §0.6 or the Bibliography section at the back of the book.
更多书籍,请看0.6节。


Eventually, you should learn another programming language. Eventually, you should learn another programming language. We don’t con-
sider it possible to be a professional in the realm of software — even if you are not
primarily a programmer — without knowing more than one language
还有就是要学一门其他语言。我们不认为之掌握一门语言就可以胜任软件开发的工作,哪怕你不是编程岗位。

0.2 A philosophy of teaching and learning 教与学的哲学

What are we trying to help you learn? And how are we approaching the process of teaching? We try to present the minimal concepts, techniques, and tools for you to do effective practical programs, including
我们怎么帮你学习C++?我们采取怎样的教学方式?我们尽量保证概念,技术,工具的精简以便于让你高效编程。

  • Program organization 程序结构
  • Debugging and testing debug和测试
  • Class design 类设计
  • Computation 计算
  • Function and algorithm design 函数和算法设计
  • Graphics (two-dimensional only) 二维图形
  • Graphical user interfaces (GUIs) 图形用户界面
  • Text manipulation 文本处理
  • Regular expression matching 正则表达式
  • Files and stream input and output (I/O) 文件和输入/输出流
  • Memory management 内存管理
  • Scientific/numerical/engineering calculations 科学/数值/工程计算
  • Design and programming ideals 设计和编程思想
  • The C++ standard library 标准库
  • Software development strategies 软件开发策略
  • C-language programming techniques C语言

Working our way through these topics, we cover the programming techniques called procedural programming (as with the C programming language), data abstraction, object-oriented programming, and generic programming
贯穿这些主题,我们始终采用以下几种编程方法:过程化编程(C语言),数据抽象,面向对象编程,泛型编程。

The main topic of this book is programming, that is, the ideals, techniques, and tools of expressing ideas in code
这本书的主题是编程——即讲解通过代码表达想法的相关思想,方法和工具。

The C++ programming language is our main tool, so we describe many of C++’s facilities in some detail. But please remember that C++ is just a tool, rather than the main topic of this book. This is “programming using C++,” not “C++ with a bit of programming theory.”
C++编程语言是我们的主要工具,所以我们会详细讲解C++相关知识。但请记住C++只是个工具,但本书的主题是编程。是「用C++学习编程」,而不是「学习C++,捎带着学点儿编程知识」。


Each topic we address serves at least two purposes: it presents a technique, concept, or principle and also a practical language or library feature.
我们讲到的每个主题都有两个目标:它既代表方法,概念或者原理,也代表语言中的一个实际语法或库中的一个特性。

For example, we use the interface to a two-dimensional graphics system to illustrate the use of classes and inheritance. This allows us to be economical with space (and your time) and also to emphasize that programming is more than simply slinging code together to get a result as quickly as possible
例如,我们通过二维图形系统接口来讲解类和继承。这节省了我们的文本量和你的时间成本,并且强调了编程并不是把零碎的代码拼接到一起就能快速运行了。

The C++ standard library is a major source of such “double duty” examples — many even do triple duty.
C++标准库中有很多具有类似前文这种「双重指责」的实力——甚至有些具有「多重指责」。

For example, we introduce the standard library vector , use it to illustrate widely useful design
techniques, and show many of the programming techniques used to implement it.
比如,我们会讲到标准库向量,借助它来讲解设计方法,并且会讲解很多实现了标准库 vector 的编程技术。

One of our aims is to show you how major library facilities are implemented and how they map to hardware. We insist that craftsmen must understand their tools, not just consider them “magical.”
我们的一个主要目标是向你展示一些库功能是如何实现的,又是怎么映射到硬件的,我们坚信工匠必须要理解它们的工具,而不是把它们视作「魔法」。


Some topics will be of greater interest to some programmers than to others. However, we encourage you not to prejudge your needs (how would you know what you’ll need in the future?) and at least look at every chapter. If you read this book as part of a course, your teacher will guide your selection.
不同读者对同一个主题的感受是不同的。但我们不推荐你按需读书,也就是觉得有些东西用不上不学了然后只读你感兴趣的部分,你怎么知道某个东西用不上呢?如果你是跟着一门课来看这本书,那你们的老师应该会告诉你看哪个部分。


We characterize our approach as “depth-first.” It is also “concrete-first” and “concept-based.”
我们是「深度优先」,也可以叫「具体优先」或「基于概念」。

First, we quickly (well, relatively quickly, Chapters 1–11) assemble a set of skills needed for writing small practical programs. In doing so, we present a lot of tools and techniques in minimal detail.
首先我们会快速讲(相对快速,1-11章)一些能让你马上上手写简单程序的技能。所以,不会涉及过多的细节。

We focus on simple concrete code examples because people grasp the concrete faster than the abstract. That’s simply the way most humans learn.
我们先从具体的代码示例入手,因为人们相比于抽象,更容易接受具体例子。这是人类学习的最简单途径。

At this initial stage, you should not expect to understand every little detail. In particular, you’ll find that trying something slightly different from what just worked can have “mysterious” effects.
在这个阶段,不要妄图理解所有细节。大胆尝试与之前不同的方法可能会给你的代码带来「神秘」的效果。

Do try, though! And please do the drills and exercises we provide. Just remember that early on you just don’t have the concepts and skills to accurately estimate what’s simple and what’s complicated; expect surprises and learn from them.
要敢于尝试!好好做练习题。起初你可能因为知识和概念的缺乏无法估计什么是难的,什么是简单的,
索性就按部就班,踏踏实实。


We move fast in this initial phase — we want to get you to the point where you can write interesting programs as fast as possible. Someone will argue, “We must move slowly and carefully; we must walk before we can run!”
这个入门阶段我们会进行得很快,因为我们想快速上手编程。一些人可能会说,「没学会走就想跑?」

But have you ever watched a baby learning to walk? Babies really do run by themselves before they
learn the finer skills of slow, controlled walking. Similarly, you will dash ahead, occasionally stumbling, to get a feel of programming before slowing down to gain the necessary finer control and understanding. You must run before you can walk!
可是你看见过小孩刻意地学走路么?小孩都是先会跑,然后才会慢慢地,稳稳地走路。学这本书也是一样,你会先快速学会编一些东西,然后在某个地方卡住,接着才去注意一些语言细节,学习一些编程方法,从而编出好的代码。你必须在学会走之前就会跑!


It is essential that you don’t get stuck in an attempt to learn “everything” about some language detail or technique
还有一点需要强调,不要试图把握所有语言细节和方法!

For example, you could memorize all of C++’s built-in types and all the rules for their use. Of course you could, and doing so might make you feel knowledgeable
比如,你可能会记住所有C++内置类型。当然这个比较好记,这可能会让你有种「学会了」的错觉。

However, it would not make you a programmer. Skipping details will get you “burned” occasionally for lack of knowledge, but it is the fastest way to gain the perspective needed to write good programs.
但这并不能让你成为程序员。跳过细节可能会让你捉襟见肘,但这是获得编程思维的最快的方法。

Note that our approach is essentially the one used by children learning their native language and also the most effective approach used to teach foreign languages.
我们的方法是类似于教小孩子学母语或者教一个人高效学外语的方法的。

We encourage you to seek help from teachers, friends, colleagues, instructors, Mentors, etc. on the inevitable occasions when you are stuck. Be assured that nothing in these early chapters is fundamentally difficult. However, much will be unfamiliar and might therefore feel difficult at first.
我们强烈建议你去寻求帮助,向谁都可以,老师,朋友,同事,助教…请放心,最开始这几章没有什么特别难的地方,只是有些东西太过于陌生,以至于看起来很难。


Later, we build on the initial skills to broaden your base of knowledge and skills. We use examples and exercises to solidify your understanding, and to provide a conceptual base for programming.
后面,我们会帮助你拓展知识和技能,构建基本能力。我们会用例子去巩固你的理解,建立好编程基础。


We place a heavy emphasis on ideals and reasons. You need ideals to guide you when you look for practical solutions — to know when a solution is good and principled.
我们把思想和理由都拿出来重点强调。你需要这些思想来指导你求解问题的过程,来判断一个解法是否是好的,符合逻辑的。

You need to understand the reasons behind those ideals to understand why they should be your ideals, why aiming for them will help you and the users of your code.
你需要理解这些思想背后是什么理由让你应该重视它们,为什么他们能给你用户和你的代码带来帮助。

Nobody should be satisfied with “because that’s the way it is” as an explanation. More importantly, an understanding of ideals and reasons allows you to generalize from what you know to new situations and to combine ideas and tools in novel ways to address new problems.
没人喜欢「由此可见」「由此易得」「显然可推出」这样的解释。理解思想最大的好处就是你具备了一种泛化的能力,能看透新的问题的本质和与之前解决过问题的共性,让你可以很快的以简洁的方式利用好点子和工具解决问题。

Knowing “why” is an essential part of acquiring programming skills. Conversely, just memorizing lots of poorly understood rules and language facilities is limiting, a source of errors, and a massive waste of time. We consider your time precious and try not to waste it.
知其所以然在编程中是非常重要的技能。反之,如果只是机械地记忆大量的规则和语言特性是非常受限的,会导致很多错误,还会浪费大量的时间。我们认为你的是件很珍贵,不该被浪费。


Many C++ language-technical details are banished to appendices and manuals, where you can look them up when needed.
诸如C++的语言细节这一大坨东西,我们都扔到附录和手册里,你可以到那里去查。

We assume that you have the initiative to search out information when needed. Use the index and the table of contents.
我们假设你能主动去查你需要的信息。索引和目录都能帮到你。

Don’t forget the online help facilities of your compiler, and the web. Remember, though, to consider every web resource highly suspect until you have reason to believe better of it. Many an authoritative-looking website is put up by a programming novice or someone with something to sell. Others are simply outdated.
不要忘了你还可以到网上寻求帮助,也可以看编译器返回的信息。不过要记住,考虑到你作为初学者难以判断网络上资源正确与否。有些看起来正经的所谓「编程」网站其实某些人忽悠恰钱的。而有些网站讲得东西早就老掉牙了。

Please don’t be too impatient for “realistic” examples. Our ideal example is the shortest and simplest code that directly illustrates a language facility, a concept, or a technique.
请不要觉得「现实」例子太长不看。我们给出的已经是最短,最简单,又能揭示语言功能,概念和方法的例子了。

Most real-world examples are far messier than ours, yet do not consist of more than a combination of what we demonstrate. Successful commercial programs with hundreds of thousands of lines of code are based on techniques that we illustrate in a dozen 50-line programs.
很多实际案例比我们给出的还杂乱无章,但也不过是由我们所展示的东西组合而成。数十万行代码的成功商业程序是基于我们在十几个50行程序中演示的技术。

The fastest way to understand real-world code is through a good understanding of the fundamentals.
理解现实代码最好的一种方式就是理解基础。

On the other hand, we do not use “cute examples involving cuddly animals” to illustrate our points. We assume that you aim to write real programs to be used by real people, so every example that is not presented as language-technical is taken from a real-world use. Our basic tone is that of professionals addressing (future) professionals.
另一方面,我们不会使用「可爱的有小动物」这样写给幼儿编程的例子。我们假设你是要成为专业程序员的人,所以一切风格与实际专业编程保持一致。我们的宗旨是用我们的专业知识培养下一代专业人才。

0.2.1 The order of topics 专题的顺序

There are many ways to teach people how to program. Clearly, we don’t subscribe to the popular “the way I learned to program is the best way to learn” theories.
教人们编程有很多种方式。需要说清楚,我们不会说「我学编程的方法就是最好的方法」这种事情。

To ease learning, we early on present topics that would have been considered advanced only a few years ago.
为了方便学习,我们很早就提出了几年前还被认为是先进的主题。

Our ideal is for the topics we present to be driven by problems you meet as you learn to program, to flow smoothly from topic to topic as you increase your understanding and practical skills
我们的想法是以问题为驱动学习编程,在主题之间平滑过渡,慢慢增进你的理解和实践技能。

The major flow of this book is more like a story than a dictionary or a hierarchical order.
这本书更像是个故事,而非字典或者分层次的书。

It is impossible to learn all the principles, techniques, and language facilities needed to write a program at once. Consequently, we have to choose a subset of principles, techniques, and features to start with
只写几个程序不可能学会所有原理,方法和语言特性。因此,我们只从众多原理,方法和特性里选取一个子集作为开始。

More generally, a textbook or a course must lead students through a series of subsets. We consider it our responsibility to select topics and to provide emphasis.
一般来说,一本教材或者一门课程应该基于这些子集来进行编排或者教学。我们把选取主题和划重点这个活当作我们自己的责任。

We can’t just present everything, so we must choose; what we leave out is at least as important as what we leave in — at each stage of the journey
我们不可能包含所有东西,所以必须进行筛选;我们难免会遗漏一些重要的东西。

For contrast, it may be useful for you to see a list of (severely abbreviated) characterizations of approaches that we decided not to take:
下面是我们弃用的东西的列表,你先看看:

  • “C first”: This approach to learning C++ is wasteful of students’ time and leads to poor programming practices by forcing students to approach problems with fewer facilities, techniques, and libraries than necessary. C++ provides stronger type checking than C, a standard library with better support for novices, and exceptions for error handling.
  • 「先讲C语言」:这种方法浪费学生大量的时间,并且会迫使学生在缺乏语言特性,方法和库支持的情况下解决问题。C++比C有更强的类型查询,标准库对新手错误处理也很友好。
  • Bottom-up: This approach distracts from learning good and effective programming practices. By forcing students to solve problems with insufficient support from the language and libraries, it promotes poor and wasteful programming practices.
  • 自底向上:这种方法效果不好,效率低。缺点基本同上。
  • “If you present something, you must present it fully”: This approach implies a bottom-up approach (by drilling deeper and deeper into every topic touched). It bores novices with technical details they have no interest in and quite likely will not need for years to come. Once you can program, you can look up technical details in a manual. Manuals are good at that, whereas they are awful for initial learning of concepts.
  • 「如果你说了什么,那就一定要把它说全」:这种方法本质就是一种自底向上的方法(深入到每个细枝末节)。这样会使初学者很快地失去乐趣,感到无聊,还会浪费他们许多年的时间。事实上,编程细节查手册就可以了,没必要挨个去学。手册是个很好的东西,但它不适合作为教材。
  • Top-down: This approach, working from first principles toward details, tends to distract readers from the practical aspects of programming and force them to concentrate on high-level concepts before they have any chance of appreciating their importance. For example, you simply can’t appreciate proper software development principles before you have learned how easy it is to make a mistake in a program and how hard it can be to correct it.
  • 自顶向下:这个方法讲求从原理到细节,起初完全不教给学生实践的细节,只是讲高级的概念。夸张点儿举例就是在你还不会写代码改错的时候就给你讲软件开发的原理。
  • “Abstract first”: Focusing on general principles and protecting the student from nasty real-world constraints can lead to a disdain for real-world problems, languages, tools, and hardware constraints. Often, this approach is supported by “teaching languages” that cannot be used later and (deliberately) insulate students from hardware and system concerns.
  • 「抽象优先」:只谈一般原理而不告诉学生现实条件的限制,妄图建造空中楼阁,纸上谈兵。通常这种方法在涉及硬件和系统编程时就会暴露出其致命之处。
  • “Software engineering principles first”: This approach and the abstract-first approach tend to share the problems of the top-down approach: without concrete examples and practical experience, you simply cannot appreciate the value of abstraction and proper software development practices.
  • 「软件工程原理优先」:这个方法和之前的「抽象优先」一样都算一种「自顶向下」,所以他们都有同样的缺点:没有具体的例子和实践经验,你根本没法体会到抽象的价值,也没法寻找到合适的软件开发路径。
  • “Object-oriented from day one”: Object-oriented programming is one of the best ways of organizing code and programming efforts, but it is not the only effective way. In particular, we feel that a grounding in the basics of types and algorithmic code is a prerequisite for appreciation of the design of classes and class hierarchies. We do use user-defined types (what some people would call “objects”) from day one, but we don’t show how to design a class until Chapter 6 and don’t show a class hierarchy until Chapter 12.
  • 「第一天就面向对象」:OOP确实是组织代码的最好方式,但它并不是唯一的办法。基本的类型和算法是类设计和类继承的前置条件。我们这本书先把其他教材称作对象的东西叫做用户定义的类型,到第6章我们再讲如何设计类,第12章再讲类的继承。
  • “Just believe in magic”: This approach relies on demonstrations of powerful tools and techniques without introducing the novice to the underlying techniques and facilities. This leaves the student guessing — and usually guessing wrong — about why things are the way they are, what it costs to use them, and where they can be reasonably applied. This can lead to over rigid following of familiar patterns of work and become a barrier to further learning
  • 「相信这一切都是魔法」:这种方法一般出现在讲解工具和技术但并不讲其底层原理。这只能让学生自己猜——害总猜错。这给学生后续学习其他课程设下障碍。

Naturally, we do not claim that these other approaches are never useful. In fact, we use several of these for specific subtopics where their strengths can be appreciated. However, as general approaches to learning programming aimed at real-world use, we reject them and apply our alternative: concrete-first and depth-first with an emphasis on concepts and techniques.
实话说,并不是指这些方法一无是处。恰巧相反,我们会在一些章节子话题里见机行事使用这些方法。但放眼学习编程的全局,我们不使用这些方法,我们用我们自己的方法:具体优先,深度优先,并辅以强调概念和方法。

0.2.2 Programming and programming language 编程和编程语言

We teach programming first and treat our chosen programming language as secondary, as a tool. Our general approach can be used with any general-purpose programming language.
我们首要教语言,其次把语言是做工具。我们希望自己的教学方法可以适用于任意通用编程语言。

Our primary aim is to help you learn general concepts, principles, and techniques. However, those cannot be appreciated in isolation.
我的主要目标是帮助你学习一般概念,原理和方法。然而,这并不能脱离语言单独做到。

For example, details of syntax, the kinds of ideas that can be directly expressed, and tool support differ from programming language to programming language
例如,语法的细节,这种通过语言直接表达的东西,还有支持工具,不同的语言之间相差很大。

However, many of the fundamental techniques for producing bug-free code, such as writing logically simple code (Chapters 5 and 6), establishing invariants (§9.4.3), and separating interfaces from implementation details (§9.7 and §14.1–2), vary little from programming language to programming language.
然而,许多能产出无bug代码的基本技术,比如(第5章和第6章会讲到)编写有逻辑的简单代码,建立不变式(9.4.3)以及分离接口(9.7 14.1-2),这些在不同语言之间差别不大。


Programming and design techniques must be learned using a programming language. Design, code organization, and debugging are not skills you can acquire in the abstract.
必须通过编程语言才能学会编程和设计方法。设计,代码结构和debug是无法在抽象(的理论)中学会的。

You need to write code in some programming language and gain practical experience with that. This implies that you must learn the basics of a programming language. We say “the basics” because the days when you could learn all of a major industrial language in a few weeks are gone for good.
你需要通过编写代码来获得实践经验。也就是说你必须要学习语言的基础。我们之所以强调「基础」是因为从前那种只用几周就学会一门语言的日子一去不返了。

The parts of C++ we present were chosen as the subset that most directly supports the production of good code. Also, we present C++ features that you can’t avoid encountering either because they are necessary for logical completeness or are common in the C++ community.
我们选择了C++ 的部分作为直接支持生成好代码的子集。此外,我们还提供了一些 C++ 特性,您躲不开这些特性,因为这些特性对于逻辑完整性是必需的,或者在C++社区中是常见的。

0.2.3 Portability 可移植性

It is common to write C++ to run on a variety of machines. Major C++ applications run on machines we haven’t ever heard of!
C++跑在任意机器上是很常见的事。有些程序甚至跑在我们连名字都没听过的机子上。

We consider portability and the use of a variety of machine architectures and operating systems most important. Essentially every example in this book is not only ISO Standard C++, but also portable.
我们认为可移植性和保证在各种机器架构和操作系统中使用是非常重要的事情。本书中的所有例子不光符合ISO标准,还具有可移植性。

Unless specifically stated, the code we present should work on every C++ implementation and has been tested on several machines and operating systems.
除非有特殊情况,我们书中给出的C++代码在很多系统和机器上都通过了测试。


The details of how to compile, link, and run a C++ program differ from system to system
但编译,链接和运行的细节,每个系统是不一样的。

It would be tedious to mention the details of every system and every compiler each time we need to refer to an implementation issue
针对每个系统都提一嘴是非常麻烦的事。

In Appendix C, we give the most basic information about getting started using Visual Studio
and Microsoft C++ on a Windows machine.
在附录C中,我们给出了在Windows下使用Visual Studio和Microsoft C++的方法。


If you have trouble with one of the popular, but rather elaborate, IDEs (integrated development environments), we suggest you try working from the command line; it’s surprisingly simple.
如果你觉得IDE难用,那我们推荐你使用命令行,非常简单。

For example, here is the full set of commands needed to compile, link, and execute a simple program consisting of two source files, my_file1.cpp and my_file2.cpp, using the GNU C++ compiler on a Unix or Linux system:
比如在Linux下用GNU C++编译,链接,运行两个C++源文件:

c++ —o my_program my_file1.cpp my_file2.cpp
./my_program

0.3 Programming and computer science 编程和计算机科学

Is programming all that there is to computer science? Of course not! The only reason we raise this question is that people have been known to be confused about this.
编程是计算机科学的全部吗?当然不是!我们提出这问题是因为大部分人都有这种困惑。

We touch upon major topics from computer science, such as algorithms and data structures, but our aim is to teach programming: the design and implementation of programs.
本书包含一些计算机科学主题,比如算法和数据结构,但讲这些是为了辅助编程教学:程序的设计和实现。

That is both more and less than most accepted notions of computer science: 这比大多数人接受的计算机科学概念要多一些,也要少一些

  • More, because programming involves many technical skills that are not usually considered part of any science
  • 多一些,因为编程中很多技术细节并不算是计算机科学领域内的知识。
  • Less, because we do not systematically present the foundation for the parts of computer science we use
  • 少一些,我们并没有以计算机科学的系统形式来讲基础。

The aim of this book is to be part of a course in computer science (if becoming a computer scientist is your aim), to be the foundation for the first of many courses in software construction and maintenance (if your aim is to become a programmer or a software engineer), and in general to be part of a greater whole.
这本书可以作为计算机科学专业的一门课,可以作为后续所有软件构建和维护课程的基础。


We rely on computer science throughout and we emphasize principles, but we teach programming as a practical skill based on theory and experience, rather than as a science.
我们基于计科因为我们要强调原理,但我们教的编程,它基于理论和经验,比起一门科学,他更像一种实践技能。

0.4 Creativity and problem solving 创造力和问题解决能力

The primary aim of this book is to help you to express your ideas in code, not to teach you how to get those ideas. Along the way, we give many examples of how we can address a problem, usually through analysis of a problem followed by gradual refinement of a solution.
这本书教你如何用代码表达想法,但并不能教你如何思考出那些想法。本书中,我们会给出问题的思考过程和改进解法的思路。

We consider programming itself a form of problem solving: only through complete understanding of a problem and its solution can you express a correct program for it, and only through constructing and
testing a program can you be certain that your understanding is complete.
我们把编程视作这样的东西:只有完全理解了问题,你才能得出正确的解法,进而写出正确的程序,而反过来,通过构建和测试程序你才能确认你的理解是否到位。

Thus, programming is inherently part of an effort to gain understanding. However, we aim to demonstrate this through examples, rather than through “preaching” or presentation of detailed prescriptions for problem solving.
因此,编程本质上是获得理解的努力的一部分。然而,我们的目标是通过实例来证明这一点,而不是通过“传道”或提供解决问题的详细处方。

0.5 Request for feedback 反馈

(略)

0.6 References 引用

  • Becker, Pete, ed. The C++ Standard. ISO/IEC 14882:2011.
  • Blanchette, Jasmin, and Mark Summerfield. C++ GUI Programming with Qt 4, Second Edition. Prentice Hall, 2008. ISBN 0132354160.
  • Koenig, Andrew, and Barbara E. Moo. Accelerated C++: Practical Programming by Example. Addison-Wesley, 2000. ISBN 020170353X.
  • Meyers, Scott. Effective C++: 55 Specific Ways to Improve Your Programs and Designs, Third Edition. Addison-Wesley, 2005. ISBN 0321334876.
  • Schmidt, Douglas C., and Stephen D. Huston. C++ Network Programming, Volume 1: Mastering Complexity with ACE and Patterns. Addison-Wesley, 2001. ISBN 0201604647.
  • Schmidt, Douglas C., and Stephen D. Huston. C++ Network Programming, Volume 2: Systematic Reuse with ACE and Frameworks. Addison-Wesley, 2002. ISBN 0201795256.
  • Stroustrup, Bjarne. The Design and Evolution of C++. Addison-Wesley, 1994. ISBN 0201543303.
  • Stroustrup, Bjarne. “Learning Standard C++ as a New Language.” C/C++ Users Journal, May 1999.
  • Stroustrup, Bjarne. The C++ Programming Language, Fourth Edition. Addison-Wesley, 2013. ISBN 0321563840.
  • Stroustrup, Bjarne. A Tour of C++. Addison-Wesley, 2013. ISBN 0321958314.
  • Sutter, Herb. Exceptional C++: 47 Engineering Puzzles, Programming Problems, and Solutions. Addison-Wesley, 1999. ISBN 0201615622.

0.7 Biographies 作者简介

(略)

本章完


  1. 当理论与实际不符时,要以实际情况为准,不要按图索骥,颠倒虚实。 ↩︎

  2. drill和exercise都有「练习」的意思,所以我们这里不对这两个词做翻译。 ↩︎

你可能感兴趣的:(c++)