The iomanip library

The iomanip library defines a collection of I/O stream manipulators to modify the behavior of insertions The iomanip manipulators are defined in the standard header file iomanip. It is common practice for C++ programs to include the iomanip header file along with the iostream header file.

#include <iostream>
#include <iomanip>

The following table lists some of the manipulators provided by iomanip except for manipulator setw(), all the manipulators are persistent.

Output Manipulators Purpose
setw(int w) Set field width to W
setfill (int c) Set the fill character to C
left Fill characters are padded after the display
right Fill characters are padded before the display
setbase (int b) Set the numeric base to b
fixed Display floating-point values in scientific notation
scientific Display floating-point values in scientific notation.
setprecision (int d) Set number of places of accuracy to d
showbase Octal displays are preceded with 0, and hexadecimal displays are preceded with 0x.
noshowbase Numbers are not displayed with a base indicator.
boolalpha Display logical values symbolically as true and false.
noboolalpha Display logical values as 0 and 1

Manipulator setw() sets the desired width of an insertion. The setw() manipulator is not persistent. The width specified to the manipulator affects only the next insertion. After that next insertion, default behavior is again used.

Manipulator setfill() sets the fill character to be displayed when the width of an insertion request is larger than necessary. The following statement demonstrates the use of this manipulator:

cout << setfill (‘#’) << setw(15) << “Hello” << endl;

In this insertion statement, the string “Hello” is right adjusted through the display of the octothrop character ‘#’.

#############Hello

Manipulators left and right control whether an insertion is displayed in a left-or right-adjusted manner. Consider the following examples, which insert three names to standard output. Each of the names are displayed with a field width of 6.

cout << “:” << left << setw(6) << “JJ” << “:” << endl;
cout << “:” << right << setw(6) << “Jenna ” << “:” << endl;
cout << “:” << left << setw(6) << “Hannah” << “:” <<endl;

It produces the following as its output:
:JJ:
: Jenna:
: Hannah:

Manipulator setbase() is for output streams and requires a single parameter. The parameter specifies the base to be used in the display of numeric data (decimal, octal, or hexadecimal). The manipulator is not generally used because iostream manipulators dec, oct, and hex are also available. One typically uses setbase() only when the desired base for inserting a value is the result of an earlier computation or input request.

Manipulators fixed and scientific specify, respectively, whether floating-point values should be displayed in decimal or scientific notation. By default, floating-point values are displayed in automatic mode. When automatic mode is in effect, large values, small values, and values close to zero are displayed in scientific notation while other values are displayed in decimal notation. Manipulator setprecision() sets the number of digits of precision when displaying a floating-point value. For automatic mode, the precision value is the preferred number of digits to be displayed. For fixed and scientific modes, the precision value is the number of places after the decimal.

Persistent manipulators boolalpha and noboolalpha specify how bool objects are to be displayed (the default for bool objects is to display them in binary notation). For example, the code segment

cout << true << endl;
cout << false << endl;

produce as its output

1
0

If string notation is preferred over binary notation, use the manipulator bool alpha. For example, the code segment

cout << boolalplia << true << endl;
cout << false << endl;

produces as its output

true
false

The binary notation for bool objects can be restored using the manipulator noboolalpha. The persistent manipulators showbase and noshowbase specify whether octal and hexadecimal numbers are displayed with identifying prefixes (the default is no identifying prefix). If manipulator showbase is used, then octal numbers are displayed with a leading 0 and hexadecimal numbers are displayed with a leading 0x. The use of identifying prefixes for subsequent insertions can be turned off with the manipulator noshowbase.

It's very calm over here, why not leave a comment?

Leave a Reply

You must be logged in to post a comment.