The following example shows how to write a manipulator, fill
, to insert a specific number of a particular character. The manipulator, which takes two arguments, is similar to setpic
in the previous example. The difference is that the character pointer type declaration is replaced by a structure declaration.
#include <iostream.h>
#include <iomanip.h>
struct fillpair {
char ch;
int cch;
};
IOMANIPdeclare( fillpair );
ostream& fp( ostream& os, fillpair pair )
{
for ( int c = 0; c < pair.cch; c++ ) {
os << pair.ch;
}
return os;
}
OMANIP(fillpair) fill( char ch, int cch )
{
fillpair pair;
pair.cch = cch;
pair.ch = ch;
return OMANIP (fillpair)( fp, pair );
}
void main()
{
cout << "10 dots coming" << fill( '.', 10 ) << "done" << endl;
}
This example can be rewritten so that the manipulator definition is in a separate program file. In this case, the header file must contain these declarations:
struct fillpair {
char ch;
int cch;
};
IOMANIPdeclare( fillpair );
ostream& fp( ostream& o, fillpair pair );
OMANIP(fillpair) fill( char ch, int cch );