ptg11539604
A Tour of C++
The C++ In-Depth Series
BJARNE STROUSTRUP, Editor
‘‘I have made this letter longer than usual, because I lack the time to make it short.’’
— Blaise Pascal
The C++ In-Depth Series is a collection of concise and focused books providing real-world pro-
grammers with reliable information about the C++ programming language. Selected by the
designer and original implementer of C++, Bjarne Stroustrup, and written by experts in the field,
each book in this series presents either a single topic, at a technical level appropriate to that topic,
or a fast-paced overview, for a quick understanding of broader language features. Its practical
approach, in either case, is designed to lift professionals (and aspiring professionals) to the next
level of programming skill or knowledge.
These short books are meant to be read and referenced without the distraction of unrelated
material. As C++ matures, it becomes increasingly important to be able to separate essential infor-
mation from hype and glitz, and to find the deep content and practical guidance needed for contin-
ued development. The C++ In-Depth Series provides the background, tools, concepts, techniques,
and new approaches that can enable this development, and thereby give readers a valuable, critical
edge.
ptg11539604
A Tour of C++
Bjarne Stroustrup
Upper Saddle River, NJ • Boston • Indianapolis • San Francisco
New York • Toronto • Montreal • London • Munich • Paris • Madrid
Capetown • Sydney • Tokyo • Singapore • Mexico City
ptg11539604
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where
those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed
with initial capital letters or in all capitals.
The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any
kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in
connection with or arising out of the use of the information or programs contained herein.
The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which
may include electronic versions and/or custom covers and content particular to your business, training goals, marketing
focus, and branding interests. For more information, please contact:
U.S. Corporate and Government Sales
(800) 382-3419
corpsales@pearsontechgroup.com
For sales outside the United States, please contact:
International Sales
international@pearsoned.com
Visit us on the Web: informit.com/aw
Library of Congress Cataloging-in-Publication Data
Stroustrup, Bjarne.
A Tour of C++ / Bjarne Stroustrup.
pages cm
Includes bibliographical references and index.
ISBN 978-0-321-958310 (pbk. : alk. paper)—ISBN 0-321-958314 (pbk. : alk. paper)
1. C++ (Computer programming language) I. Title.
QA76.73.C153 S77 2013
005.13’3—dc23
Copyright © 2014 by Pearson Education, Inc.
2013002159
All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must
be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any
form or by any means, electronic, mechanical, photocopying, recording, or likewise. To obtain permission to use material
from this work, please submit a written request to Pearson Education, Inc., Permissions Department, One Lake Street, Upper
Saddle River, New Jersey 07458, or you may fax your request to (201) 236-3290.
This book was typeset in Times and Helvetica by the author.
ISBN-13: 978-0-321-958310
ISBN-10:
0-321-958314
Te xt printed in the United States on recycled paper at Edwards Brothers Malloy in Ann Arbor, Michigan.
First printing, September 2013
ptg11539604
Contents
Contents
Preface
1 The Basics
1.1
Introduction ................................................................................. 1
1.2 Programs ..................................................................................... 1
1.3 Hello, World! .............................................................................. 2
1.4 Functions ..................................................................................... 3
1.5 Types, Variables, and Arithmetic ................................................ 5
1.6 Scope ........................................................................................... 8
1.7 Constants ..................................................................................... 8
1.8 Pointers, Arrays, and References ................................................ 9
1.9 Tests ............................................................................................ 12
1.10 Advice ......................................................................................... 14
2 User-Defined Types
2.1
Introduction ................................................................................. 15
2.2 Structures .................................................................................... 16
2.3 Classes ........................................................................................ 17
2.4 Unions ......................................................................................... 19
2.5 Enumerations .............................................................................. 20
2.6 Advice ......................................................................................... 21
v
ix
1
15
ptg11539604
vi Contents
3 Modularity
3.1
Introduction ................................................................................. 23
3.2 Separate Compilation .................................................................. 24
3.3 Namespaces ................................................................................. 26
3.4 Error Handling ............................................................................ 27
3.5 Advice ......................................................................................... 31
4 Classes
4.1
Introduction ................................................................................. 33
4.2 Concrete Types ............................................................................ 34
4.3 Abstract Types ............................................................................ 39
4.4 Virtual Functions ......................................................................... 42
4.5 Class Hierarchies ........................................................................ 42
4.6 Copy and Move ........................................................................... 48
4.7 Advice ......................................................................................... 56
5 Templates
5.1
Introduction ................................................................................. 59
5.2 Parameterized Types ................................................................... 59
5.3 Function Templates ..................................................................... 62
5.4 Concepts and Generic Programming .......................................... 62
5.5 Function Objects ......................................................................... 64
5.6 Variadic Templates ...................................................................... 66
5.7 Aliases ......................................................................................... 67
5.8 Template Compilation Model ..................................................... 68
5.9 Advice ......................................................................................... 68
6 Library Overview
6.1
Introduction ................................................................................. 71
6.2 Standard-Library Components .................................................... 72
6.3 Standard-Library Headers and Namespace ................................. 72
6.4 Advice ......................................................................................... 74
7 Strings and Regular Expressions
7.1
Introduction ................................................................................. 75
7.2 Strings ......................................................................................... 75
7.3 Regular Expressions .................................................................... 78
7.4 Advice ......................................................................................... 84
23
33
59
71
75
ptg11539604
8 I/O Streams
8.1
Introduction ................................................................................. 85
8.2 Output ......................................................................................... 86
Input ............................................................................................ 87
8.3
8.4
I/O State ...................................................................................... 89
8.5
I/O of User-Defined Types .......................................................... 90
8.6 Formatting ................................................................................... 91
8.7 File Streams ................................................................................ 92
8.8 String Streams ............................................................................. 92
8.9 Advice ......................................................................................... 93
9 Containers
Introduction ................................................................................. 95
9.1
vector ........................................................................................... 96
9.2
list ................................................................................................ 100
9.3
9.4 map .............................................................................................. 101
9.5
unordered_map ............................................................................ 102
9.6 Container Overview .................................................................... 103
9.7 Advice ......................................................................................... 104
10 Algorithms
10.1
Introduction ................................................................................. 107
10.2 Use of Iterators ............................................................................ 108
10.3
Iterator Types .............................................................................. 111
10.4 Stream Iterators ........................................................................... 112
10.5 Predicates .................................................................................... 113
10.6 Algorithm Overview ................................................................... 114
10.7 Container Algorithms ................................................................. 115
10.8 Advice ......................................................................................... 115
11 Utilities
11.1
Introduction ................................................................................. 117
11.2 Resource Management ................................................................ 117
11.3 Specialized Containers ................................................................ 121
11.4 Time ............................................................................................ 125
11.5 Function Adaptors ....................................................................... 125
11.6 Type Functions ............................................................................ 128
11.7 Advice ......................................................................................... 131
vii
85
95
107
117
ptg11539604