You are previewing C++ Cookbook.
O'Reilly logo
C++ Cookbook

Book Description

Despite its highly adaptable and flexible nature, C++ is also one of the more complex programming languages to learn. Once mastered, however, it can help you organize and process information with amazing efficiency and quickness.

The C++ Cookbook will make your path to mastery much shorter. This practical, problem-solving guide is ideal if you're an engineer, programmer, or researcher writing an application for one of the legions of platforms on which C++ runs. The algorithms provided in C++ Cookbook will jump-start your development by giving you some basic building blocks that you don't have to develop on your own.

Less a tutorial than a problem-solver, the book addresses many of the most common problems you're likely encounter--whether you've been programming in C++ for years or you're relatively new to the language. Here are just some of the time-consuming tasks this book contains practical solutions for:

  • Reading the contents of a directory

  • Creating a singleton class

  • Date and time parsing/arithmetic

  • String and text manipulation

  • Working with files

  • Parsing XML

  • Using the standard containers

Typical of O'Reilly's "Cookbook" series, C++ Cookbook is written in a straightforward format, featuring recipes that contain problem statements and code solutions, and apply not to hypothetical situations, but those that you're likely to encounter. A detailed explanation then follows each recipe in order to show you how and why the solution works. This question-solution-discussion format is a proven teaching method, as any fan of the "Cookbook" series can attest to. This book will move quickly to the top of your list of essential C++ references.

Table of Contents

  1. C++ Cookbook
  2. A Note Regarding Supplemental Files
  3. Preface
    1. About the Examples
    2. Conventions Used in This Book
    3. Using Code Examples
    4. Comments and Questions
    5. Safari Enabled
    6. Acknowledgments
      1. From D. Ryan Stephens
      2. From Christopher Diggins
      3. From Jonathan Turkanis
  4. 1. Building C++ Applications
    1. Introduction to Building
      1. Basic Terminology
      2. IDEs and Build Systems
      3. Toolset Overview
        1. The GNU Compiler Collection (GCC)
        2. Visual C++
        3. Intel
        4. Metrowerks
        5. Borland
        6. Comeau
        7. Digital Mars
      4. IDE Overview
        1. Visual C++
        2. CodeWarrior
        3. C++Builder
        4. Dev-C++
      5. John, Paul, George, and Ringo
    2. 1.1. Obtaining and Installing GCC
      1. Problem
      2. Solution
        1. Windows
        2. Unix
      3. Discussion
      4. See Also
    3. 1.2. Building a Simple "Hello, World" Application from the Command Line
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 1.3. Building a Static Library from the Command Line
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 1.4. Building a Dynamic Library from the Command Line
      1. Problem
      2. Solution
      3. Discussion
        1. Symbol visibility
        2. Passing libraries to the linker
        3. Import libraries and module definition files
        4. Exporting symbols from a DLL
        5. Importing symbols from a DLL
        6. Building DLLs with GCC
        7. GCC 4.0's -fvisibility option
        8. Symbol Visibility with Metrowerks for Mac OS X
        9. Command-line options
      4. See Also
    6. 1.5. Building a Complex Application from the Command Line
      1. Problem
      2. Solution
      3. Discussion
        1. Searching for included headers
        2. Passing libraries to the linker
        3. Running your application
      4. See Also
    7. 1.6. Installing Boost.Build
      1. Problem
      2. Solution
      3. Discussion
    8. 1.7. Building a Simple "Hello, World" Application Using Boost.Build
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. 1.8. Building a Static Library Using Boost.Build
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    10. 1.9. Building a Dynamic Library Using Boost.Build
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    11. 1.10. Building a Complex application Using Boost.Build
      1. Problem
      2. Solution
      3. Discussion
        1. Library targets
          1. Installation
          2. Project organization
      4. See Also
    12. 1.11. Building a Static Library with an IDE
      1. Problem
      2. Solution
      3. Discussion
        1. CodeWarrior
        2. C++Builder
        3. Dev-C++
      4. See Also
    13. 1.12. Building a Dynamic Library with an IDE
      1. Problem
      2. Solution
      3. Discussion
        1. CodeWarrior
        2. C++Builder
        3. Dev-C++
      4. See Also
    14. 1.13. Building a Complex Application with an IDE
      1. Problem
      2. Solution
      3. Discussion
        1. CodeWarrior
        2. C++Builder
        3. Dev-C++
      4. See Also
    15. 1.14. Obtaining GNU make
      1. Problem
      2. Solution
        1. Windows
        2. Unix
      3. Discussion
      4. See Also
    16. 1.15. Building A Simple "Hello, World" Application with GNU make
      1. Problem
      2. Solution
      3. Discussion
        1. Make variables
        2. Implicit Rules
        3. Customization points
        4. VPATH and the vpath directive
      4. See Also
    17. 1.16. Building a Static Library with GNU Make
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    18. 1.17. Building a Dynamic Library with GNU Make
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    19. 1.18. Building a Complex Application with GNU make
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    20. 1.19. Defining a Macro
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    21. 1.20. Specifying a Command-Line Option from Your IDE
      1. Problem
      2. Solution
      3. Discussion
    22. 1.21. Producing a Debug Build
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    23. 1.22. Producing a Release Build
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    24. 1.23. Specifying a Runtime Library Variant
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    25. 1.24. Enforcing Strict Conformance to the C++ Standard
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    26. 1.25. Causing a Source File to Be Linked Automatically Against a Specified Library
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    27. 1.26. Using Exported Templates
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  5. 2. Code Organization
    1. Introduction
    2. 2.1. Making Sure a Header File Gets Included Only Once
      1. Problem
      2. Solution
      3. Discussion
    3. 2.2. Ensuring You Have Only One Instance of a Variable Across Multiple Source Files
      1. Problem
      2. Solution
      3. Discussion
    4. 2.3. Reducing #includes with Forward Class Declarations
      1. Problem
      2. Solution
      3. Discussion
    5. 2.4. Preventing Name Collisions with Namespaces
      1. Problem
      2. Solution
      3. Discussion
    6. 2.5. Including an Inline File
      1. Problem
      2. Solution
  6. 3. Numbers
    1. Introduction
    2. 3.1. Converting a String to a Numeric Type
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 3.2. Converting Numbers to Strings
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 3.3. Testing Whether a String Contains a Valid Number
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 3.4. Comparing Floating-Point Numbers with Bounded Accuracy
      1. Problem
      2. Solution
      3. Discussion
    6. 3.5. Parsing a String Containing a Number in Scientific Notation
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. 3.6. Converting Between Numeric Types
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. 3.7. Getting the Minimum and Maximum Values for a Numeric Type
      1. Problem
      2. Solution
      3. Discussion
  7. 4. Strings and Text
    1. Introduction
    2. 4.1. Padding a String
      1. Problem
      2. Solution
      3. Discussion
    3. 4.2. Trimming a String
      1. Problem
      2. Solution
      3. Discussion
    4. 4.3. Storing Strings in a Sequence
      1. Problem
      2. Solution
      3. Discussion
    5. 4.4. Getting the Length of a String
      1. Problem
      2. Solution
      3. Discussion
    6. 4.5. Reversing a String
      1. Problem
      2. Solution
      3. Discussion
    7. 4.6. Splitting a String
      1. Problem
      2. Solution
      3. Discussion
    8. 4.7. Tokenizing a String
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. 4.8. Joining a Sequence of Strings
      1. Problem
      2. Solution
      3. Discussion
    10. 4.9. Finding Things in Strings
      1. Problem
      2. Solution
      3. Discussion
    11. 4.10. Finding the nth Instance of a Substring
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    12. 4.11. Removing a Substring from a String
      1. Problem
      2. Solution
      3. Discussion
    13. 4.12. Converting a String to Lower- or Uppercase
      1. Problem
      2. Solution
      3. Discussion
    14. 4.13. Doing a Case-Insensitive String Comparison
      1. Problem
      2. Solution
      3. Discussion
    15. 4.14. Doing a Case-Insensitive String Search
      1. Problem
      2. Solution
      3. Discussion
    16. 4.15. Converting Between Tabs and Spaces in a Text File
      1. Problem
      2. Solution
      3. Discussion
    17. 4.16. Wrapping Lines in a Text File
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    18. 4.17. Counting the Number of Characters, Words, and Lines in a Text File
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    19. 4.18. Counting Instances of Each Word in a Text File
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    20. 4.19. Add Margins to a Text File
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    21. 4.20. Justify a Text File
      1. Problem
      2. Solution
      3. Discussion
    22. 4.21. Squeeze Whitespace to Single Spaces in a Text File
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    23. 4.22. Autocorrect Text as a Buffer Changes
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    24. 4.23. Reading a Comma-Separated Text File
      1. Problem
      2. Solution
      3. Discussion
    25. 4.24. Using Regular Expressions to Split a String
      1. Problem
      2. Solution
      3. Discussion
  8. 5. Dates and Times
    1. Introduction
    2. 5.1. Obtaining the Current Date and Time
      1. Problem
      2. Solution
      3. Discussion
    3. 5.2. Formatting a Date/Time as a String
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 5.3. Performing Date and Time Arithmetic
      1. Problem
      2. Solution
      3. Discussion
    5. 5.4. Converting Between Time Zones
      1. Problem
      2. Solution
      3. Discussion
    6. 5.5. Determining a Day's Number Within a Given Year
      1. Problem
      2. Solution
      3. Discussion
    7. 5.6. Defining Constrained Value Types
      1. Problem
      2. Solution
      3. Discussion
  9. 6. Managing Data with Containers
    1. Introduction
    2. 6.1. Using vectors Instead of Arrays
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 6.2. Using vectors Efficiently
      1. Problem
      2. Solution
      3. Discussion
        1. How vectors work
        2. Optimizing vector performance
    4. 6.3. Copying a vector
      1. Problem
      2. Solution
      3. Discussion
    5. 6.4. Storing Pointers in a vector
      1. Problem
      2. Solution
      3. Discussion
    6. 6.5. Storing Objects in a list
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. 6.6. Mapping strings to Other Things
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. 6.7. Using Hashed Containers
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. 6.8. Storing Objects in Sorted Order
      1. Problem
      2. Solution
      3. Discussion
    10. 6.9. Storing Containers in Containers
      1. Problem
      2. Solution
      3. Discussion
  10. 7. Algorithms
    1. Introduction
    2. 7.1. Iterating Through a Container
      1. Problem
      2. Solution
      3. Discussion
        1. Using iterators
        2. Iterator categories
      4. See Also
    3. 7.2. Removing Objects from a Container
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 7.3. Randomly Shuffling Data
      1. Problem
      2. Solution
      3. Discussion
    5. 7.4. Comparing Ranges
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 7.5. Merging Data
      1. Problem
      2. Solution
      3. Discussion
    7. 7.6. Sorting a Range
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. 7.7. Partitioning a Range
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. 7.8. Performing Set Operations on Sequences
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    10. 7.9. Transforming Elements in a Sequence
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    11. 7.10. Writing Your Own Algorithm
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    12. 7.11. Printing a Range to a Stream
      1. Problem
      2. Solution
      3. Discussion
  11. 8. Classes
    1. Introduction
    2. 8.1. Initializing Class Member Variables
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 8.2. Using a Function to Create Objects (a.k.a. Factory Pattern)
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 8.3. Using Constructors and Destructors to Manage Resources (or RAII)
      1. Problem
      2. Solution
      3. Discussion
    5. 8.4. Automatically Adding New Class Instances to a Container
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 8.5. Ensuring a Single Copy of a Member Variable
      1. Problem
      2. Solution
      3. Discussion
    7. 8.6. Determining an Object's Type at Runtime
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. 8.7. Determining if One Object's Class Is a Subclass of Another
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. 8.8. Giving Each Instance of a Class a Unique Identifier
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    10. 8.9. Creating a Singleton Class
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    11. 8.10. Creating an Interface with an Abstract Base Class
      1. Problem
      2. Solution
      3. Discussion
    12. 8.11. Writing a Class Template
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    13. 8.12. Writing a Member Function Template
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    14. 8.13. Overloading the Increment and Decrement Operators
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    15. 8.14. Overloading Arithmetic and Assignment Operators for Intuitive Class Behavior
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    16. 8.15. Calling a Superclass Virtual Function
      1. Problem
      2. Solution
      3. Discussion
  12. 9. Exceptions and Safety
    1. Introduction
    2. 9.1. Creating an Exception Class
      1. Problem
      2. Solution
      3. Discussion
    3. 9.2. Making a Constructor Exception-Safe
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 9.3. Making an Initializer List Exception-Safe
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 9.4. Making Member Functions Exception-Safe
      1. Problem
      2. Solution
      3. Discussion
    6. 9.5. Safely Copying an Object
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  13. 10. Streams and Files
    1. Introduction
    2. 10.1. Lining Up Text Output
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 10.2. Formatting Floating-Point Output
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 10.3. Writing Your Own Stream Manipulators
      1. Problem
      2. Solution
      3. Discussion
    5. 10.4. Making a Class Writable to a Stream
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 10.5. Making a Class Readable from a Stream
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. 10.6. Getting Information About a File
      1. Problem
      2. Solution
      3. Discussion
    8. 10.7. Copying a File
      1. Problem
      2. Solution
      3. Discussion
    9. 10.8. Deleting or Renaming a File
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    10. 10.9. Creating a Temporary Filename and File
      1. Problem
      2. Solution
      3. Discussion
    11. 10.10. Creating a Directory
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    12. 10.11. Removing a Directory
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    13. 10.12. Reading the Contents of a Directory
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    14. 10.13. Extracting a File Extension from a String
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    15. 10.14. Extracting a Filename from a Full Path
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    16. 10.15. Extracting a Path from a Full Path and Filename
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    17. 10.16. Replacing a File Extension
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    18. 10.17. Combining Two Paths into a Single Path
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  14. 11. Science and Mathematics
    1. Introduction
    2. 11.1. Computing the Number of Elements in a Container
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 11.2. Finding the Greatest or Least Value in a Container
      1. Problem
      2. Solution
      3. Discussion
    4. 11.3. Computing the Sum and Mean of Elements in a Container
      1. Problem
      2. Solution
      3. Discussion
    5. 11.4. Filtering Values Outside a Given Range
      1. Problem
      2. Solution
      3. Discussion
    6. 11.5. Computing Variance, Standard Deviation, and Other Statistical Functions
      1. Problem
      2. Solution
      3. Discussion
    7. 11.6. Generating Random Numbers
      1. Problem
      2. Solution
      3. Discussion
    8. 11.7. Initializing a Container with Random Numbers
      1. Problem
      2. Solution
      3. Discussion
    9. 11.8. Representing a Dynamically Sized Numerical Vector
      1. Problem
      2. Solution
      3. Discussion
    10. 11.9. Representing a Fixed-Size Numerical Vector
      1. Problem
      2. Solution
      3. Discussion
    11. 11.10. Computing a Dot Product
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    12. 11.11. Computing the Norm of a Vector
      1. Problem
      2. Solution
      3. Discussion
    13. 11.12. Computing the Distance Between Two Vectors
      1. Problem
      2. Solution
      3. Discussion
    14. 11.13. Implementing a Stride Iterator
      1. Problem
      2. Solution
      3. Discussion
    15. 11.14. Implementing a Dynamically Sized Matrix
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    16. 11.15. Implementing a Constant-Sized Matrix
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    17. 11.16. Multiplying Matricies
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    18. 11.17. Computing the Fast Fourier Transform
      1. Problem
      2. Solution
      3. Discussion
    19. 11.18. Working with Polar Coordinates
      1. Problem
      2. Solution
      3. Discussion
    20. 11.19. Performing Arithmetic on Bitsets
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    21. 11.20. Representing Large Fixed-Width Integers
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    22. 11.21. Implementing Fixed-Point Numbers
      1. Problem
      2. Solution
      3. Discussion
  15. 12. Multithreading
    1. Introduction
    2. 12.1. Creating a Thread
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 12.2. Making a Resource Thread-Safe
      1. Problem
      2. Solution
      3. Discussion
        1. Using mutexes
        2. Dangers
    4. 12.3. Notifying One Thread from Another
      1. Problem
      2. Solution
      3. Discussion
    5. 12.4. Initializing Shared Resources Once
      1. Problem
      2. Solution
      3. Discussion
    6. 12.5. Passing an Argument to a Thread Function
      1. Problem
      2. Solution
      3. Discussion
  16. 13. Internationalization
    1. Introduction
    2. 13.1. Hardcoding a Unicode String
      1. Problem
      2. Solution
      3. Discussion
    3. 13.2. Writing and Reading Numbers
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 13.3. Writing and Reading Dates and Times
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 13.4. Writing and Reading Currency
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 13.5. Sorting Localized Strings
      1. Problem
      2. Solution
      3. Discussion
  17. 14. XML
    1. Introduction
    2. 14.1. Parsing a Simple XML Document
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 14.2. Working with Xerces Strings
      1. Problem
      2. Solution
      3. Discussion
    4. 14.3. Parsing a Complex XML Document
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 14.4. Manipulating an XML Document
      1. Problem
      2. Solution
      3. Discussion
    6. 14.5. Validating an XML Document with a DTD
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. 14.6. Validating an XML Document with a Schema
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. 14.7. Transforming an XML Document with XSLT
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. 14.8. Evaluating an XPath Expression
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    10. 14.9. Using XML to Save and Restore a Collection of Objects
      1. Problem
      2. Solution
      3. Discussion
  18. 15. Miscellaneous
    1. Introduction
    2. 15.1. Using Function Pointers for Callbacks
      1. Problem
      2. Solution
      3. Discussion
    3. 15.2. Using Pointers to Class Members
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 15.3. Ensuring That a Function Doesn't Modify an Argument
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 15.4. Ensuring That a Member Function Doesn't Modify Its Object
      1. Problem
      2. Solution
      3. Discussion
    6. 15.5. Writing an Operator That Isn't a Member Function
      1. Problem
      2. Solution
      3. Discussion
    7. 15.6. Initializing a Sequence with Comma-Separated Values
      1. Problem
      2. Solution
      3. Discussion
  19. Index
  20. About the Authors
  21. Colophon
  22. Copyright