This section describes how manipulators are used with iostream. #include <iostream.h> IOMANIPdeclare(T); class SMANIP(T) SMANIP(T)(ios& (*)(ios&,T), T); }; class SAPP(T) SAPP(T)(ios& (*)(ios&,T)); }; class IMANIP(T) IMANIP(T)(istream& (*)(istream&,T),T); }; class IAPP(T) IAPP(T)(istream& (*)(istream&,T)); }; class OMANIP(T) OMANIP(T)(ostream& (*)(ostream&,T),T); }; class OAPP(T) OAPP(T)(ostream& (*)(ostream&,T)); }; class IOMANIP(T) IOMANIP(T)(iostream& (*)(iostream&,T),T); }; class IOAPP(T) IOAPP(T)(iostream& (*)(iostream&,T)); }; IOMANIPdeclare(int); SMANIP(int) | |
Manipulators are values that may be "inserted into" or "extracted from" streams to
to flush cout. Several iostream classes supply manipulators, see ios, istream and ostream. 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." Themacros 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 Ts is 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:
s<<SMANIP(T)( (ios& (*)(ios&, T)) f, T t) Returns f(s,t), where s is the left operand of the insertion or extractor operator (i.e. s,i, o or io). i>>IMANIP(T)( (istream& (*)(istream&, T)) isf, T t) Returns isf(i,t). o<<OMANIP(T)( (ostream& (*)(ostream&, T)) osf,T t) Returns osf(o,t). io<<IOMANIP(T)( (iostream& (*)(iostream&, T)) iof,T t) Returns iof(io,t). <iomanip.h> contains some additional 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 for further details. o<<setbase(int n) Sets the conversion base format flag to be n. o<<resetiosflags(long l) The format bits specified by l are flushed in the stream (o or i) (thus calling o<<setfill(int n) Sets the fill character of the stream (o or i) to be n. o<<setiosflags(long l) Turns on in the stream (o or i) the format flags marked in l. (Calls o.setf(l) or o<<setprecision(int n) Sets the precision of the stream (o or i) to be n. o<<setw(int n) Sets the field width of the stream (left-hand operand: o or i) to n. | |
EXAMPLE | The following program shows the use of manipulators (like setw) which globally alter output by changing private data members in cout: #include <iostream.h> #include <iomanip.h> #include <string.h> void testline(const char * const p) { /* Put the parameter string onto cout and set the field width */ /* of the cout stream to be twice the length of the string */ int N = 2 * strlen(p); cout << setw(N) ; cout << p; } void someoutput(const char * const p, const char * const q) { /* Given a string p and a string q containing a list of fill */ /* characters; display the string p in a variety of fill */ /* character contexts */ int i; int M = strlen(q); for (i = 0; i < M; ++i) { cout << setfill(q[i]); testline(p); } } int main() { someoutput( "A Test String\n", ".,!$%&*()"); /* Note how the output is right justified for text strings */ return 0; } Note how <string.h> must be included to get the function prototype for strlen(), and <iomanip.h> must be included to get the prototypes for setw() and setfill(). The result of executing the program is: .....................A Test String ,,,,,,,,,,,,,,,,,,,,,A Test String !!!!!!!!!!!!!!!!!!!!!A Test String $$$$$$$$$$$$$$$$$$$$$A Test String %%%%%%%%%%%%%%%%%%%%%A Test String &&&&&&&&&&&&&&&&&&&&&A Test String *********************A Test String (((((((((((((((((((((A Test String )))))))))))))))))))))A Test String % CCM0998 CPU time used: 0.0006 seconds |
SEE ALSO | |