- •Thinking in C++ 2nd edition Volume 2: Standard Libraries & Advanced Topics
- •Preface
- •What’s new in the second edition
- •What’s in Volume 2 of this book
- •How to get Volume 2
- •Prerequisites
- •Learning C++
- •Goals
- •Chapters
- •Exercises
- •Exercise solutions
- •Source code
- •Language standards
- •Language support
- •The book’s CD ROM
- •Seminars, CD Roms & consulting
- •Errors
- •Acknowledgements
- •Library overview
- •1: Strings
- •What’s in a string
- •Creating and initializing C++ strings
- •Initialization limitations
- •Operating on strings
- •Appending, inserting and concatenating strings
- •Replacing string characters
- •Concatenation using non-member overloaded operators
- •Searching in strings
- •Finding in reverse
- •Finding first/last of a set
- •Removing characters from strings
- •Stripping HTML tags
- •Comparing strings
- •Using iterators
- •Iterating in reverse
- •Strings and character traits
- •A string application
- •Summary
- •Exercises
- •2: Iostreams
- •Why iostreams?
- •True wrapping
- •Iostreams to the rescue
- •Sneak preview of operator overloading
- •Inserters and extractors
- •Manipulators
- •Common usage
- •Line-oriented input
- •Overloaded versions of get( )
- •Reading raw bytes
- •Error handling
- •File iostreams
- •Open modes
- •Iostream buffering
- •Seeking in iostreams
- •Creating read/write files
- •User-allocated storage
- •Output strstreams
- •Automatic storage allocation
- •Proving movement
- •A better way
- •Output stream formatting
- •Internal formatting data
- •Format fields
- •Width, fill and precision
- •An exhaustive example
- •Formatting manipulators
- •Manipulators with arguments
- •Creating manipulators
- •Effectors
- •Iostream examples
- •Code generation
- •Maintaining class library source
- •Detecting compiler errors
- •A simple datalogger
- •Generating test data
- •Verifying & viewing the data
- •Counting editor
- •Breaking up big files
- •Summary
- •Exercises
- •3: Templates in depth
- •Nontype template arguments
- •Typedefing a typename
- •Using typename instead of class
- •Function templates
- •A string conversion system
- •A memory allocation system
- •Type induction in function templates
- •Taking the address of a generated function template
- •Local classes in templates
- •Applying a function to an STL sequence
- •Template-templates
- •Member function templates
- •Why virtual member template functions are disallowed
- •Nested template classes
- •Template specializations
- •A practical example
- •Pointer specialization
- •Partial ordering of function templates
- •Design & efficiency
- •Preventing template bloat
- •Explicit instantiation
- •Explicit specification of template functions
- •Controlling template instantiation
- •Template programming idioms
- •Summary
- •Containers and iterators
- •STL reference documentation
- •The Standard Template Library
- •The basic concepts
- •Containers of strings
- •Inheriting from STL containers
- •A plethora of iterators
- •Iterators in reversible containers
- •Iterator categories
- •Input: read-only, one pass
- •Output: write-only, one pass
- •Forward: multiple read/write
- •Bidirectional: operator--
- •Random-access: like a pointer
- •Is this really important?
- •Predefined iterators
- •IO stream iterators
- •Manipulating raw storage
- •Basic sequences: vector, list & deque
- •Basic sequence operations
- •vector
- •Cost of overflowing allocated storage
- •Inserting and erasing elements
- •deque
- •Converting between sequences
- •Cost of overflowing allocated storage
- •Checked random-access
- •list
- •Special list operations
- •list vs. set
- •Swapping all basic sequences
- •Robustness of lists
- •Performance comparison
- •A completely reusable tokenizer
- •stack
- •queue
- •Priority queues
- •Holding bits
- •bitset<n>
- •vector<bool>
- •Associative containers
- •Generators and fillers for associative containers
- •The magic of maps
- •A command-line argument tool
- •Multimaps and duplicate keys
- •Multisets
- •Combining STL containers
- •Creating your own containers
- •Summary
- •Exercises
- •5: STL Algorithms
- •Function objects
- •Classification of function objects
- •Automatic creation of function objects
- •Binders
- •Function pointer adapters
- •SGI extensions
- •A catalog of STL algorithms
- •Support tools for example creation
- •Filling & generating
- •Example
- •Counting
- •Example
- •Manipulating sequences
- •Example
- •Searching & replacing
- •Example
- •Comparing ranges
- •Example
- •Removing elements
- •Example
- •Sorting and operations on sorted ranges
- •Sorting
- •Example
- •Locating elements in sorted ranges
- •Example
- •Merging sorted ranges
- •Example
- •Set operations on sorted ranges
- •Example
- •Heap operations
- •Applying an operation to each element in a range
- •Examples
- •Numeric algorithms
- •Example
- •General utilities
- •Creating your own STL-style algorithms
- •Summary
- •Exercises
- •Perspective
- •Duplicate subobjects
- •Ambiguous upcasting
- •virtual base classes
- •The "most derived" class and virtual base initialization
- •"Tying off" virtual bases with a default constructor
- •Overhead
- •Upcasting
- •Persistence
- •MI-based persistence
- •Improved persistence
- •Avoiding MI
- •Mixin types
- •Repairing an interface
- •Summary
- •Exercises
- •7: Exception handling
- •Error handling in C
- •Throwing an exception
- •Catching an exception
- •The try block
- •Exception handlers
- •Termination vs. resumption
- •The exception specification
- •Better exception specifications?
- •Catching any exception
- •Rethrowing an exception
- •Uncaught exceptions
- •Function-level try blocks
- •Cleaning up
- •Constructors
- •Making everything an object
- •Exception matching
- •Standard exceptions
- •Programming with exceptions
- •When to avoid exceptions
- •Not for asynchronous events
- •Not for ordinary error conditions
- •Not for flow-of-control
- •You’re not forced to use exceptions
- •New exceptions, old code
- •Typical uses of exceptions
- •Always use exception specifications
- •Start with standard exceptions
- •Nest your own exceptions
- •Use exception hierarchies
- •Multiple inheritance
- •Catch by reference, not by value
- •Throw exceptions in constructors
- •Don’t cause exceptions in destructors
- •Avoid naked pointers
- •Overhead
- •Summary
- •Exercises
- •8: Run-time type identification
- •The “Shape” example
- •What is RTTI?
- •Two syntaxes for RTTI
- •Syntax specifics
- •Producing the proper type name
- •Nonpolymorphic types
- •Casting to intermediate levels
- •void pointers
- •Using RTTI with templates
- •References
- •Exceptions
- •Multiple inheritance
- •Sensible uses for RTTI
- •Revisiting the trash recycler
- •Mechanism & overhead of RTTI
- •Creating your own RTTI
- •Explicit cast syntax
- •Summary
- •Exercises
- •9: Building stable systems
- •Shared objects & reference counting
- •Reference-counted class hierarchies
- •Finding memory leaks
- •An extended canonical form
- •Exercises
- •10: Design patterns
- •The pattern concept
- •The singleton
- •Variations on singleton
- •Classifying patterns
- •Features, idioms, patterns
- •Basic complexity hiding
- •Factories: encapsulating object creation
- •Polymorphic factories
- •Abstract factories
- •Virtual constructors
- •Destructor operation
- •Callbacks
- •Observer
- •The “interface” idiom
- •The “inner class” idiom
- •The observer example
- •Multiple dispatching
- •Visitor, a type of multiple dispatching
- •Efficiency
- •Flyweight
- •The composite
- •Evolving a design: the trash recycler
- •Improving the design
- •“Make more objects”
- •A pattern for prototyping creation
- •Trash subclasses
- •Parsing Trash from an external file
- •Recycling with prototyping
- •Abstracting usage
- •Applying double dispatching
- •Implementing the double dispatch
- •Applying the visitor pattern
- •More coupling?
- •RTTI considered harmful?
- •Summary
- •Exercises
- •11: Tools & topics
- •The code extractor
- •Debugging
- •Trace macros
- •Trace file
- •Abstract base class for debugging
- •Tracking new/delete & malloc/free
- •CGI programming in C++
- •Encoding data for CGI
- •The CGI parser
- •Testing the CGI parser
- •Using POST
- •Handling mailing lists
- •Maintaining your list
- •Mailing to your list
- •A general information-extraction CGI program
- •Parsing the data files
- •Summary
- •Exercises
- •General C++
- •My own list of books
- •Depth & dark corners
- •Design Patterns
- •Index
T << endl << f << endl;
2300114.50000000020000000000 T.setf(ios::scientific, ios::floatfield); T << endl << f << endl;
2.30011450000000020000e+06 T.setf(ios::fixed, ios::floatfield); T << f << endl; 2300114.50000000020000000000 T.setf(0, ios::floatfield);
T << f << endl; 2300114.50000000020000000000 T.width(10);
Is there any more? T.width(40);
0000000000000000000000Is there any more? T.setf(ios::left, ios::adjustfield); T.width(40);
Is there any more?0000000000000000000000 T.unsetf(ios::showpoint); T.unsetf(ios::unitbuf); T.unsetf(ios::stdio);
Studying this output should clarify your understanding of the iostream formatting member functions.
Formatting manipulators
As you can see from the previous example, calling the member functions can get a bit tedious. To make things easier to read and write, a set of manipulators is supplied to duplicate the actions provided by the member functions.
Manipulators with no arguments are provided in <iostream>. These include dec, oct, and hex , which perform the same action as, respectively, setf(ios::dec, ios::basefield), setf(ios::oct, ios::basefield), and setf(ios::hex, ios::basefield), albeit more succinctly.
<iostream>8 also includes ws, endl, ends, and flush and the additional set shown here:
8 These only appear in the revised library; you won’t find them in older implementations of iostreams.
Chapter 14: Templates & Container Classes
95
manipulator |
effect |
|
|
showbase |
Indicate the numeric base (dec, |
noshowbase |
oct, or hex) when printing an |
|
integral value. The format used |
|
can be read by the C++ |
|
compiler. |
|
|
showpos |
Show plus sign (+) for positive |
noshowpos |
values |
|
|
uppercase |
Display uppercase A-F for |
nouppercase |
hexadecimal values, and E for |
|
scientific values |
|
|
showpoint |
Show decimal point and trailing |
noshowpoint |
zeros for floating-point values. |
|
|
skipws |
Skip white space on input. |
noskipws |
|
|
|
left |
Left-align, pad on right. |
right |
Right-align, pad on left. |
internal |
Fill between leading sign or base |
|
indicator and value. |
|
|
scientific |
Use scientific notation |
fixed |
setprecision( ) or |
|
ios::precision( ) sets number of |
|
places after the decimal point. |
|
|
Manipulators with arguments
If you are using manipulators with arguments, you must also include the header file <iomanip>. This contains code to solve the general problem of creating manipulators with arguments. In addition, it has six predefined manipulators:
manipulator effect
Chapter 14: Templates & Container Classes
96
manipulator |
effect |
|
|
setiosflags (fmtflags n) |
Sets only the format flags |
|
specified by n. Setting remains |
|
in effect until the next change, |
|
like ios::setf( ). |
|
|
resetiosflags(fmtflags n) |
Clears only the format flags |
|
specified by n. Setting remains |
|
in effect until the next change, |
|
like ios::unsetf( ). |
|
|
setbase(base n) |
Changes base to n, where n is |
|
10, 8, or 16. (Anything else |
|
results in 0.) If n is zero, output |
|
is base 10, but input uses the C |
|
conventions: 10 is 10, 010 is 8, |
|
and 0xf is 15. You might as well |
|
use dec, oct, and hex for output. |
|
|
setfill(char n) |
Changes the fill character to n, |
|
like ios::fill( ). |
|
|
setprecision(int n) |
Changes the precision to n, like |
|
ios::precision( ). |
|
|
setw(int n) |
Changes the field width to n, |
|
like ios::width( ). |
|
|
If you’re using a lot of inserters, you can see how this can clean things up. As an example, here’s the previous program rewritten to use the manipulators. (The macro has been removed to make it easier to read.)
//: C02:Manips.cpp
// Format.cpp using manipulators #include <fstream>
#include <iomanip> using namespace std;
int main() {
ofstream trc("trace.out"); int i = 47;
float f = 2300114.414159;
Chapter 14: Templates & Container Classes
97
char* s = "Is there any more?";
trc << setiosflags(
ios::unitbuf /*| ios::stdio */ /// ?????
| ios::showbase | ios::uppercase | ios::showpos);
trc << i << endl; // Default to dec trc << hex << i << endl;
trc << resetiosflags(ios::uppercase)
<<oct << i << endl; trc.setf(ios::left, ios::adjustfield); trc << resetiosflags(ios::showbase)
<<dec << setfill('0');
trc << "fill char: " << trc.fill() << endl; trc << setw(10) << i << endl; trc.setf(ios::right, ios::adjustfield);
trc << setw(10) << i << endl; trc.setf(ios::internal, ios::adjustfield); trc << setw(10) << i << endl;
trc << i << endl; // Without setw(10)
trc << resetiosflags(ios::showpos)
<<setiosflags(ios::showpoint)
<<"prec = " << trc.precision() << endl; trc.setf(ios::scientific, ios::floatfield); trc << f << endl;
trc.setf(ios::fixed, ios::floatfield); trc << f << endl;
trc.setf(0, ios::floatfield); // Automatic trc << f << endl;
trc << setprecision(20);
trc << "prec = " << trc.precision() << endl; trc << f << endl;
trc.setf(ios::scientific, ios::floatfield); trc << f << endl;
trc.setf(ios::fixed, ios::floatfield); trc << f << endl;
trc.setf(0, ios::floatfield); // Automatic trc << f << endl;
trc << setw(10) << s << endl; trc << setw(40) << s << endl;
trc.setf(ios::left, ios::adjustfield);
Chapter 14: Templates & Container Classes
98