Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

manip iostream manipulation

&pagelevel(3)&pagelevel


This section describes how manipulators are used with iostream.


#include <iostream.h>
#include <iomanip.h>

IOMANIPdeclare(T);

class SMANIP(T)
{
public:

SMANIP(T)(ios& (*)(ios&,T), T);
friend istream& operator>>(istream&, const SMANIP(T)&);
friend ostream& operator<<(ostream&, const SMANIP(T)&);

};

class SAPP(T)
{
public:

SAPP(T)(ios& (*)(ios&,T));
SMANIP(T) operator()(T);

};

class IMANIP(T)
{
public:

IMANIP(T)(istream& (*)(istream&,T),T);
friend istream& operator>>(istream&, const IMANIP(T)&);

};

class IAPP(T)
{
public:

IAPP(T)(istream& (*)(istream&,T));
IMANIP(T) operator()(T);

};

class OMANIP(T)
{
public:

OMANIP(T)(ostream& (*)(ostream&,T),T);
friend ostream& operator<<(ostream&, const OMANIP(T)&);

};

class OAPP(T)
{
public:

OAPP(T)(ostream& (*)(ostream&,T));
OMANIP(T) operator()(T);

};

class IOMANIP(T)
{
public:

IOMANIP(T)(iostream& (*)(iostream&,T),T);
friend istream& operator>>(iostream&, const IOMANIP(T)&);
friend ostream& operator<<(iostream&, const IOMANIP(T)&);

};

class IOAPP(T)
{
public:

IOAPP(T)(iostream& (*)(iostream&,T));
IOMANIP(T) operator()(T);

};

IOMANIPdeclare(int);
IOMANIPdeclare(long);

SMANIP(int)   setbase(int);
SMANIP(long)  resetiosflags(long);
SMANIP(int)   setfill(int);
SMANIP(long)  setiosflags(long);
SMANIP(int)   setprecision(int);
SMANIP(int)   setw(int w);



Manipulators are values that may be "inserted into" or "extracted from" streams to
achieve some effect (other than to insert or extract values), with a convenient syntax.They enable you to embed a function call in an expression containing series of
insertions or extractions. For example, the predefined manipulator for ostreams, flush,can be used as follows:

cout << flush

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:

  • t is a T, or type name.
  • s is an ios.
  • i is an istream.
  • o is an ostream.
  • io is an iostream.
  • f is an ios& (*)(ios&, T).
  • isf is an istream& (*)(istream&, T).
  • osf is an ostream& (*)(ostream&, T).
  • iof is an iostream& (*)(iostream&, T).
  • n is an int.
  • l is a long.

s<<SMANIP(T)( (ios& (*)(ios&, T)) f, T t)
s>>SMANIP(T)( (ios& (*)(ios&, T)) f, T t)
s<<SAPP(T)( (ios& (*)(ios&, T)) f)(T t)
s>>SAPP(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)
i>>IAPP(T)( (istream& (*)(istream&, T)) isf) (T t)

Returns isf(i,t).

o<<OMANIP(T)( (ostream& (*)(ostream&, T)) osf,T t)
o<<OAPP(T)( (ostream& (*)(ostream&, T)) osf) (T t)

Returns osf(o,t).

io<<IOMANIP(T)( (iostream& (*)(iostream&, T)) iof,T t)
io>>IOMANIP(T)( (iostream& (*)(iostream&, T)) iof,T t)
io<<IOAPP(T)( (iostream& (*)(iostream&, T)) iof) (T t)
io>>IOAPP(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)
i>>setbase(int n)

Sets the conversion base format flag to be n.

o<<resetiosflags(long l)
i>>resetiosflags(long l)

The format bits specified by l are flushed in the stream (o or i) (thus calling
o.setf(0, l) or i.setf(0, l)).

o<<setfill(int n)
i>>setfill(int n)

Sets the fill character of the stream (o or i) to be n.

o<<setiosflags(long l)
i>>setiosflags(long l)

Turns on in the stream (o or i) the format flags marked in l. (Calls o.setf(l) or
i.setf(l)).

o<<setprecision(int n)
i>>setprecision(int n)

Sets the precision of the stream (o or i) to be n.

o<<setw(int n)
i>>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


ios, istream, ostream