WEBVTT

00:01.500 --> 00:06.000
Hello again! In this video, we are going to look at stream manipulators and formatting.

00:08.500 --> 00:13.420
A manipulator is something that gets pushed onto a stream to affect the stream's behaviour.

00:14.050 --> 00:17.760
We have already met the flush and endl manipulators.

00:18.790 --> 00:23.740
These affect these stream's behaviour, by causing it to flush the buffer immediately, instead of waiting

00:23.740 --> 00:25.030
until the buffer is full.

00:26.620 --> 00:33.280
The C++ library defines a number of manipulators for streams, and these alter the way that the stream

00:33.280 --> 00:34.420
formats the output.

00:35.140 --> 00:41.470
Most of these are in the iostream header, but there are some which are in iomanip, and that is for manipulators

00:41.470 --> 00:42.730
which take an argument.

00:47.030 --> 00:52.750
If you have tried to print out a Boolean, you will have noticed that it prints out the number 1 or

00:52.790 --> 00:55.160
0, and not the words "true" or "false".

00:56.360 --> 00:58.460
There is a boolalpha manipulator.

00:59.060 --> 01:02.060
And if we push that on the stream, then the stream will 

01:02.060 --> 01:04.310
actually start printing out the words "true" or "false".

01:05.000 --> 01:06.920
And then that will change the state of the stream.

01:06.920 --> 01:11.480
So any Boolean expression you give to it will be printed out as true or false.

01:15.700 --> 01:17.030
So let's try this out.

01:17.050 --> 01:21.790
So first of all, we are doing it the traditional way, without doing anything special.

01:22.330 --> 01:24.250
We have a Boolean expression.

01:24.940 --> 01:31.390
And then we print it out again, using boolalpha. And then we try printing out something, else just to

01:31.390 --> 01:33.190
show whether the stream still does it.

01:35.610 --> 01:38.580
And the answer is that it does. This will affect the behaviour of the stream.

01:38.850 --> 01:43.230
And if you want to turn it back to the default, then you have to push "noboolalpha".

01:44.010 --> 01:47.640
So that will go back to the default behaviour of printing 1's or 0's.

01:52.010 --> 01:54.920
So if we do nothing, we get the number zero, for false.

01:55.670 --> 01:58.100
And then with boolalpha, it will print false.

01:59.570 --> 02:03.470
And if we try it again, without putting boolalpha this time, it still does.

02:04.010 --> 02:04.360
True.

02:05.300 --> 02:08.570
And then with noboolalpha, it goes back to printing numbers.

02:11.820 --> 02:17.490
So, boolalpha will change the state of the stream and noboolalpha will change it back to the default.

02:18.360 --> 02:22.080
By default, output streams do not perform any formatting on the data.

02:22.560 --> 02:28.920
If you print, for example, two variables, then it will use just enough room for the data in the first

02:29.310 --> 02:32.010
argument and then just enough room for the second.

02:32.640 --> 02:37.260
So they're going to be displayed like this. And quite often that is good enough.

02:37.260 --> 02:39.660
But maybe we want to have these lined up.

02:39.660 --> 02:41.400
So the five is right above the two.

02:45.560 --> 02:51.050
For example, if we have some results and we want to present them in a table. So we need to make

02:51.050 --> 02:56.720
all the outputs line up, and that means we need to make each output field the same width as the

02:56.720 --> 02:58.010
one below it or above it.

03:01.390 --> 03:08.680
There is a setw manipulator. So we can tell the stream how wide we want a field to be. The argument

03:08.680 --> 03:16.010
to setw is the width of the field. And this will only affect the next data item on the stream.

03:16.070 --> 03:19.090
setw takes an argument, so it is defined in iomanip.

03:22.030 --> 03:23.260
So let's have a look at this.

03:23.720 --> 03:26.620
We are going to display our "Penguins" and "Polar Bears".

03:26.800 --> 03:32.320
(Perhaps it is the football results, I do not know!) And we are going to set the width of the field to 15.

03:32.830 --> 03:34.870
So this means that it is going to use 15

03:36.340 --> 03:40.180
characters to present the data in "Penguins" and 15 for "Polar Bears".

03:40.300 --> 03:45.190
What it will actually do is to add some space characters to make the field the required width.

03:45.880 --> 03:46.900
So let's try that out.

03:49.980 --> 03:56.010
Well, we have got the "5" and the "2" matching up, but it is not quite what we wanted. The "Penguins"

03:56.010 --> 03:57.820
and the "Polar bears" are over here.

03:58.200 --> 03:59.670
We expected they would be over there.

04:00.330 --> 04:04.980
The reason for this is that by default, the space characters are added at the left.

04:05.490 --> 04:07.350
So the output lines up on the right.

04:07.770 --> 04:10.500
So this is what is known as "right-justified" output.

04:14.910 --> 04:19.050
There is another manipulator we can use to make the output "left-justified".

04:19.110 --> 04:25.710
So the "Penguins" and "Polar Bears" are going to be at the left of the field and this manipulation is called

04:25.710 --> 04:26.130
"left".

04:28.980 --> 04:35.790
So setw() will only affect the next output field, so "5" would have the normal width, but the "left"

04:35.790 --> 04:37.860
manipulator permanently changes the stream.

04:38.340 --> 04:40.650
That is what is known as a "sticky" manipulator.

04:41.160 --> 04:45.420
So when you make a change, it "sticks". Until you change it back.

04:46.380 --> 04:51.840
And if we want to go back to using the default, right-justified, then there is a "right" manipulator,

04:51.840 --> 04:52.620
which will do that.

04:55.160 --> 04:56.720
So let's try adding that...

05:00.220 --> 05:00.880
To our code...

05:03.520 --> 05:07.600
And then there we are, we get the "Penguins" and "Polar Bears" lined up at the left of the field.

05:15.370 --> 05:20.500
So a manipulator is said to be "sticky" if it permanently changes the state of the stream.

05:21.010 --> 05:23.710
And in fact, that applies to most of the manipulators.

05:24.250 --> 05:29.710
So the stream will remain in the same state, until we push another manipulator, which has the opposite

05:29.710 --> 05:30.130
effect.

05:30.520 --> 05:37.210
So if we push "left" to make the output left-justified, its will remain left-justified until we push "right"

05:37.630 --> 05:39.400
to change it back to the default.

05:41.840 --> 05:46.100
If a manipulation is not sticky, it will only affect the next output operation.

05:46.820 --> 05:51.200
So this applies to setw(). And, in fact, setw() is the only one which is not "sticky".

05:51.770 --> 05:53.450
So this is typical C++!

05:53.450 --> 05:56.810
There's always one exceptional case, one which does not follow the rules.

06:01.250 --> 06:05.930
And then finally, if you do not want to have spaces as the padding character, you can change it to something

06:05.930 --> 06:06.230
else.

06:06.710 --> 06:11.270
So setfill(), and then that will make the argument the padding character.

06:14.680 --> 06:19.570
Again, this is a sticky manipulator, and you need to set it back to the default after you have used

06:19.570 --> 06:19.810
it.

06:21.430 --> 06:26.380
So there we are. The same code that we had before, but actually without the "left". So let's put that back

06:26.380 --> 06:26.650
in.

06:32.980 --> 06:40.420
And then we set the defaults back to right-justified as well. It is always a good idea to set the defaults

06:40.420 --> 06:40.930
back again.

06:41.470 --> 06:46.000
If you are working with other people and you change the defaults to something they are not expecting,

06:46.360 --> 06:47.850
then you are probably going to annoy them.

06:47.890 --> 06:50.860
So we want to avoid annoying other people.

06:51.700 --> 06:56.330
If you're working in industry, then programming is all about teamwork. And part of teamwork is not

06:56.330 --> 06:56.920
annoying people!

07:00.110 --> 07:04.760
So there we are, instead of the space padding we have the has padding. And actually we still have one

07:04.940 --> 07:10.280
space character, because that was in the original expression. So perhaps we should remove that...

07:13.110 --> 07:14.670
To make things less confusing.

07:16.870 --> 07:21.190
So there we are, the padding character is now the hash symbol...

07:32.530 --> 07:35.830
And just to prove that things are back to normal - I do not know.

07:44.300 --> 07:45.770
Let's have 11 hippopotamuses.

07:49.230 --> 07:51.360
So you see, we've gone back to right justification.

07:51.810 --> 07:56.760
And incidentally, I think there is more than 15 characters in "hippopotamuses", so it has just spilt over.

07:59.230 --> 08:00.850
OK, so that is it for this video.

08:01.300 --> 08:04.090
I will see you next time, but meanwhile, keep coding!
