Show Lecture.Manipulators as a slide show.
CS253 Manipulators
I/O Manipulators
As you recall, there are a number of I/O manipulators:
[no]boolalpha
cout << false << ' ' << true << '\n'
<< boolalpha << false << ' ' << true << '\n'
<< noboolalpha << false << ' ' << true << '\n';
0 1
false true
0 1
- For boolean output, write
true
or false
instead of 1
or 0
.
[no]showbase
cout << dec << 65 << ' ' << hex << 65 << ' ' << oct << 65 << '\n'
<< showbase << dec << 65 << ' ' << hex << 65 << ' ' << oct << 65 << '\n'
<< noshowbase << dec << 65 << ' ' << hex << 65 << ' ' << oct << 65 << '\n';
65 41 101
65 0x41 0101
65 41 101
- Label integer output, using the same conventions as integer constants
in a C++ program:
- Prefix hex integer output with
0x
.
- Ensure that octal integer output starts with a
0
.
- There is no binary output.
[no]showpoint
cout << 123.0 << ' ' << 456.789 << '\n'
<< showpoint << 123.0 << ' ' << 456.789 << '\n'
<< noshowpoint << 123.0 << ' ' << 456.789 << '\n';
123 456.789
123.000 456.789
123 456.789
- Floating-point numbers get a decimal point, whether or not
they’re whole numbers.
[no]showpos
cout << 123 << ' ' << 0 << ' ' << -456 << '\n'
<< showpos << 123 << ' ' << 0 << ' ' << -456 << '\n'
<< noshowpos << 123 << ' ' << 0 << ' ' << -456 << '\n';
123 0 -456
+123 +0 -456
123 0 -456
- Non-negative numbers gets a plus sign.
[no]skipws
istringstream iss;
int a;
iss.str("\n\r\v\f 123 ");
iss >> a;
cout << (iss?"good":"bad") << ' ' << a << '\n';
iss.str("\n\r\v\f 456 ");
iss >> skipws >> a;
cout << (iss?"good":"bad") << ' ' << a << '\n';
iss.str("\n\r\v\f 789 ");
iss >> noskipws >> a;
cout << (iss?"good":"bad") << ' ' << a << '\n';
good 123
good 456
bad 0
- Whitespace is normally skipped before formatted input (
<<
).
[no]uppercase
cout << hex << 64206 << ' ' << 1e99 << " end\n"
<< uppercase << hex << 64206 << ' ' << 1e99 << " end\n"
<< nouppercase << hex << 64206 << ' ' << 1e99 << " end\n";
face 1e+99 end
FACE 1E+99 end
face 1e+99 end
- Use UPPERCASE letters in hex output, and in floating-point exponents.
- Strings and characters are unchanged.
left
/ right
/ internal
cout << 123 << "★\n"
<< setw(10) << left << 123 << "★\n"
<< setw(10) << right << 123 << "★\n"
<< setw(10) << internal << 123 << "★\n";
123★
123 ★
123★
123★
cout << -123 << "★\n"
<< setw(10) << left << -123 << "★\n"
<< setw(10) << right << -123 << "★\n"
<< setw(10) << internal << -123 << "★\n";
-123★
-123 ★
-123★
- 123★
cout << -123 << "★\n"
<< setw(10) << setfill('x') << left << -123 << "★\n"
<< setw(10) << setfill('x') << right << -123 << "★\n"
<< setw(10) << setfill('x') << internal << -123 << "★\n";
-123★
-123xxxxxx★
xxxxxx-123★
-xxxxxx123★
- Alignment for numbers and strings.
There is no
center
.
dec
/ oct
/ hex
cout << 10 << ' ' << 30 << ' ' << 50 << '\n'
<< dec << 10 << ' ' << 30 << ' ' << 50 << '\n'
<< oct << 10 << ' ' << 30 << ' ' << 50 << '\n'
<< hex << 10 << ' ' << 30 << ' ' << 50 << '\n';
10 30 50
10 30 50
12 36 62
a 1e 32
- Output in base 10, base 8, and base 16.
cout << bin << 10;
c.cc:1: error: 'bin' was not declared in this scope
- There is no base 2 output.
fixed
/ scientific
/ hexfloat
/ defaultfloat
cout << 16e5 << ' ' << 3.14159 << ' ' << 4.5e-12 << '\n'
<< fixed << 16e5 << ' ' << 3.14159 << ' ' << 4.5e-12 << '\n'
<< scientific << 16e5 << ' ' << 3.14159 << ' ' << 4.5e-12 << '\n'
<< hexfloat << 16e5 << ' ' << 3.14159 << ' ' << 4.5e-12 << '\n'
<< defaultfloat << 16e5 << ' ' << 3.14159 << ' ' << 4.5e-12 << '\n';
1.6e+06 3.14159 4.5e-12
1600000.000000 3.141590 0.000000
1.600000e+06 3.141590e+00 4.500000e-12
0x1.86ap+20 0x1.921f9f01b866ep+1 0x1.3ca8cb153a753p-38
1.6e+06 3.14159 4.5e-12
- Fixed-point notation: no exponent
- Scientific notation: digit
.
digits e
exponent
- Hexadecimal: the bits that represent the number
- Default: choose based on which one makes sense.
setfill
cout << 123 << '\n'
<< setw(10) << 456 << '\n'
<< setfill('*') << setw(10) << 789 << '\n';
123
456
*******789
- Set the fill (padding) character, which defaults to a space.
- Only relevant if
setw()
is used.
setprecision
http://www.cplusplus.com/reference/ios/ios_base/precision/ says:
- Using the default floating-point notation, the precision field specifies the maximum number of meaningful digits to display in total counting both those before and those after the decimal point. Notice that it is not a minimum, and therefore it does not pad the displayed number with trailing zeros if the number can be displayed with less digits than the precision.
- In both the fixed and scientific notations, the precision field specifies exactly how many digits to display after the decimal point, even if this includes trailing decimal zeros. The digits before the decimal point are not relevant for the precision in this case.
setprecision
cout << 123.45 << '\n'
<< setprecision(1) << 123.45 << '\n'
<< setprecision(9) << 123.45 << '\n';
123.45
1e+02
123.45
cout << fixed
<< 123.45 << '\n'
<< setprecision(1) << 123.45 << '\n'
<< setprecision(9) << 123.45 << '\n';
123.450000
123.5
123.450000000
cout << scientific
<< 123.45 << '\n'
<< setprecision(1) << 123.45 << '\n'
<< setprecision(9) << 123.45 << '\n';
1.234500e+02
1.2e+02
1.234500000e+02
setw
cout << setw(5) << 1 << 2 << 3 << 4 << '\n';
1234
cout << setw(9) << 6.7 << '\n'
<< setw(9) << 'x' << '\n'
<< setw(9) << "foo" << '\n';
6.7
x
foo
cout << setfill('*') << setw(4) << 8.9 << '\n'
<< setw(4) << 'y' << '\n'
<< setw(4) << "bar" << '\n'
<< setw(4) << "alpha" << '\n';
*8.9
***y
*bar
alpha
setw
is not sticky—it is reset upon formatted output.
- It affects more than just numbers.
- It pads with the fill character.
Defining Your Own
It’s easy to define your own I/O manipulators:
ostream &dog(ostream &os) {
return os << "Kokopelli";
}
int main() {
cout << "My dog is " << dog << ".\n";
return 0;
}
My dog is Kokopelli.
Of course, if that’s all you want:
const string dog = "Kokopelli";
cout << "My dog is " << dog << ".\n";
My dog is Kokopelli.
Better Example
ostream &bucks(ostream &os) {
return os << fixed << setprecision(2);
}
int main() {
cout << 34.5 << ' ' << bucks << 78.9 << '\n';
return 0;
}
34.5 78.90
- This sets output to be two digits after the decimal point,
as is traditional for money in the USA.