|
Contents at a Glance |
3 |
|
|
Contents |
598 |
|
|
About the Author |
614 |
|
|
About the Technical Reviewers |
616 |
|
|
Acknowledgment |
617 |
|
|
Introduction |
7 |
|
|
EXPLORATION 1: Honing Your Tools |
11 |
|
|
Ray’s Recommendations |
11 |
|
|
Microsoft Windows |
11 |
|
|
Macintosh OS X |
12 |
|
|
Everyone Else |
12 |
|
|
Read the Documentation |
12 |
|
|
Your First Program |
13 |
|
|
EXPLORATION 2: Reading C++ Code |
18 |
|
|
Comments |
19 |
|
|
Headers |
19 |
|
|
Main Program |
20 |
|
|
Variable Definitions |
21 |
|
|
Statements |
22 |
|
|
Output |
23 |
|
|
EXPLORATION 3: Integer Expressions |
24 |
|
|
EXPLORATION 4: Strings |
30 |
|
|
EXPLORATION 5: Simple Input |
35 |
|
|
EXPLORATION 6: Error Messages |
40 |
|
|
Misspelling |
41 |
|
|
Bogus Character |
42 |
|
|
Unknown Operator |
43 |
|
|
Unknown Name |
43 |
|
|
Symbol Errors |
44 |
|
|
Fun with Errors |
44 |
|
|
EXPLORATION 7: For Loops |
45 |
|
|
Bounded Loops |
45 |
|
|
Initialization |
46 |
|
|
Condition |
47 |
|
|
Postiteration |
47 |
|
|
How a for Loop Works |
47 |
|
|
Your Turn |
48 |
|
|
EXPLORATION 8: Formatted Output |
49 |
|
|
The Problem |
49 |
|
|
Field Width |
50 |
|
|
Fill Character |
51 |
|
|
std Prefix |
52 |
|
|
Alignment |
52 |
|
|
Exploring Formatting |
53 |
|
|
Alternative Syntax |
54 |
|
|
On Your Own |
55 |
|
|
EXPLORATION 9: Arrays and Vectors |
57 |
|
|
Vectors for Arrays |
58 |
|
|
Vectors |
58 |
|
|
Iterators |
59 |
|
|
EXPLORATION 10: Algorithms and Iterators |
61 |
|
|
Algorithms |
61 |
|
|
Member Types |
62 |
|
|
A Simpler Loop |
63 |
|
|
Using Iterators and Algorithms |
64 |
|
|
EXPLORATION 11: Increment and Decrement |
67 |
|
|
Increment |
67 |
|
|
Decrement |
68 |
|
|
EXPLORATION 12: Conditions and Logic |
72 |
|
|
I/O and bool |
72 |
|
|
Boolean Type |
73 |
|
|
Logic Operators |
75 |
|
|
Old-Fashioned Syntax |
76 |
|
|
Comparison Operators |
76 |
|
|
EXPLORATION 13: Compound Statements |
79 |
|
|
Statements |
79 |
|
|
Local Definitions and Scope |
82 |
|
|
Definitions in for Loop Headers |
85 |
|
|
EXPLORATION 14: Introduction to File I/O |
88 |
|
|
Reading Files |
88 |
|
|
Writing Files |
89 |
|
|
EXPLORATION 15: The Map Data Structure |
92 |
|
|
Using Maps |
93 |
|
|
Pairs |
93 |
|
|
Searching in Maps |
95 |
|
|
EXPLORATION 16: Type Synonyms |
98 |
|
|
typedef Declarations |
98 |
|
|
Common typedefs |
99 |
|
|
Type Aliases |
99 |
|
|
EXPLORATION 17: Characters |
101 |
|
|
Character Type |
101 |
|
|
Character I/O |
103 |
|
|
Newlines and Portability |
104 |
|
|
Character Escapes |
105 |
|
|
EXPLORATION 18: Character Categories |
106 |
|
|
Character Sets |
106 |
|
|
Character Categories |
108 |
|
|
Locales |
109 |
|
|
Exploration 19: Case-Folding |
112 |
|
|
Simple Cases |
112 |
|
|
Harder Cases |
113 |
|
|
EXPLORATION 20: Writing Functions |
115 |
|
|
Functions |
115 |
|
|
Function Call |
117 |
|
|
Declarations and Definitions |
117 |
|
|
Counting Words—Again |
119 |
|
|
The main() Function |
122 |
|
|
EXPLORATION 21: Function Arguments |
123 |
|
|
Argument Passing |
123 |
|
|
Pass-by-Reference |
125 |
|
|
const References |
127 |
|
|
const_iterator |
129 |
|
|
Multiple Output Parameters |
129 |
|
|
Exploration 22: Using Algorithms |
130 |
|
|
Transforming Data |
130 |
|
|
Predicates |
135 |
|
|
Other Algorithms |
137 |
|
|
Exploration 23: Unnamed Functions |
140 |
|
|
Lambdas |
140 |
|
|
Naming an Unnamed Function |
142 |
|
|
Capturing Local Variables |
142 |
|
|
const Capture |
144 |
|
|
Return Type |
145 |
|
|
Exploration 24: Overloading Function Names |
147 |
|
|
Overloading |
147 |
|
|
bool is_alpha(char ch) |
149 |
|
|
bool is_alpha(std::string const& str) |
149 |
|
|
char to_lower(char ch) |
149 |
|
|
std::string to_lower(std::string str) |
150 |
|
|
char to_upper(char ch) |
150 |
|
|
std::string to_upper(std::string str) |
150 |
|
|
Exploration 25: Big and Little Numbers |
153 |
|
|
The Long and Short of It |
153 |
|
|
Long Integers |
154 |
|
|
Short Integers |
154 |
|
|
Integer Literals |
155 |
|
|
Byte-Sized Integers |
156 |
|
|
Type Casting |
157 |
|
|
Make Up Your Own Literals |
158 |
|
|
Integer Arithmetic |
159 |
|
|
Overload Resolution |
160 |
|
|
Exploration 26: Very Big and Very Little Numbers |
162 |
|
|
Floating-Point Numbers |
162 |
|
|
Floating-Point Literals |
163 |
|
|
Floating-Point Traits |
164 |
|
|
Floating-Point I/O |
165 |
|
|
Exploration 27: Documentation |
169 |
|
|
Doxygen |
169 |
|
|
Structured Comments |
169 |
|
|
Documentation Tags and Markdown |
170 |
|
|
Using Doxygen |
175 |
|
|
@b word |
170 |
|
|
@brief one-sentence-description |
170 |
|
|
@c word |
170 |
|
|
@em word |
170 |
|
|
@file file name |
170 |
|
|
@link entity text @endlink |
170 |
|
|
@mainpage title |
171 |
|
|
@p name |
171 |
|
|
@par title |
171 |
|
|
@param name description |
171 |
|
|
@post postcondition |
171 |
|
|
@pre precondition |
171 |
|
|
@return description |
171 |
|
|
@see xref |
171 |
|
|
@ &, @@, @\, @%, @< |
172 |
|
|
Exploration 28: Project 1: Body-Mass Index |
176 |
|
|
Hints |
177 |
|
|
Exploration 29: Custom Types |
178 |
|
|
Defining a New Type |
178 |
|
|
Member Functions |
179 |
|
|
Constructors |
182 |
|
|
Overloading Constructors |
184 |
|
|
Exploration 30: Overloading Operators |
185 |
|
|
Comparing Rational Numbers |
185 |
|
|
Arithmetic Operators |
188 |
|
|
Math Functions |
191 |
|
|
Exploration 31: Custom I/O Operators |
194 |
|
|
Input Operator |
194 |
|
|
Output Operator |
195 |
|
|
Error State |
196 |
|
|
Exploration 32: Assignment and Initialization |
198 |
|
|
Assignment Operator |
198 |
|
|
Constructors |
199 |
|
|
Putting It All Together |
200 |
|
|
Exploration 33: Writing Classes |
205 |
|
|
Anatomy of a Class |
205 |
|
|
Member Functions |
206 |
|
|
Constructor |
208 |
|
|
Defaulted and Deleted Constructors |
212 |
|
|
Exploration 34: More About Member Functions |
213 |
|
|
Revisiting Project 1 |
213 |
|
|
Const Member Functions |
217 |
|
|
Exploration 35: Access Levels |
220 |
|
|
Public vs. Private |
220 |
|
|
class vs. struct |
223 |
|
|
Plain Old Data |
223 |
|
|
Public or Private ? |
224 |
|
|
Exploration 36: Introduction to Object-Oriented Programming |
229 |
|
|
Books and Magazines |
229 |
|
|
Classification |
230 |
|
|
Inheritance |
232 |
|
|
Liskov’s Substitution Principle |
233 |
|
|
Type Polymorphism |
234 |
|
|
Exploration 37: Inheritance |
235 |
|
|
Deriving a Class |
235 |
|
|
Member Functions |
238 |
|
|
Destructors |
238 |
|
|
Access Level |
242 |
|
|
Programming Style |
243 |
|
|
Exploration 38: Virtual Functions |
244 |
|
|
Type Polymorphism |
244 |
|
|
Virtual Functions |
247 |
|
|
References and Slices |
250 |
|
|
Pure Virtual Functions |
251 |
|
|
Virtual Destructors |
251 |
|
|
Exploration 39: Classes and Types |
252 |
|
|
Classes vs. typedefs |
252 |
|
|
Value Types |
255 |
|
|
Copying |
255 |
|
|
Assigning |
255 |
|
|
Moving |
256 |
|
|
Comparing |
257 |
|
|
Resource Acquisition Is Initialization |
260 |
|
|
EXPLORATION 40: Declarations and Definitions |
262 |
|
|
Declaration vs. Definition |
262 |
|
|
inline Functions |
264 |
|
|
Variable Declarations and Definitions |
265 |
|
|
Static Variables |
267 |
|
|
Static Data Members |
269 |
|
|
Declarators |
271 |
|
|
EXPLORATION 41: Using Multiple Source Files |
272 |
|
|
Multiple Source Files |
272 |
|
|
Declarations and Definitions |
273 |
|
|
#include Files |
276 |
|
|
Quotes and Brackets |
277 |
|
|
Nested #include Directives |
277 |
|
|
Include Guards |
278 |
|
|
Forward Declarations |
280 |
|
|
Documentation |
280 |
|
|
extern Variables |
282 |
|
|
inline Functions |
283 |
|
|
One-Definition Rule |
284 |
|
|
EXPLORATION 42: Function Objects |
286 |
|
|
The Function Call Operator |
286 |
|
|
Function Objects |
288 |
|
|
EXPLORATION 43: Useful Algorithms |
290 |
|
|
Searching |
290 |
|
|
Linear Search Algorithms |
290 |
|
|
Binary Search Algorithms |
295 |
|
|
Comparing |
298 |
|
|
Rearranging Data |
300 |
|
|
Copying Data |
302 |
|
|
Deleting Elements |
302 |
|
|
Iterators |
303 |
|
|
EXPLORATION 44: Iterators |
304 |
|
|
Kinds of Iterators |
304 |
|
|
Input Iterators |
305 |
|
|
Output Iterators |
305 |
|
|
Forward Iterators |
305 |
|
|
Bidirectional Iterators |
305 |
|
|
Random Access Iterators |
306 |
|
|
Working with Iterators |
307 |
|
|
const_iterator vs. const iterator |
308 |
|
|
Error Messages |
310 |
|
|
Specialized Iterators |
311 |
|
|
EXPLORATION 45: Exceptions |
314 |
|
|
Introducing Exceptions |
314 |
|
|
Catching Exceptions |
316 |
|
|
Throwing Exceptions |
317 |
|
|
Program Stack |
318 |
|
|
Standard Exceptions |
322 |
|
|
I/O Exceptions |
323 |
|
|
Custom Exceptions |
324 |
|
|
Don’t Throw Exceptions |
326 |
|
|
Exceptional Advice |
327 |
|
|
EXPLORATION 46: More Operators |
328 |
|
|
Conditional Operator |
328 |
|
|
Short-Circuit Operators |
330 |
|
|
Comma Operator |
330 |
|
|
Arithmetic Assignment Operators |
333 |
|
|
Increment and Decrement |
334 |
|
|
Exploration 47: Project 2: Fixed-Point Numbers |
338 |
|
|
value_type |
338 |
|
|
places |
338 |
|
|
places10 |
338 |
|
|
fixed(?) |
338 |
|
|
fixed(value_type integer, value_type fraction) |
338 |
|
|
fixed(double val) |
339 |
|
|
to_string(?) |
339 |
|
|
round(?) |
339 |
|
|
integer(?) |
339 |
|
|
fraction(?) |
339 |
|
|
Exploration 48: Function Templates |
343 |
|
|
Generic Functions |
343 |
|
|
Using Function Templates |
344 |
|
|
Writing Function Templates |
345 |
|
|
Template Parameters |
347 |
|
|
Template Arguments |
348 |
|
|
Declarations and Definitions |
350 |
|
|
Member Function Templates |
350 |
|
|
Exploration 49: Class Templates |
352 |
|
|
Parameterizing a Type |
352 |
|
|
Parameterizing the rational Class |
353 |
|
|
Using Class Templates |
355 |
|
|
Overloaded Operators |
356 |
|
|
Mixing Types |
358 |
|
|
Exploration 50: Template Specialization |
360 |
|
|
Instantiation and Specialization |
360 |
|
|
Custom Comparators |
363 |
|
|
Specializing Function Templates |
364 |
|
|
Traits |
365 |
|
|
Exploration 51: Partial Template Specialization |
368 |
|
|
Degenerate Pairs |
368 |
|
|
Partial Specialization |
369 |
|
|
Partially Specializing Function Templates |
370 |
|
|
Value Template Parameters |
370 |
|
|
Exploration 52: Names and Namespaces |
373 |
|
|
Namespaces |
373 |
|
|
Nested Namespaces |
376 |
|
|
Global Namespace |
378 |
|
|
The std Namespace |
378 |
|
|
Using Namespaces |
379 |
|
|
The using Directive |
379 |
|
|
The using Declaration |
381 |
|
|
The using Declaration in a Class |
384 |
|
|
Unnamed Namespaces |
385 |
|
|
Name Lookup |
385 |
|
|
Exploration 53: Containers |
390 |
|
|
Properties of Containers |
390 |
|
|
Member Types |
391 |
|
|
value_type |
391 |
|
|
key_type |
391 |
|
|
reference |
391 |
|
|
const_reference |
392 |
|
|
iterator |
392 |
|
|
const_iterator |
392 |
|
|
size_type |
392 |
|
|
What Can Go into a Container |
392 |
|
|
Inserting and Erasing |
393 |
|
|
Inserting in a Sequence Container |
393 |
|
|
Erasing from a Sequence Container |
394 |
|
|
Inserting in an Associative Container |
395 |
|
|
Erasing from an Associative Container |
396 |
|
|
Exceptions |
396 |
|
|
Iterators and References |
397 |
|
|
Sequence Containers |
399 |
|
|
The array Class Template |
401 |
|
|
The deque Class Template |
402 |
|
|
The list Class Template |
402 |
|
|
The vector Class Template |
403 |
|
|
Associative Containers |
404 |
|
|
Exploration 54: Locales and Facets |
409 |
|
|
The Problem |
409 |
|
|
Locales to the Rescue |
410 |
|
|
Locales and I/O |
411 |
|
|
Facets |
411 |
|
|
Character Categories |
413 |
|
|
Collation Order |
418 |
|
|
Exploration 55: International Characters |
420 |
|
|
Why Wide? |
420 |
|
|
Using Wide Characters |
420 |
|
|
Wide Strings |
421 |
|
|
Wide Character I/O |
423 |
|
|
Multi-Byte Character Sets |
424 |
|
|
Unicode |
425 |
|
|
Universal Character Names |
426 |
|
|
Unicode Difficulties |
427 |
|
|
Exploration 56: Text I/O |
428 |
|
|
File Modes |
428 |
|
|
String Streams |
429 |
|
|
Text Conversion |
435 |
|
|
Exploration 57: Project 3: Currency Type |
441 |
|
|
Exploration 58: Pointers |
442 |
|
|
A Programming Problem |
442 |
|
|
The Solution |
450 |
|
|
Addresses vs. Pointers |
451 |
|
|
Dependency Graphs |
452 |
|
|
Exploration 59: Dynamic Memory |
455 |
|
|
Allocating Memory |
455 |
|
|
Freeing Memory |
456 |
|
|
Pointer to Nothing |
456 |
|
|
Implementing Standard Containers |
458 |
|
|
Adding Variables |
459 |
|
|
Special Member Functions |
466 |
|
|
Exploration 60: Moving Data with Rvalue References |
470 |
|
|
Temporary Objects |
470 |
|
|
Lvalues, Rvalues, and More |
473 |
|
|
Implementing Move |
474 |
|
|
Rvalue or Lvalue? |
475 |
|
|
Special Member Functions |
476 |
|
|
EXPLORATION 61: Exception-Safety |
478 |
|
|
Memory Leaks |
478 |
|
|
Exceptions and Dynamic Memory |
480 |
|
|
Automatically Deleting Pointers |
481 |
|
|
Exceptions and Constructors |
484 |
|
|
EXPLORATION 62: Old-Fashioned Arrays |
488 |
|
|
C-Style Arrays |
488 |
|
|
Array Limitations |
489 |
|
|
Dynamically Allocating an Array |
490 |
|
|
The array Type |
491 |
|
|
Multidimensional Arrays |
491 |
|
|
C-Style Strings |
492 |
|
|
Command-Line Arguments |
492 |
|
|
Pointer Arithmetic |
494 |
|
|
EXPLORATION 63: Smart Pointers |
496 |
|
|
Revisiting unique_ptr |
496 |
|
|
Copyable Smart Pointers |
497 |
|
|
Smart Arrays |
498 |
|
|
Pimpls |
499 |
|
|
Iterators |
504 |
|
|
Exploration 64: Working with Bits |
505 |
|
|
Integer As a Set of Bits |
505 |
|
|
Bitmasks |
507 |
|
|
Shifting Bits |
508 |
|
|
Safe Shifting with Unsigned Types |
509 |
|
|
Signed and Unsigned Types |
510 |
|
|
Unsigned Literals |
511 |
|
|
Type Conversions |
511 |
|
|
Overflow |
515 |
|
|
Introducing Bitfields |
516 |
|
|
Portability |
517 |
|
|
The bitset Class Template |
517 |
|
|
Exploration 65: Enumerations |
520 |
|
|
Scoped Enumerations |
520 |
|
|
Unscoped Enumerations |
522 |
|
|
Strings and Enumerations |
522 |
|
|
Revisiting Projects |
524 |
|
|
Exploration 66: Multiple Inheritance |
527 |
|
|
Multiple Base Classes |
527 |
|
|
Virtual Base Classes |
530 |
|
|
Java-Like Interfaces |
532 |
|
|
Interfaces vs. Templates |
534 |
|
|
Mix-Ins |
536 |
|
|
Protected Access Level |
537 |
|
|
EXPLORATION 67: Traits and Policies |
539 |
|
|
Case Study: Iterators |
539 |
|
|
Iterator Traits |
542 |
|
|
Type Traits |
543 |
|
|
Case Study: char_traits |
543 |
|
|
Policy-Based Programming |
545 |
|
|
EXPLORATION 68: Names, Namespaces, and Templates |
552 |
|
|
Common Rules |
552 |
|
|
Name Lookup in Templates |
553 |
|
|
Three Kinds of Name Lookup |
553 |
|
|
Member Access Operators |
553 |
|
|
Qualified Name Lookup |
555 |
|
|
Unqualified Name Lookup |
557 |
|
|
Argument-Dependent Lookup |
558 |
|
|
EXPLORATION 69: Overloaded Functions and Operators |
561 |
|
|
Type Conversion |
561 |
|
|
Review of Overloaded Functions |
562 |
|
|
Overload Resolution |
565 |
|
|
Ranking Functions |
565 |
|
|
List Initialization |
567 |
|
|
Tie-Breakers |
567 |
|
|
Default Arguments |
571 |
|
|
EXPLORATION 70: Metaprogramming |
573 |
|
|
Use constexpr for Compile-Time Values |
573 |
|
|
Variable-Length Template Argument Lists |
575 |
|
|
Types as Values |
578 |
|
|
Conditional Types |
580 |
|
|
Checking Traits |
581 |
|
|
Substitution Failure Is Not An Error (SFINAE) |
583 |
|
|
EXPLORATION 71: Project 4: Calculator |
585 |
|
|
Index |
586 |
|