Hello again! In this video, we are going to look at stream manipulators and formatting.
A manipulator is something that gets pushed onto a stream to affect the stream's behaviour.
We have already met the flush and endl manipulators.
These affect these stream's behaviour, by causing it to flush the buffer immediately, instead of waiting
until the buffer is full.
The C++ library defines a number of manipulators for streams, and these alter the way that the stream
formats the output.
Most of these are in the iostream header, but there are some which are in iomanip, and that is for manipulators
which take an argument.
If you have tried to print out a Boolean, you will have noticed that it prints out the number 1 or
0, and not the words "true" or "false".
There is a boolalpha manipulator.
And if we push that on the stream, then the stream will
actually start printing out the words "true" or "false".
And then that will change the state of the stream.
So any Boolean expression you give to it will be printed out as true or false.
So let's try this out.
So first of all, we are doing it the traditional way, without doing anything special.
We have a Boolean expression.
And then we print it out again, using boolalpha. And then we try printing out something, else just to
show whether the stream still does it.
And the answer is that it does. This will affect the behaviour of the stream.
And if you want to turn it back to the default, then you have to push "noboolalpha".
So that will go back to the default behaviour of printing 1's or 0's.
So if we do nothing, we get the number zero, for false.
And then with boolalpha, it will print false.
And if we try it again, without putting boolalpha this time, it still does.
True.
And then with noboolalpha, it goes back to printing numbers.
So, boolalpha will change the state of the stream and noboolalpha will change it back to the default.
By default, output streams do not perform any formatting on the data.
If you print, for example, two variables, then it will use just enough room for the data in the first
argument and then just enough room for the second.
So they're going to be displayed like this. And quite often that is good enough.
But maybe we want to have these lined up.
So the five is right above the two.
For example, if we have some results and we want to present them in a table. So we need to make
all the outputs line up, and that means we need to make each output field the same width as the
one below it or above it.
There is a setw manipulator. So we can tell the stream how wide we want a field to be. The argument
to setw is the width of the field. And this will only affect the next data item on the stream.
setw takes an argument, so it is defined in iomanip.
So let's have a look at this.
We are going to display our "Penguins" and "Polar Bears".
(Perhaps it is the football results, I do not know!) And we are going to set the width of the field to 15.
So this means that it is going to use 15
characters to present the data in "Penguins" and 15 for "Polar Bears".
What it will actually do is to add some space characters to make the field the required width.
So let's try that out.
Well, we have got the "5" and the "2" matching up, but it is not quite what we wanted. The "Penguins"
and the "Polar bears" are over here.
We expected they would be over there.
The reason for this is that by default, the space characters are added at the left.
So the output lines up on the right.
So this is what is known as "right-justified" output.
There is another manipulator we can use to make the output "left-justified".
So the "Penguins" and "Polar Bears" are going to be at the left of the field and this manipulation is called
"left".
So setw() will only affect the next output field, so "5" would have the normal width, but the "left"
manipulator permanently changes the stream.
That is what is known as a "sticky" manipulator.
So when you make a change, it "sticks". Until you change it back.
And if we want to go back to using the default, right-justified, then there is a "right" manipulator,
which will do that.
So let's try adding that...
To our code...
And then there we are, we get the "Penguins" and "Polar Bears" lined up at the left of the field.
So a manipulator is said to be "sticky" if it permanently changes the state of the stream.
And in fact, that applies to most of the manipulators.
So the stream will remain in the same state, until we push another manipulator, which has the opposite
effect.
So if we push "left" to make the output left-justified, its will remain left-justified until we push "right"
to change it back to the default.
If a manipulation is not sticky, it will only affect the next output operation.
So this applies to setw(). And, in fact, setw() is the only one which is not "sticky".
So this is typical C++!
There's always one exceptional case, one which does not follow the rules.
And then finally, if you do not want to have spaces as the padding character, you can change it to something
else.
So setfill(), and then that will make the argument the padding character.
Again, this is a sticky manipulator, and you need to set it back to the default after you have used
it.
So there we are. The same code that we had before, but actually without the "left". So let's put that back
in.
And then we set the defaults back to right-justified as well. It is always a good idea to set the defaults
back again.
If you are working with other people and you change the defaults to something they are not expecting,
then you are probably going to annoy them.
So we want to avoid annoying other people.
If you're working in industry, then programming is all about teamwork. And part of teamwork is not
annoying people!
So there we are, instead of the space padding we have the has padding. And actually we still have one
space character, because that was in the original expression. So perhaps we should remove that...
To make things less confusing.
So there we are, the padding character is now the hash symbol...
And just to prove that things are back to normal - I do not know.
Let's have 11 hippopotamuses.
So you see, we've gone back to right justification.
And incidentally, I think there is more than 15 characters in "hippopotamuses", so it has just spilt over.
OK, so that is it for this video.
I will see you next time, but meanwhile, keep coding!