Thinking in C++ Vol 2 - Practical Programming |
Prev |
Home |
Next |
The second type of formatting flags work in a group. Only
one of these flags can be set at a time, like the buttons on old car radios you
push one in, the rest pop out. Unfortunately this doesn t happen automatically,
and you must pay attention to what flags you re setting so that you don t
accidentally call the wrong setf( ) function. For example, there s
a flag for each of the number bases: hexadecimal, decimal, and octal.
Collectively, these flags are referred to as the ios::basefield. If the ios::dec flag is set and you call setf(ios::hex), you ll set
the ios::hex flag, but you won t clear the ios::dec bit,
resulting in undefined behavior. Instead, call the second form of setf( )
like this: setf(ios::hex, ios::basefield). This function first clears
all the bits in the ios::basefield and then sets ios::hex.
Thus, this form of setf( ) ensures that the other flags in the
group pop out whenever you set one. The ios::hex manipulator does all
this for you, automatically, so you don t need to concern yourself with the
internal details of the implementation of this class or to even care
that it s a set of binary flags. Later you ll see that there are manipulators
to provide equivalent functionality in all the places you would use setf( ).
Here are the flag groups and their effects:
ios::basefield
|
Effect
|
ios::dec
|
Format integral values in base 10 (decimal) (the default
radix no prefix is visible).
|
ios::hex
|
Format integral values in base 16 (hexadecimal).
|
ios::oct
|
Format integral values in base 8 (octal).
|
ios::floatfield
|
Effect
|
ios::scientific
|
Display floating-point numbers in scientific format.
Precision field indicates number of digits after the decimal point.
|
ios::fixed
|
Display floating-point numbers in fixed format. Precision
field indicates number of digits after the decimal point.
|
automatic (Neither bit is set.)
|
Precision field indicates the total number of significant
digits.
|
ios::adjustfield
|
Effect
|
ios::left
|
Left-align values; pad on the right with the fill character.
|
ios::right
|
Right-align values. Pad on the left with the fill character.
This is the default alignment.
|
ios::internal
|
Add fill characters after any leading sign or base indicator,
but before the value. (In other words, the sign, if printed, is
left-justified while the number is right-justified.)
|
Thinking in C++ Vol 2 - Practical Programming |
Prev |
Home |
Next |