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

  • Preface 序言正文
    • A note to students 写给学生
    • A note to teacher 写给教师
    • ISO standard C++ ISO的C++标准
    • Support 学习支持
    • Acknowledgments 致谢名单

Preface 序言正文

Damn the torpedoes! 1 去他妈的鱼雷!
Full speed ahead. 给我全速前进!
——Admiral Farragut 2 海军上将法拉格特

Programming is the art of expressing solutions to problems so that a computer can execute those solutions.
编程是一种艺术,它表达出问题的解法,从而让计算机可以执行那些解法(得到问题的答案)。

Much of the effort in programming is spent finding and refining solutions.
编程时,大部分精力都花在寻找和改进解法上。

Often, a problem is only fully understood through the process of programming a solution for it.
通常情况下,只有过一遍求解过程才能让你完全理解这个问题。


This book is for someone who has never programmed before but is willing to work hard to learn.
这本书是为了那些从未编过程,但下决心要努力学习编程的人而写的。

It helps you understand the principles and acquire the practical skills of programming using the C++ programming language.
它会帮助你使用C++编程语言来理解编程原理和掌握实用技巧。

My aim is for you to gain sufficient knowledge and experience to perform simple useful programming tasks using the best up-to-date techniques.
我的目标就是让你获取到充足的知识和经验,使用最好最新的技术来完成一些简单有用的编程任务。

How long will that take?
需要花费多长时间呢?

As part of a first-year university course, you can work through this book in a semester (assuming that you have a workload of four courses of average difficulty).
作为一门大一课程,你可以在一个学期内完成这本书的学习(假设你同时在上一般难度的四门课程)

If you work by yourself, don’t expect to spend less time than that (maybe 15 hours a week for 14 weeks).
如果你是自学,那么请不要妄想用少于14周,每周15小时 3 就把这本书学完。


Three months may seem a long time, but there’s a lot to learn and you’ll be writing your first simple programs after about an hour.
三个月看起来很长,但实际上你要学很多东西,(不要着急)因为学一个小时你就能写出第一个简单的程序了。

Also, all learning is gradual: each chapter introduces new useful concepts and illustrates them with examples inspired by real-world uses.
当然了,学习是一个循序渐进的过程:每一章都会介绍有用的新概念,并辅以实际生活中的运用示例。

Your ability to express ideas in code — getting a computer to do what you want it to do — gradually and steadily increases as you go along.
你通过代码表达思维的能力——也就是让计算机做你想做的事的能力——会随着学习的过程逐渐提升并巩固。

I never say, “Learn a month’s worth of theory and then see if you can use it.”
我从不会说(我从不相信),学一个月理论然后看看你会不会直接用。


Why would you want to program?
为什么要学编程?

Our civilization runs on software.
人类文明运行在软件上。

Without understanding software you are reduced to believing in “magic” and will be locked out of many of the most interesting, profitable, and socially useful technical fields of work.
如果不理解软件那就只能选择相信计算机中存在某种「魔法」并被一些最有趣,最高效,对社会最有用的一些工作领域拒之门外。

When I talk about programming, I think of the whole spectrum of computer programs from personal computer applications with GUIs (graphical user interfaces), through engineering calculations and embedded systems control applications (such as digital cameras, cars, and cell phones), to text manipulation applications as found in many humanities and business applications.
我说的编程是一个宽泛的概念,包括个人电脑上有图形用户界面的计算机程序、工程计算、嵌入式系统中的控制应用(比如数码相机,汽车和手机)、还有很多在人文社科、商业场景可见到的文本编辑程序。

Like mathematics, programming — when done well — is a valuable intellectual exercise that
sharpens our ability to think.
就像数学一样,编程——如果认真琢磨——是一个能锻炼思维的非常有价值的智力活动。

However, thanks to feedback from the computer, programming is more concrete than most forms of math, and therefore accessible to more people.
然而,多亏了计算机返回的反馈信息,让编程比数学更加具体,因此容易被更多人掌握。

It is a way to reach out and change the world — ideally for the better. Finally, programming can be great fun.
编程是一种能让世界变得更美好的方式,编程也因此充满了乐趣。


Why C++? You can’t learn to program without a programming language, and C++ directly supports the key concepts and techniques used in real-world software.
为什么选择C++?你不可能不借助一门编程语言来学习编程,C++直接支持真实世界软件用到的关键概念和技术。

C++ is one of the most widely used programming languages, found in an unsurpassed range of application areas.
C++是世界上使用最多的编程语言之一,在很多领域都得到了应用。

You find C++ applications everywhere from the bottom of the oceans to the surface of Mars
从海底到火星都有C++的应用。

C++ is precisely and comprehensively defined by a non-proprietary international standard.
C++语言是由一个非专有的国际标准精确并完整地定义的。

Quality and/or free implementations are available on every kind of computer.
在任意型号的计算机上都可以自由并保证质量地实现。

Most of the programming concepts that you will learn using C++ can be used directly in other languages, such as C, C#, Fortran, and Java.
你在C++中学到的大多数编程概念都适用于其他编程语言,比如C、C#、Fortran、Java。

Finally, I simply like C++ as a language for writing elegant and efficient code.
我单纯地喜欢C++是因为可以用它编写出简洁和高效的代码。


This is not the easiest book on beginning programming; it is not meant to be.
这并不是入门编程最简单的一本书;我本不是有意这样写书的。

I just aim for it to be the easiest book from which you can learn the basics of real-world programming.
我本想写一本最简单的学习真实世界编程基础的书。

That’s quite an ambitious goal because much modern software relies on techniques considered advanced just a few years ago.
但这确实是一个宏大的目标,因为很多现代软件都基于几年前的「先进」技术。


My fundamental assumption is that you want to write programs for the use of others, and to do so responsibly, providing a decent level of system quality; that is, I assume that you want to achieve a level of professionalism.
我的基本假设是你想编写让别人用的软件,并且是负责任地保证系统质量处在一个较高的水平;也就是说,我假设你想把编程作为一种职业,达到专业水平。

Consequently, I chose the topics for this book to cover what is needed to get started with real-world programming, not just what is easy to teach and learn.
所以,我选择那些真实世界编程实际需要的东西作为主题,而不是随便选些好教易学的东西。

If you need a technique to get basic work done right, I describe it, demonstrate concepts and language facilities needed to support the technique, provide exercises for it, and expect you to work on those exercises
假设你想掌握一种技术来把基本工作做好,我会描述它,指明在背后支持技术的概念和语言特性,并提供练习,希望你能好好做这些练习。

If you just want to understand toy programs, you can get along with far less than I present
如果你只是想写一些小型程序,那么我讲的很多东西你都没必要掌握。

On the other hand, I won’t waste your time with material of marginal practical importance.
换句话说,我不会填鸭式地用大量的资料和实践中的边界情况来浪费你的时间。

If an idea is explained here, it’s because you’ll almost certainly need it.
如果我在某处解释了一个概念,那意味着这是你确实需要知道的。


If your desire is to use the work of others without understanding how things are done and without adding significantly to the code yourself, this book is not for you.
如果你只是使用别人的工作成果而不想了解其背后原理或者不需要加入自己的代码,那么这本书不适合你。

If so, please consider whether you would be better served by another book and another language.
如果你是这样的读者,那么你应该考虑是否换一本书或一门语言。

If that is approximately your view of programming, please also consider from where you got that view and whether it in fact is adequate for your needs.
如果你对编程就是持这种(简单易学的)看法,那么请你认真考虑这种想法是从哪听到的,并推敲它是否匹配你的实际需求。

People often underestimate the complexity of programming as well as its value.
人们经常会低估编程本身的价值和其复杂性。

I would hate for you to acquire a dislike for programming because of a mismatch between what you need and the part of the software reality I describe.
我不喜欢因为你内心对编程的认知与我所描述的编程的实际样子不符就讨厌编程的情况出现。

There are many parts of the “information technology” world that do not require knowledge of programming.
还有很多「IT」领域并不需要编程的知识。

This book is aimed to serve those who do want to write or understand nontrivial programs.
这本书是写给那些想要写好并理解复杂程序的人的。


Because of its structure and practical aims, this book can also be used as a second book on programming for someone who already knows a bit of C++ or for someone who programs in another language and wants to learn C++.
出于结构化和实践的目标,本书也可以作为那些学过一点儿C++和其他语言的第二本书。

If you fit into one of those categories, I refrain from guessing how long it will take you to read this book, but I do encourage you to do many of the exercises
如果你是这样的读者,我依然不会为你预估需要多长时间读完这本书,并且我强烈建议你应该做书中的练习

This will help you to counteract the common problem of writing programs in older, familiar styles rather than adopting newer techniques where these are more appropriate.
这样有助于你使用老的,你熟悉的编程风格,而非使用新技术解决问题。(节省了学习成本)

If you have learned C++ in one of the more traditional ways, you’ll find something surprising and useful before you reach Chapter 7.
如果你以传统方式学过C++,你仍可以在第7章之前学到一些令你惊喜和有用的东西。

Unless your name is Stroustrup, what I discuss here is not “your father’s C++.”
除非你的名字是Stroustrup 4,否则我如上所说并不是指「你爸爸那一辈儿人学的C++」。


Programming is learned by writing programs.
编程自然要通过动手编程来学。

In this, programming is similar to other endeavors with a practical component.
这一点,编程和其他需要付出努力和勤奋练习的事业一样。

You cannot learn to swim, to play a musical instrument, or to drive a car just from reading a book — you must practice.
你不可能只靠看书就学会游泳、弹乐器、开车,你必须练习。

Nor can you learn to program without reading and writing lots of code.
同样,你不读和写大量代码的话,你不可能学会编程。

This book focuses on code examples closely tied to explanatory text and diagrams.
这本书提供了很多结合文本和图表的代码示例。

You need those to understand the ideals, concepts, and principles of programming and to master the language constructs used to express them.
你需要通过它们来理解编程思想、概念和原理,然后通过表达来精通语言结构。

That’s essential, but by itself, it will not give you the practical skills of programming.
这是本质,但单靠这一点,无法赋予你实际的编程能力。

For that, you need to do the exercises and get used to the tools for writing, compiling, and running
programs.
你需要做练习去熟悉编写、编译、运行程序的工具。

You need to make your own mistakes and learn to correct them. There is no substitute for writing code. Besides, that’s where the fun is!
你要自己犯错并学会改正它们。没有任何方式能替代亲手编写代码来学会代码。这也是编程的乐趣所在!


On the other hand, there is more to programming — much more — than following a few rules and reading the manual.
另一方面,学习编程也不是简单死板地遵循某些规则和阅读手册。

This book is emphatically not focused on “the syntax of C++.”
这本书重点强调不要纠结于「C++的语法」

Understanding the fundamental ideals, principles, and techniques is the essence of a good programmer.
理解基本的思想、原理和技术是成为好程序员的本质。

Only well-designed code has a chance of becoming part of a correct, reliable, and maintainable system.
只有经过精心设计的代码才有机会成为正确、可靠、易于维持的系统的一部分。

Also, “the fundamentals” are what last: they will still be essential after today’s languages and
tools have evolved or been replaced.
基础就是最后沉淀下来的:尽管语言和工具更新迭代,但基础始终还是那些,一直没变。


What about computer science, software engineering, information technology, etc.?
那么计算机科学,软件工程,信息技术呢?

Is that all programming? Of course not!
它们也算编程么?当然不是!

Programming is one of the fundamental topics that underlie everything in computer-related fields, and it has a natural place in a balanced course of computer science
编程是所有计算机相关领域最基础的东西,在任何计算机课程中都占有一席之地。

I provide brief introductions to key concepts and techniques of algorithms, data structures, user interfaces, data processing, and software engineering.
我会简要介绍一些算法、数据结构、用户界面、数据处理和软件工程中的重要概念。

However, this book is not a substitute for a thorough and balanced study of those topics
如果你想全面的,平衡地学习这些知识,这本书并非扮演这种角色。


Code can be beautiful as well as useful.
代码可以兼具美感和实用性。

This book is written to help you see that, to understand what it means for code to be beautiful, and to help you to master the principles and acquire the practical skills to create such code.
这本书将带你领略这一点,带你理解美丽的代码意味着什么,并且会帮助你掌握原理和实用技能,让你也可以创造出美丽的代码。

Good luck with programming!
祝你好运!

A note to students 写给学生

Of the many thousands of first-year students we have taught so far using this book at Texas A&M University 5, about 60% had programmed before and about 40% had never seen a line of code in their lives.
这本书已经作为德克萨斯农工大学的大一教材很多个年头,学生中有六成之前或多或少有编程经验,而四成在此之前从未写过一行代码。

Most succeeded, so you can do it, too.
但他们其中的大多数最后都成功的完成了这门课程的学习,所以,相信你自己,你也可以学会!

You don’t have to read his book as part of a course
你没有必要把这本书作为课程的一部分来阅读。

The book is widely used for self-study.
这本书完全可以作为自学的教材

However, whether you work your way through as part of a course or independently, try to work with others.
如果无论你跟着老师学,还是自己学,都要尽量和他人合作学习

Most people work better and learn faster when they are part of a group with a common aim.
大部分人在一个有共同目标的小组中学习时都会学得更快更好。

Learning together and discussing problems with friends is not cheating!
但共同学习和讨论不代表作弊!

It is the most efficient — as well as most pleasant — way of making progress
小组学习是最有效,也是最愉快的学习方式。

If nothing else, working with friends forces you to articulate your ideas, which is just about the most efficient way of testing your understanding and making sure you remember.
和朋友学习强制你表达出的思想,这是检验你知识理解程度和记忆水平的最有效方式。

You don’t actually have to personally discover the answer to every obscure language and programming environment problem.
和朋友学习,也不用独自一人去解决难题和编程环境配置问题。

However, please don’t cheat yourself by not doing the drills and a fair number of exercises (even if no teacher forces you to do them).
然而,千万别偷懒,不深入思考,不做练习,哪怕没有老师管着你也要自觉。

Remember: programming is (among other things) a practical skill that you need to practice to master
要始终牢记:编程是一门需要实践的技能,你需要不断地练习,孰能生巧。

If you don’t write code (do several exercises for each chapter), reading this book will be a pointless theoretical exercise.
如果你不做章末的练习只读书,那这只是在理论的海洋里无目的地漫游,毛用没有。


Most students — especially thoughtful good students — face times when they wonder whether their hard work is worthwhile.
大部分学生——尤其是那些爱思考的好学生——也许在碰到坎儿时,会质疑自己勤奋刻苦,付出这么多精力时间是否值得。

Then (not if ) this happens to you, take a break, reread this Preface, and look at Chapter 1 (“Computers, People, and Programming”) and Chapter 22 (“Ideals and History”).
如果你遇到这样的瓶颈,休息一会儿,重新读读这篇序言,然后看看第1章(「计算机、人类、编程」)和第22章(「计算机思想和历史」)。

There, I try to articulate what I find exciting about programming and why I consider it a crucial
tool for making a positive contribution to the world.
这两章介绍了我在计算机领域里发现的令我惊叹的东西以及一些为世界做出贡献的重要工具。

If you wonder about my teaching philosophy and general approach, have a look at Chapter 0 (“Notes to the Reader”).
如果你对我的教学方式感到质疑,那还请你读读第0章(「写给读者」)


You might find the weight of this book worrying, but it should reassure you that part of the reason for the heft is that I prefer to repeat an explanation or add an example rather than have you search for the one and only explanation.
你可能发现这是本砖头书,但我需要澄清一下,写得这么厚都是为了让你更好地理解,我会重复解释一件事情或者举很多例子,省得你自己去查或者只能得到一种解释。

The other major reason is that the second half of the book is reference material and “additional material” presented for you to explore only if you are interested in more information about a specific area of programming, such as embedded systems programming, text analysis, or numerical computation.
书厚的另一个原因是这本书的后半部分更像是参考手册和附加材料,供那些对面向特定领域编程有兴趣的读者查阅,比如嵌入式编程,文本分析和数值计算。

And please don’t be too impatient. Learning any major new and valuable skill takes time and is worth it.
所以不要着急,学习任何新的有价值的技能都需要花费时间,也值得。

A note to teacher 写给教师

(略) 6

ISO standard C++ ISO的C++标准

C++ is defined by an ISO standard. The first ISO C++ standard was ratified in 1998, so that version of C++ is known as C++98.
C++是由ISO标准定义的。第一版标准在1998年确定,所以这个标准被叫做C++98。

I wrote the first edition of this book while working on the design of C++11.
我是在C++11标准设计之时撰写的本书第一版。

It was most frustrating not to be able to use the novel features (such as uniform initialization, range-for-loops, move semantics, lambdas, and concepts) to simplify the presentation of principles and techniques.
没法在书里写进最新的语言特性是个令人非常失望的事情。这些语言特性(比如uniform初始化,range-for-循环,move语义,lambda概念)可以简化程序原理和技术的表达。

However, the book was designed with C++11 in mind, so it was relatively easy to “drop in” the features in the contexts where they belonged.
然而,这本书的大框是基于C++11的,所以新的特性只需要直接填补就行。

As of this writing, the current standard is C++11 from 2011, and facilities from the upcoming 2014 ISO standard, C++14, are finding their way into mainstream C++ implementations.
而撰写这本书时,标准已经从C++11更新为C++14。

The language used in this book is C++11 with a few C++14 features
所以这本书主要是以C++11为标准,并加入了一些C++14的新特性。

For example, if your compiler complains about
举个例子,如果你的编译器返回错误:

vector<int> v1;
vector<int> v2 {v1};  // C++14风格的拷贝构建

那么请用以下替代:

vector<int> v1;
vector<int> v2=v1;  // C++98风格的拷贝构建

If your compiler does not support C++11, get a new compiler.
如果你的编译器不支持C++11,请换一个新的编译器。

Good, modern C++ compilers can be downloaded from a variety of suppliers; see https://www.stroustrup/compilers.html
优秀的,现代的C++编译器可以从各处下载,可以看这个链接。

Learning to program using an earlier and less supportive version of the language can be unnecessarily hard.
学习编程时使用一个老的,无法提供最新语法支持的编译器会给你的学习过程增添不必要的麻烦。

Support 学习支持

The book’s support website, https://www.stroustrup/programming.html 7, contains a variety of material supporting the teaching and learning of programming using this book.

The material is likely to be improved with time, but for starters, you can find
材料会一直更新,初学者可以在这里找到:

  • Slides for lectures based on the book 幻灯片(供老师使用的)
  • An instructor’s guide 教师用书
  • Header files and implementations of libraries used in the book 本书用到的库的头文件
  • Code for examples in the book 书中的示例代码
  • Solutions to selected exercises 部分习题答案
  • Potentially useful links 可能有用的链接
  • Errata 勘误表

Suggestions for improvements are always welcome.
欢迎大家多提意见。

Acknowledgments 致谢名单

(略)

本节完


  1. Battle of Mobile Bay#“Damn the torpedoes” - Wikipedia 莫尔比湾海战中法拉格特上将名言的背景故事。作者这里引用应该是意在给初学者加油打气,类似于冬泳怪鸽的著名演讲「干就完了!奥利给!」 ↩︎

  2. David Farragut - Wikipedia 大卫·法拉格特,美国19世纪内战期间海军著名将领。 ↩︎

  3. 每周15小时,也就是除去双休,每天要保证3个小时(4个45分钟课时,相当于大学里的一个大实验的时间量,一个上午或一个下午),或者周末无休,每天70分钟。共14周,以我自己为例,从2021年8月30号开始,到2021年12月5日结束,三个月零一周。 ↩︎

  4. Bjarne Stroustrup - Wikipedia 比雅尼·斯特劳斯特鲁普,丹麦计算机科学家,本书作者,C++语言之父。 ↩︎

  5. TAMU - Wikipedia 德克萨斯农工大学,2020上海软科全球大学排名151-200区间学校,这个学校的计算机科学专业在2016年的U.S.News全美大学专业排名中排行第21位,位于前列。 ↩︎

  6. 我只是自学,所以这节不翻译了。 ↩︎

  7. 作者给的原网址是https://www.stroustrup/Programming,但是打不开报403错误,我自行替换了正确的网址,但是并没有他说的材料,应该是版权原因。 ↩︎

更多推荐

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