C++ The Complete Reference 4th Edition-Herbert Schildt

1058

description

C++ The Complete Reference 4th Edition-Herbert Schildt

Transcript of C++ The Complete Reference 4th Edition-Herbert Schildt

  • 1.C++: The Complete Reference, Fourth Edition

2. About the Author Herbert Schildt is the worlds leading programming author. He is an authority on the C, C++, Java, and C# languages, and is a master Windows programmer. His programming books have sold more than 3 million copies worldwide and have been translated into all major foreign languages. He is the author of numerous bestsellers, including C++: The Complete Reference, C#: The Complete Reference, Java 2: The Complete Reference, C: The Complete Reference, C++ from the Ground Up, C++: A Beginners Guide, C#: A Beginners Guide, and Java 2: A Beginners Guide. Schildt holds a masters degree in computer science from the University of Illinois. He can be reached at his consulting office at (217) 586-4683.Copyright 2003 by The McGraw-Hill Companies. Click here for terms of use. 3. C++: The Complete Reference, Fourth Edition Herbert SchildtMcGraw-Hill/Osborne New York Chicago San Francisco Lisbon London Madrid Mexico City Milan New Delhi San Juan Seoul Singapore Sydney Toronto 4. Copyright 2003 by The McGraw-Hill Companies. All rights reserved. Manufactured in the United States of America. Except as permitted under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher. 0-07-150239-4 The material in this eBook also appears in the print version of this title: 0-07-222680-3. All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the trademark. Where such designations appear in this book, they have been printed with initial caps. McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training programs. For more information, please contact George Hoare, Special Sales, at [email protected] or (212) 904-4069. TERMS OF USE This is a copyrighted work and The McGraw-Hill Companies, Inc. (McGraw-Hill) and its licensors reserve all rights in and to the work. Use of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hills prior consent. You may use the work for your own noncommercial and personal use; any other use of the work is strictly prohibited. Your right to use the work may be terminated if you fail to comply with these terms. THE WORK IS PROVIDED AS IS. McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant or guarantee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages resulting therefrom. McGraw-Hill has no responsibility for the content of any information accessed through the work. Under no circumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work, even if any of them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in contract, tort or otherwise. DOI: 10.1036/0072226803 5. ProfessionalWant to learn more? We hope you enjoy this McGraw-Hill eBook! If youd like more information about this book, its author, or related books and websites, please click here. 6. Contents at a Glance Part I The Foundation of C++: The C Subset 1 2 3 4 5 6 7 8 9 10 Part II 11 12An Overview of C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Arrays and Null-Terminated Strings . . . . . . . . . . . . . . . . Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Structures, Unions, Enumerations, and User-Defined Types . . . . . . . . . . . . . . . . . . . . . . . . C-Style Console I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Preprocessor and Comments . . . . . . . . . . . . . . . . . .3 13 57 89 113 137 161 187 211 237C++ An Overview of C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .v255 289 7. viC++: The Complete Reference13 14 15 16 17 18 19 20 21 22 23 24Arrays, Pointers, References, and the Dynamic Allocation Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . Function Overloading, Copy Constructors, and Default Arguments . . . . . . . . . . . . . . . . . . . . . . . . . Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Virtual Functions and Polymorphism . . . . . . . . . . . . . . . Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The C++ I/O System Basics . . . . . . . . . . . . . . . . . . . . . . . C++ File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Run-Time Type ID and the Casting Operators . . . . . . . Namespaces, Conversion Functions, and Other Advanced Topics . . . . . . . . . . . . . . . . . . . . . Introducing the Standard Template Library . . . . . . . . . .325 359 383 417 443 459 487 509 539 567 591 629Part III The Standard Function Library 25 26 27 28 29 30 31The C-Based I/O Functions . . . . . . . . . . . . . . . . . . . . . . . The String and Character Functions . . . . . . . . . . . . . . . . The Mathematical Functions . . . . . . . . . . . . . . . . . . . . . . Time, Date, and Localization Functions . . . . . . . . . . . . . The Dynamic Allocation Functions . . . . . . . . . . . . . . . . . Utility Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Wide-Character Functions . . . . . . . . . . . . . . . . . . . . .699 723 737 747 757 761 775Part IV The Standard C++ Class Library 32 33 34 35 36 37 38The Standard C++ I/O Classes . . . . . . . . . . . . . . . . . . . . The STL Container Classes . . . . . . . . . . . . . . . . . . . . . . . . The STL Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . STL Iterators, Allocators, and Function Objects . . . . . . The String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Numeric Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exception Handling and Miscellaneous Classes . . . . . .787 811 839 861 881 897 925 8. Contents at a GlancePart V 39 40 A BApplying C++ Integrating New Classes: A Custom String Class . . . . . 935 Parsing Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 963 The .NET Managed Extensions to C++ . . . . . . . . . . . . . . 999 C++ and the Robotics Age . . . . . . . . . . . . . . . . . . . . . . . . 1005 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1009vii 9. This page intentionally left blank 10. For more information about this title, click hereContents Introduction.............................................xxixPart I The Foundation of C++: The C Subset2An Overview of C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3The Origins and History of C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . C Is a Middle-Level Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . C Is a Structured Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . C Is a Programmers Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Form of a C Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Library and Linking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Separate Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Understanding the .C and .CPP File Extensions . . . . . . . . . . . . . . . .14 5 6 8 9 10 12 12Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13The Five Basic Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Modifying the Basic Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Identifier Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where Variables Are Declared . . . . . . . . . . . . . . . . . . . . . . . . Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14 15 16 17 18 18ix 11. xC++: The Complete ReferenceFormal Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The const and volatile Qualifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . const . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . volatile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Storage Class Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . extern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . static Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . register Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Variable Initializations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Hexadecimal and Octal Constants . . . . . . . . . . . . . . . . . . . . String Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Backslash Character Constants . . . . . . . . . . . . . . . . . . . . . . . Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Assignment Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . Type Conversion in Assignments . . . . . . . . . . . . . . . . . . . . . Multiple Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Increment and Decrement . . . . . . . . . . . . . . . . . . . . . . . . . . . Relational and Logical Operators . . . . . . . . . . . . . . . . . . . . . Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The ? Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The & and * Pointer Operators . . . . . . . . . . . . . . . . . . . . . . . The Compile-Time Operator sizeof . . . . . . . . . . . . . . . . . . . The Comma Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Dot (.) and Arrow (>) Operators . . . . . . . . . . . . . . . . . The [ ] and ( ) Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Precedence Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Order of Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Type Conversion in Expressions . . . . . . . . . . . . . . . . . . . . . . Casts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Spacing and Parentheses . . . . . . . . . . . . . . . . . . . . . . . . . . . . Compound Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . .321 21 23 23 24 25 25 28 30 31 32 33 33 33 35 35 35 37 37 38 40 42 47 48 50 50 51 51 52 52 52 53 54 55 56Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57True and False in C and C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Selection Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Nested ifs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The if-else-if Ladder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The ? Alternative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Conditional Expression . . . . . . . . . . . . . . . . . . . . . . . . . .58 59 59 60 62 63 66 12. Contentsswitch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Nested switch Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . Iteration Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . for Loop Variations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Infinite Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . for Loops with No Bodies . . . . . . . . . . . . . . . . . . . . . . . . . . . The while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The do-while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Declaring Variables Within Selection and Iteration Statements . . . Jump Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The return Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The goto Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The break Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The exit( ) Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The continue Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Expression Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Block Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .67 70 70 70 72 76 77 77 79 81 82 82 83 83 85 86 88 885Arrays and Null-Terminated Strings . . . . . . . . . . . . . . . .89Single-Dimension Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Generating a Pointer to an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . Passing Single-Dimension Arrays to Functions . . . . . . . . . . . . . . . . Null-Terminated Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Two-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Arrays of Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Indexing Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Array Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Unsized Array Initializations . . . . . . . . . . . . . . . . . . . . . . . . . A Tic-Tac-Toe Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .490 92 92 94 96 100 101 102 105 106 108Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .113What Are Pointers? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Pointer Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Pointer Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Pointer Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Pointer Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Pointer Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Pointer Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Pointers and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Arrays of Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Multiple Indirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Initializing Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Pointers to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .114 115 115 116 117 117 119 121 122 123 125 126xi 13. xiiC++: The Complete ReferenceC's Dynamic Allocation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . Problems with Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129 1318137 138 138 139 139 140 142 144 147 148 149 151 152 153 153 155 157 158 158Structures, Unions, Enumerations, and User-Defined Types . . . . . . . . . . . . . . . . . . . . . . . . .161Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Accessing Structure Members . . . . . . . . . . . . . . . . . . . . . . . . Structure Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Arrays of Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Passing Structures to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Passing Structure Members to Functions . . . . . . . . . . . . . . . Passing Entire Structures to Functions . . . . . . . . . . . . . . . . . Structure Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Declaring a Structure Pointer . . . . . . . . . . . . . . . . . . . . . . . . . Using Structure Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Arrays and Structures Within Structures . . . . . . . . . . . . . . . . . . . . . . Bit-Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using sizeof to Ensure Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . typedef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The General Form of a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Scope Rules of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Function Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Call by Value, Call by Reference . . . . . . . . . . . . . . . . . . . . . . Creating a Call by Reference . . . . . . . . . . . . . . . . . . . . . . . . . Calling Functions with Arrays . . . . . . . . . . . . . . . . . . . . . . . argc and argvArguments to main( ) . . . . . . . . . . . . . . . . . . . . . . . . The return Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Returning from a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . Returning Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Returning Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Functions of Type void . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What Does main( ) Return? . . . . . . . . . . . . . . . . . . . . . . . . . . Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Function Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Standard Library Function Prototypes . . . . . . . . . . . . . . . . . Declaring Variable-Length Parameter Lists . . . . . . . . . . . . . . . . . . . . Old-Style Versus Modern FunctionParameter Declarations . . . . . .6162 165 165 166 167 167 168 169 170 170 173 174 176 180 183 184C-Style Console I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .187An Important Application Note . . . . . . . . . . . . . . . . . . . . . . . . . . . . .188 14. ContentsReading and Writing Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A Problem with getchar( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . Alternatives to getchar( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . Reading and Writing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Formatted Console I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . printf( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Printing Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Printing Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Displaying an Address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The %n Specifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Format Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Minimum Field Width Specifier . . . . . . . . . . . . . . . . . . The Precision Specifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Justifying Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Handling Other Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . The * and # Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . scanf( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Format Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Inputting Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Inputting Unsigned Integers . . . . . . . . . . . . . . . . . . . . . . . . . Reading Individual Characters Using scanf( ) . . . . . . . . . . Reading Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Inputting an Address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The %n Specifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using a Scanset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Discarding Unwanted White Space . . . . . . . . . . . . . . . . . . . Non-White-Space Characters in the Control String . . . . . . You Must Pass scanf( ) Addresses . . . . . . . . . . . . . . . . . . . . . Format Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Suppressing Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9189 190 190 192 195 195 196 196 198 198 199 199 200 201 202 202 203 203 203 205 205 205 206 206 206 207 208 208 208 209File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211C Versus C++ File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Streams and Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Text Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Binary Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . File System Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The File Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Opening a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Closing a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Writing a Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Reading a Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .212 212 212 213 213 213 214 215 215 217 217 218xiii 15. xivC++: The Complete ReferenceUsing fopen( ), getc( ), putc( ), and fclose( ) . . . . . . . . . . . . . Using feof( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Working with Strings: fputs( ) and fgets( ) . . . . . . . . . . . . . . rewind( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ferror( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Erasing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Flushing a Stream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . fread( ) and fwrite( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using fread( ) and fwrite( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . fseek( ) and Random-Access I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . fprintf( ) and fscanf( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Standard Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Console I/O Connection . . . . . . . . . . . . . . . . . . . . . . . . . Using freopen( ) to Redirect the Standard Streams . . . . . .The Preprocessor and Comments . . . . . . . . . . . . . . . . . .237The Preprocessor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . #define . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Defining Function-like Macros . . . . . . . . . . . . . . . . . . . . . . . #error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . #include . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Conditional Compilation Directives . . . . . . . . . . . . . . . . . . . . . . . . . . #if, #else, #elif, and #endif . . . . . . . . . . . . . . . . . . . . . . . . . . . #ifdef and #ifndef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . #undef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using defined . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . #line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . #pragma . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The # and ## Preprocessor Operators . . . . . . . . . . . . . . . . . . . . . . . . . Predefined Macro Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Single-Line Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10218 220 222 223 224 226 226 227 227 229 230 232 233 234238 238 240 241 242 242 243 245 246 247 248 248 248 250 250 252Part II C++11An Overview of C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . .255The Origins of C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What Is Object-Oriented Programming? . . . . . . . . . . . . . . . . . . . . . . Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Some C++ Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A Sample C++ Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . .256 257 258 258 259 259 260 16. ContentsA Closer Look at the I/O Operators . . . . . . . . . . . . . . . . . . . Declaring Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . No Default to int . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The bool Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Old-Style vs. Modern C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The New C++ Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Working with an Old Compiler . . . . . . . . . . . . . . . . . . . . . . . Introducing C++ Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Function Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Constructors and Destructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The C++ Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The General Form of a C++ Program . . . . . . . . . . . . . . . . . . . . . . . . .263 264 265 266 267 268 269 270 270 275 278 278 283 287 28813Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .289Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Structures and Classes Are Related . . . . . . . . . . . . . . . . . . . . . . . . . . . Unions and Classes Are Related . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Anonymous Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Friend Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Friend Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Inline Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Defining Inline Functions Within a Class . . . . . . . . . . . . . . Parameterized Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Constructors with One Parameter: A Special Case . . . . . . . Static Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Static Data Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Static Member Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . When Constructors and Destructors Are Executed . . . . . . . . . . . . . The Scope Resolution Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Nested Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Local Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Passing Objects to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Returning Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Object Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12290 293 295 296 297 302 303 306 307 309 310 310 315 317 319 319 320 320 323 324Arrays, Pointers, References, and the Dynamic Allocation Operators . . . . . . . . . . . . . . . . . . . . . . . . . . .325Arrays of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating Initialized vs. Uninitialized Arrays . . . . . . . . . . . Pointers to Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Type Checking C++ Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .326 328 329 332xv 17. xviC++: The Complete ReferenceThe this Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Pointers to Derived Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Pointers to Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Reference Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Passing References to Objects . . . . . . . . . . . . . . . . . . . . . . . . Returning References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Independent References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . References to Derived Types . . . . . . . . . . . . . . . . . . . . . . . . . Restrictions to References . . . . . . . . . . . . . . . . . . . . . . . . . . . . A Matter of Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . C++'s Dynamic Allocation Operators . . . . . . . . . . . . . . . . . . . . . . . . . Initializing Allocated Memory . . . . . . . . . . . . . . . . . . . . . . . Allocating Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Allocating Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The nothrow Alternative . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Placement Form of new . . . . . . . . . . . . . . . . . . . . . . . . .15Function Overloading, Copy Constructors, and Default Arguments . . . . . . . . . . . . . . . . . . . . . . . . .359Function Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Overloading Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Overloading a Constructor to Gain Flexibility . . . . . . . . . . Allowing Both Initialized and Uninitialized Objects . . . . . Copy Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Finding the Address of an Overloaded Function . . . . . . . . . . . . . . . The overload Anachronism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Default Function Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Default Arguments vs. Overloading . . . . . . . . . . . . . . . . . . Using Default Arguments Correctly . . . . . . . . . . . . . . . . . . . Function Overloading and Ambiguity . . . . . . . . . . . . . . . . . . . . . . . .14332 334 337 339 339 343 344 345 346 347 347 347 349 350 351 356 357360 362 362 364 366 370 371 371 376 377 378Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . .383Creating a Member Operator Function . . . . . . . . . . . . . . . . . . . . . . . Creating Prefix and Postfix Forms of the Increment and Decrement Operators . . . . . . . . . . Overloading the Shorthand Operators . . . . . . . . . . . . . . . . . Operator Overloading Restrictions . . . . . . . . . . . . . . . . . . . . Operator Overloading Using a Friend Function . . . . . . . . . . . . . . . . Using a Friend to Overload ++ or . . . . . . . . . . . . . . . . . . Friend Operator Functions Add Flexibility . . . . . . . . . . . . . Overloading new and delete . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Overloading new and delete for Arrays . . . . . . . . . . . . . . . Overloading the nothrow Version of new and delete . . . .384 389 390 390 391 393 396 398 403 406 18. ContentsOverloading Some Special Operators . . . . . . . . . . . . . . . . . . . . . . . . . Overloading [ ] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Overloading ( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Overloading > . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Overloading the Comma Operator . . . . . . . . . . . . . . . . . . . . . . . . . . .407 407 411 413 414Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .417Base-Class Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Inheritance and protected Members . . . . . . . . . . . . . . . . . . . . . . . . . . Protected Base-Class Inheritance . . . . . . . . . . . . . . . . . . . . . . Inheriting Multiple Base Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Constructors, Destructors, and Inheritance . . . . . . . . . . . . . . . . . . . . When Constructors and Destructors Are Executed . . . . . . Passing Parameters to Base-Class Constructors . . . . . . . . . Granting Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Virtual Base Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16418 420 424 425 426 426 430 434 43718Virtual Functions and Polymorphism . . . . . . . . . . . . . . .443Virtual Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Calling a Virtual Function Through a Base Class Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Virtual Attribute Is Inherited . . . . . . . . . . . . . . . . . . . . . . . . . . . . Virtual Functions Are Hierarchical . . . . . . . . . . . . . . . . . . . . . . . . . . . Pure Virtual Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Virtual Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Early vs. Late Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17444 447 448 450 453 455 455 458Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .459Generic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A Function with Two Generic Types . . . . . . . . . . . . . . . . . . . Explicitly Overloading a Generic Function . . . . . . . . . . . . . Overloading a Function Template . . . . . . . . . . . . . . . . . . . . . Using Standard Parameters with Template Functions . . . . Generic Function Restrictions . . . . . . . . . . . . . . . . . . . . . . . . Applying Generic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A Generic Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Compacting an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Generic Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . An Example with Two Generic Data Types . . . . . . . . . . . . . Applying Template Classes: A Generic Array Class . . . . . Using Non-Type Arguments with Generic Classes . . . . . . Using Default Arguments with Template Classes . . . . . . . Explicit Class Specializations . . . . . . . . . . . . . . . . . . . . . . . . .460 463 463 466 466 467 468 469 470 472 476 477 479 481 483xvii 19. xviiiC++: The Complete ReferenceThe typename and export Keywords . . . . . . . . . . . . . . . . . . . . . . . . . The Power of Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .484 485Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .487Exception Handling Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . Catching Class Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Multiple catch Statements . . . . . . . . . . . . . . . . . . . . . . Handling Derived-Class Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . Exception Handling Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Catching All Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Restricting Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Rethrowing an Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . Understanding terminate( ) and unexpected( ) . . . . . . . . . . . . . . . . . Setting the Terminate and Unexpected Handlers . . . . . . . . The uncaught_exception( ) Function . . . . . . . . . . . . . . . . . . . . . . . . . The exception and bad_exception Classes . . . . . . . . . . . . . . . . . . . . . Applying Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19488 494 495 497 498 498 500 502 503 504 505 506 50621The C++ I/O System Basics . . . . . . . . . . . . . . . . . . . . . . .509Old vs. Modern C++ I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . C++ Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The C++ Stream Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . C++'s Predefined Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . Formatted I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Formatting Using the ios Members . . . . . . . . . . . . . . . . . . . . Setting the Format Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Clearing Format Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . An Overloaded Form of setf( ) . . . . . . . . . . . . . . . . . . . . . . . . Examining the Formatting Flags . . . . . . . . . . . . . . . . . . . . . . Setting All Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using width( ), precision( ), and fill( ) . . . . . . . . . . . . . . . . . Using Manipulators to Format I/O . . . . . . . . . . . . . . . . . . . Overloading > . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating Your Own Inserters . . . . . . . . . . . . . . . . . . . . . . . . . Creating Your Own Extractors . . . . . . . . . . . . . . . . . . . . . . . . Creating Your Own Manipulator Functions . . . . . . . . . . . .20510 511 511 512 513 513 514 515 516 518 519 520 522 526 526 532 535C++ File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .539 and the File Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Opening and Closing a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Reading and Writing Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Unformatted and Binary I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Characters vs. Bytes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . put( ) and get( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . read( ) and write( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .540 540 543 545 545 546 548 20. ContentsMore get( ) Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . getline( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Detecting EOF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The ignore( ) Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . peek( ) and putback( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . flush( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Random Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Obtaining the Current File Position . . . . . . . . . . . . . . . . . . . I/O Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Customized I/O and Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .551 551 553 555 556 556 557 561 561 56324567 568 578 578Namespaces, Conversion Functions, and Other Advanced Topics . . . . . . . . . . . . . . . . . . . . .591Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Namespace Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . using . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Unnamed Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Some Namespace Options . . . . . . . . . . . . . . . . . . . . . . . . . . . The std Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating Conversion Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . const Member Functions and mutable . . . . . . . . . . . . . . . . . . . . . . . . Volatile Member Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Explicit Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Member Initialization Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using the asm Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Linkage Specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Array-Based I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Array-Based Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating an Array-Based Output Stream . . . . . . . . . . . . . . . Using an Array as Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Input/Output Array-Based Streams . . . . . . . . . . . . . . . . . . . Using Dynamic Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Binary I/O with Array-Based Streams . . . . . . . . . . . Summarizing the Differences Between C and C++ . . . . . . . . . . . . .23Run-Time Type ID and the Casting Operators . . . . . . . Run-Time Type Identification (RTTI) . . . . . . . . . . . . . . . . . . . . . . . . . The Casting Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . dynamic_cast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22592 592 596 598 599 601 603 607 609 610 611 616 617 618 619 619 621 623 624 625 626Introducing the Standard Template Library . . . . . . . . . .629An Overview of the STL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .630 630xix 21. xxC++: The Complete ReferenceAlgorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Other STL Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Container Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . General Theory of Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Accessing a Vector Through an Iterator . . . . . . . . . . . . . . . . Inserting and Deleting Elements in a Vector . . . . . . . . . . . . Storing Class Objects in a Vector . . . . . . . . . . . . . . . . . . . . . . Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Understanding end( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . push_front( ) vs. push_back( ) . . . . . . . . . . . . . . . . . . . . . . . . Sort a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Merging One List with Another . . . . . . . . . . . . . . . . . . . . . . Storing Class Objects in a List . . . . . . . . . . . . . . . . . . . . . . . . Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Storing Class Objects in a Map . . . . . . . . . . . . . . . . . . . . . . . Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Removing and Replacing Elements . . . . . . . . . . . . . . . . . . . Reversing a Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Transforming a Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Function Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Unary and Binary Function Objects . . . . . . . . . . . . . . . . . . . Using the Built-in Function Objects . . . . . . . . . . . . . . . . . . . Creating a Function Object . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Binders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The string Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Some string Member Functions . . . . . . . . . . . . . . . . . . . . . . . Strings Are Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Putting Strings into Other Containers . . . . . . . . . . . . . . . . . Final Thoughts on the STL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .631 631 632 633 634 635 639 641 643 645 649 651 652 653 655 658 662 664 664 670 672 673 675 675 675 678 680 683 687 693 694 695Part III The Standard Function Library25The C-Based I/O Functions . . . . . . . . . . . . . . . . . . . . . . .699clearerr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . fclose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . feof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ferror . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . fflush . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . fgetc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . fgetpos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .700 701 701 701 702 702 702 22. Contentsfgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . fopen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . fprintf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . fputc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . fputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . fread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . freopen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . fscanf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . fseek . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . fsetpos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ftell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . fwrite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . getc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . getchar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . gets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . perror . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . printf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . putc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . putchar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . puts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . remove . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . rename . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . rewind . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . scanf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . setbuf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . setvbuf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sprintf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sscanf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . tmpfile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . tmpnam . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ungetc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vprintf, vfprintf, and vsprintf . . . . . . . . . . . . . . . . . . . . . . . .26703 703 705 705 706 706 706 707 707 708 708 709 709 710 710 710 711 714 714 714 715 715 715 715 719 719 720 720 720 721 721 722The String and Character Functions . . . . . . . . . . . . . . . .723isalnum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . isalpha . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iscntrl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . isdigit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . isgraph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . islower . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . isprint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ispunct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . isspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . isupper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .724 724 725 725 725 725 726 726 726 727xxi 23. xxiiC++: The Complete Referenceisxdigit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . memchr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . memcmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . memcpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . memmove . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . memset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . strcat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . strchr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . strcmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . strcoll . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . strcpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . strcspn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . strerror . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . strlen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . strncat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . strncmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . strncpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . strpbrk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . strrchr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . strspn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . strstr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . strtok . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . strxfrm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . tolower . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . toupper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27727 727 727 728 728 729 729 729 730 730 731 731 731 731 732 732 733 733 733 734 734 734 735 735 735The Mathematical Functions . . . . . . . . . . . . . . . . . . . . . .737acos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . asin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . atan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . atan2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ceil . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . cos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . cosh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . exp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . fabs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . floor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . fmod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . frexp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ldexp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . log10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . modf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . pow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .738 738 739 739 739 740 740 740 741 741 741 741 742 742 742 743 743 743 24. Contentssinh sqrt tan tanh30Time, Date, and Localization Functions . . . . . . . . . . . . .747 748 749 749 750 750 750 752 752 752 753 754The Dynamic Allocation Functions . . . . . . . . . . . . . . . . .757calloc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . free . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . malloc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . realloc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .29744 744 744 745asctime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ctime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . difftime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . gmtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . localeconv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . localtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . mktime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . setlocale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . strftime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28............................................... ............................................... ............................................... ..............................................758 758 759 759Utility Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .761abort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . abs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . assert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . atexit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . atof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . atoi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . atol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . bsearch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . div . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . exit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . getenv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . labs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ldiv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . longjmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . mblen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . mbstowcs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . mbtowc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . qsort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . raise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .762 762 763 763 763 764 764 764 765 766 766 766 767 767 767 768 768 768 769xxiii 25. xxivC++: The Complete Referencerand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . setjmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . signal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . srand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . strtod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . strtol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . strtoul . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . va_arg, va_start, and va_end . . . . . . . . . . . . . . . . . . . . . . . . . wcstombs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . wctomb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Wide-Character Functions . . . . . . . . . . . . . . . . . . . . .775The Wide-Character Classification Functions . . . . . . . . . . . . . . . . . . The Wide-Character I/O Functions . . . . . . . . . . . . . . . . . . . . . . . . . . The Wide-Character String Functions . . . . . . . . . . . . . . . . . . . . . . . . . Wide-Character String Conversion Functions . . . . . . . . . . . . . . . . . . Wide-Character Array Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . Multibyte/Wide-Character Conversion Functions . . . . . . . . . . . . . .31770 770 770 771 771 772 772 773 773 774 774776 779 779 779 782 783Part IV The Standard C++ Class Library32The Standard C++ I/O Classes . . . . . . . . . . . . . . . . . . . .787The I/O Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The I/O Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Format Flags and I/O Manipulators . . . . . . . . . . . . . . . . . . . . . . Several Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The streamsize and streamoff Types . . . . . . . . . . . . . . . . . . . The streampos and wstreampos Types . . . . . . . . . . . . . . . . The pos_type and off_type Types . . . . . . . . . . . . . . . . . . . . . The openmode Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The iostate Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The seekdir Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The failure Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Overload > Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The General-Purpose I/O Functions . . . . . . . . . . . . . . . . . . . . . . . . . bad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . clear . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . eof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . fail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . fill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .788 790 791 793 793 793 793 793 794 794 794 794 795 795 795 795 796 796 796 797 26. Contentsflush . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . fstream, ifstream, and ofstream . . . . . . . . . . . . . . . . . . . . . . . gcount . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . get . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . getline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . good . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ignore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . open . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . peek . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . precision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . put . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . putback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . rdstate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . read . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . readsome . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . seekg and seekp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . setf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . setstate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . str . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . stringstream, istringstream, ostringstream . . . . . . . . . . . . . sync_with_stdio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . tellg and tellp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . unsetf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . width . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . write . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .797 797 798 798 799 800 800 800 801 802 802 802 802 803 803 804 805 805 806 806 807 808 808 808 80934The STL Container Classes . . . . . . . . . . . . . . . . . . . . . . . .811The Container Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . bitset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . deque . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . multimap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . multiset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . priority_queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .33812 814 816 819 822 824 827 829 830 831 833 834The STL Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .839adjacent_find . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . binary_search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .840 840 841xxv 27. xxviC++: The Complete Referencecopy_backward . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . count . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . count_if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . equal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . equal_range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . fill and fill_n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . find . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . find_end . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . find_first_of . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . find_if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . for_each . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . generate and generate_n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . includes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . inplace_merge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iter_swap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . lexicographical_compare . . . . . . . . . . . . . . . . . . . . . . . . . . . . lower_bound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . make_heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . max . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . max_element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . merge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . min . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . min_element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . mismatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . next_permutation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . nth_element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . partial_sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . partial_sort_copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . partition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . pop_heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . prev_permutation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . push_heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . random_shuffle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . remove, remove_if, remove_copy, and remove_copy_if . replace, replace_copy, replace_if, and replace_copy_if . . . reverse and reverse_copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . rotate and rotate_copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . search_n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . set_difference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . set_intersection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . set_symmetric_difference . . . . . . . . . . . . . . . . . . . . . . . . . . . . set_union . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sort_heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .841 841 842 842 842 843 843 843 843 844 844 844 845 845 845 846 846 846 847 847 847 848 848 848 849 849 849 850 850 850 851 851 851 852 852 853 853 854 854 854 855 855 856 856 856 28. Contentsstable_partition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . stable_sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . swap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . swap_ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . unique and unique_copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . upper_bound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .857 857 857 858 858 858 859STL Iterators, Allocators, and Function Objects . . . . . .861Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Basic Iterator Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Low-Level Iterator Classes . . . . . . . . . . . . . . . . . . . . . . . The Predefined Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Two Iterator Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Function Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Function Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Binders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Negators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Adaptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Allocators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35862 862 863 864 872 872 873 874 875 876 879The String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .881The basic_string Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The char_traits Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36882 89438The Numeric Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . .897The complex Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The valarray Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The slice and gslice Classes . . . . . . . . . . . . . . . . . . . . . . . . . . The Helper Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Numeric Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . accumulate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . adjacent_difference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . inner_product . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . partial_sum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37898 902 917 920 920 920 921 922 923Exception Handling and Miscellaneous Classes . . . . . .925Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . auto_ptr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The pair Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Other Classes of Interest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .926 926 927 928 930 931 931xxvii 29. xxviiiC++: The Complete ReferencePart V Applying C++935 936 938 939 941 942 945 947 948 949 958 961 961Parsing Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .963Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Parsing Expressions: The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . Parsing an Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Parser Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Dissecting an Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A Simple Expression Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Understanding the Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . Adding Variables to the Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Syntax Checking in a Recursive-Descent Parser . . . . . . . . . . . . . . . . Building a Generic Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Some Things to Try . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .40Integrating New Classes: A Custom String Class . . . . . The StrType Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Constructors and Destructors . . . . . . . . . . . . . . . . . . . . . . . . . . . I/O on Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Assignment Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Substring Subtraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Miscellaneous String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Entire StrType Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using the StrType Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating and Integrating New Types in General . . . . . . . . . . . . . . . A Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39964 965 966 968 969 971 977 978 988 989 997BThe .NET Managed Extensions to C++ . . . . . . . . . . . . . .999The .NET Keyword Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Preprocessor Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The attribute Attribute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Compiling Managed C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .A1000 1002 1003 1003C++ and the Robotics Age . . . . . . . . . . . . . . . . . . . . . . . . 1005 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1009 30. f there is one language that defines modern programming, it is C++. Its syntax, style, and philosophy have set the standard by which all other languages are judged. Furthermore, C++ is the universal language of programming. When an algorithm or technique is described, it is usually done so using the C++ syntax. The long-term success of C++ has also left a lasting impression on computer language development. For example, both Java and C# are descended from C++. Frankly, to be a professional programmer implies proficiency in C++. It is the one language that no programmer can afford to ignore. This is the fourth edition of C++: The Complete Reference. It fully describes and demonstrates the keywords, syntax, functions, classes, and features that define the C++ language. More specifically, this book fully describes Standard C++. This is the version of C++ defined by the ANSI/ISO Standard for C++ and it is the version of C++ that is supported by all major compilers, including Microsofts Visual C++ and Borlands C++ Builder. Thus, the information in this book is applicable to all modern programming environments. In the time that has passed since the previous edition of this book, there have been no changes to the C++ language. There have, however, been big changes to the computing environment. For example, a new standard for C, called C99, was created, Java became the dominant language for Web programming, the .NET Framework wasIxxix Copyright 2003 by The McGraw-Hill Companies. Click here for terms of use. 31. xxxC++: The Complete Referencereleased, and C# was invented. Through all the changes of the past few years, one thing has remained constant: the staying power of C++. C++ has been, is, and will remain the preeminent language for the development of high-performance software well into the foreseeable future.Whats New in the Fourth Edition The overall structure and organization of the fourth edition is similar to the third edition. Thus, if you have been using the third edition, you will feel right at home with the fourth edition. Most of the changes to the fourth edition involve updating and expanding the coverage throughout. In some cases, additional details were added. In other cases, the presentation of a topic was improved. In still other situations, descriptions were modernized to reflect the current programming environment. Several new sections were also added. In Part One, the relationship of C++ to the new C standard, called C99, is noted where appropriate. Two appendices were also added. The first described the extended keywords defined by Microsoft that are used for creating managed code for the .NET Framework. The second shows off an area of personal interest: robotics. Robotics has long been a hobby of mine and I thought that many readers would find my experimental robot to be of interest. Most of the software that drives it is, of course, written in C++! Finally, all code examples were retested against the current crop of compilers, including Microsofts Visual Studio .NET and Borlands C++ Builder.Whats Inside This books covers in detail all aspects of the C++ language, including its foundation, C. The book is divided into these five parts: The C SubsetThe foundation of C++ The C++ language The Standard Function Library The Standard Class Library Sample C++ applications Part One provides a comprehensive discussion of the C subset of C++. As most readers will know, C is the foundation upon which C++ was built. It is the C subset that defines the bedrock features of C++, including such things as for loops and if statements. It also defines the essential nature of C++s block structure, pointers, and functions. Since many readers are already familiar with and proficient in C, discussing the C subset separately in Part One prevents the knowledgeable C programmer from having to wade through reams of information he or she already knows. Instead, the 32. xxxiexperienced C programmer can simply turn to the sections of this book that cover the C++-specific features. Part Two discusses in detail the features that move beyond the C foundation and define the C++ language These include its object-oriented features such as classes, constructors, destructors, RTTI, and templates. Thus, Part Two covers those constructs that make C++, C++. Part Three describes the standard function library and Part Four examines the standard class library, including the STL (Standard Template Library). Part Five shows two practical examples of applying C++ and object-oriented programming.A Book for All Programmers This C++ reference is designed for all C++ programmers, regardless of their experience level. It does assume, however, a reader able to create at least a simple program. If you are just learning C++, this book will make an excellent companion to any C++ tutorial and serve as a source of answers to your specific questions. Experienced C++ pros will find the in-depth coverage of C++s more advanced features especially useful.If Youre Using Windows If your computer uses Windows, then you have chosen the right language. C++ is completely at home with Windows programming. However, none of the programs in this book are Windows programs. Instead, they are console-based programs. The reason for this is easy to understand: Windows programs are, by their nature, large and complex. The overhead required to create even a minimal Windows skeletal program is 50 to 70 lines of code. To write Windows programs that demonstrate the features of C++ would require hundreds of lines of code each. Put simply, Windows is not an appropriate environment in which to discuss the features of a programming language. However, you can still use a Windows-based compiler to compile the programs in this book because the compiler will automatically create a console session in which to execute your program.Dont Forget: Code on the Web Remember, the source code for all of the programs in this book is available free-ofcharge on the Web at www.osborne.com. Downloading this code prevents you from having to type in the examples. 33. xxxiiC++: The Complete ReferenceFor Further Study C++: The Complete Reference is your gateway to the Herb Schildt series of programming books. Here are some others that you will find of interest. To learn more about C++, try C++: A Beginners Guide C++ from the Ground Up Teach Yourself C++ STL Programming from the Ground Up C++ Programmers Reference To learn about Java programming, we recommend the following: Java 2: A Beginners Guide Java 2: The Complete Reference Java 2 Programmers Reference To learn about C#, Herb offers these books: C#: A Beginners Guide C#: The Complete Reference To learn about Windows programming we suggest the following Schildt books: Windows 98 Programming from the Ground Up Windows 2000 Programming from the Ground Up MFC Programming from the Ground Up The Windows Programming Annotated Archives If you want to learn about the C language, which is the found