13.7.3. Padding (fill, setfill)

The fill member function specifies the fill character to be used with justified fields; spaces are used for padding by default. The function returns the prior padding character. The setfill manipulator also sets the padding character. Figure 13.16 demonstrates function fill (line 30) and stream manipulator setfill (lines 34 and 37) to set the fill character.


 1   // Fig. 13.16: fig13_16.cpp
 2   // Using member function fill and stream manipulator setfill to change
 3   // the padding character for fields larger than the printed value.
 4   #include <iostream>
 5   #include <iomanip>
 6   using namespace std;
 7
 8   int main()
 9   {
10      int x = 10000;
11
12      // display x
13      cout << x << " printed as int right and left justified "
14         << "and as hex with internal justification. "
15         << "Using the default pad character (space):" << endl;
16
17      // display x with base
18      cout << showbase << setw( 10 ) << x << endl;
19
20      // display x with left justification
21      cout << left << setw( 10 ) << x << endl;
22
23      // display x as hex with internal justification
24      cout << internal << setw( 10 ) << hex << x << endl << endl;
25
26      cout << "Using various padding characters:" << endl;
27
28      // display x using padded characters (right justification)
29      cout << right;
30      cout.fill( '*' );
31      cout << setw( 10 ) << dec << x << endl;
32
33      // display x using padded characters (left justification)
34      cout << left << setw( 10 ) << setfill( '%' ) << x << endl;
35
36      // display x using padded characters (internal justification)
37      cout << internal << setw( 10 ) << setfill( '^' ) << hex
38         << x << endl;
39   } // end main


10000 printed as int right and left justified
and as hex with internal justification.
Using the default pad character (space):
     10000
10000
0x    2710

Using various padding characters:
*****10000
10000%%%%%
0x^^^^2710


Fig. 13.16. Using member function fill and stream manipulator setfill to change the padding character for fields larger than the printed values.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.188.121