|
|
#include <iostream.h> #include <iomanip.h>IOMANIPdeclare(T) ;
class SMANIP(T) { SMANIP(T)( ios& (*)(ios&,T), T); friend istream& operator>>(istream&, SMANIP(T)&); friend ostream& operator<<(ostream&, SMANIP(T)&); }; class SAPP(T) { SAPP(T)( ios& (*)(ios&,T)); SMANIP(T) operator()(T); }; class IMANIP(T) { IMANIP(T)( istream& (*)(istream&,T), T); friend istream& operator>>(istream&, IMANIP(T)&); }; class IAPP(T) { IAPP(T)( istream& (*)(istream&,T)); IMANIP(T) operator()(T); }; class OMANIP(T) { OMANIP(T)( ostream& (*)(ostream&,T), T); friend ostream& operator<<(ostream&, OMANIP(T)&); }; class OAPP(T) { OAPP(T)( ostream& (*)(ostream&,T)); OMANIP(T) operator()(T); }; class IOMANIP(T) { IOMANIP(T)( iostream& (*)(iostream&,T), T); friend istream& operator>>(iostream&, IOMANIP(T)&); friend ostream& operator<<(iostream&, IOMANIP(T)&); }; class IOAPP(T) { IOAPP(T)( iostream& (*)(iostream&,T)); IOMANIP(T) operator()(T); };
IOMANIPdeclare(int); IOMANIPdeclare(long);SMANIP(long) resetiosflags(long); SMANIP(int) setfill(int); SMANIP(long) setiosflags(long); SMANIP(int) setprecision(int); SMANIP(int) setw(int w);
ostream
s,
flush
, can be used as follows:
cout << flushto flush
cout
.
Several iostream classes supply manipulators:
see
ios(C++),
istream(C++),
and
ostream(C++).
flush
is a simple manipulator;
some manipulators take arguments, such as the predefined
ios
manipulators, setfill
and setw
(see below).
The header file iomanip.h supplies macro definitions
which programmers can use to define new parameterized manipulators.
Ideally, the types relating to manipulators would be parameterized
as ``templates.''
The macros defined in iomanip.h are used to simulate templates.
IOMANIPdeclare(T)
declares
the various classes and operators.
(All code is declared inline so that no separate definitions are required.)
Each of the other T
s are used to construct the
real names and therefore must be a single identifier.
Each of the other macros also requires an identifier
and expands to a name.
In the following descriptions, assume:
T
, or type name
ios
istream
ostream
iostream
ios& (*)(ios&)
istream& (*)(istream&)
ostream& (*)(ostream&)
iostream& (*)(iostream&)
int
long
<<SMANIP(T)(
f,
t)
>>SMANIP(T)(
f,
t)
<<SAPP(T)(
f)(
t)
>>SAPP(T)(
f)(
t)
>>IMANIP(T)(
if,
t)
>>IAPP(T)(
if)(
t)
<<OMANIP(T)(
of,
t)
<<OAPP(T)(
of)(
t)
<<IOMANIP(T)(
iof,
t)
>>IOMANIP(T)(
iof,
t)
<<IOAPP(T)(
iof)(
t)
>>IOAPP(T)(
iof)(
t)
iomanip.h contains two declarations, IOMANIPdeclare(int)
and IOMANIPdeclare(long)
and some manipulators that take an int
or a long
argument.
These manipulators all have to do with changing the format state
of a stream; see
ios(C++)
for further details.
<<setw(
n)
>>setw(
n)
<<setfill(
n)
>>setfill(
n)
<<setprecision(
n)
>>setprecision(
n)
<<setiosflags(
l)
>>setiosflags(
l)
o.setf(
l)
or i.setf(
l)
.)
<<resetiosflags(
l)
>>resetiosflags(
l)
o.setf(0,
l)
or i.setf(0,
l)
.)
IOMANIPdeclare(T)
occurs more than once in a file with the
same T
.