1 00:00:05,770 --> 00:00:09,370 In this video, we'll learn about some of the c++ stream manipulators. 2 00:00:10,270 --> 00:00:14,070 C++ streams have many useful member methods that can be used to 3 00:00:14,070 --> 00:00:15,270 control formatting. 4 00:00:16,370 --> 00:00:19,250 There are methods that work on input and output streams, 5 00:00:19,250 --> 00:00:21,610 but formatting output is much more common. 6 00:00:22,270 --> 00:00:26,820 An important concept to understand is that when we manipulate a stream for formatting, 7 00:00:26,820 --> 00:00:31,220 sometimes the settings we make will be set for the remainder of the program, 8 00:00:31,220 --> 00:00:35,020 other times they'll only affect the next object placed on the stream, 9 00:00:35,020 --> 00:00:37,020 and sometimes their effect is immediate. 10 00:00:37,570 --> 00:00:40,930 When we go over the individual formatting methods and manipulators, 11 00:00:40,930 --> 00:00:45,230 I'll show you the effect each one has as well as how long the effect lasts. 12 00:00:45,230 --> 00:00:48,130 Most of the stream formatters come in two versions, 13 00:00:48,130 --> 00:00:50,730 a method version and a manipulator version. 14 00:00:51,030 --> 00:00:55,130 The method version is simply a method call that you use with the stream object. 15 00:00:55,530 --> 00:00:58,030 For example, cout.width 10. 16 00:00:58,430 --> 00:01:02,790 I'll explain what this does in a later section. But for now, you can see the syntax. 17 00:01:03,040 --> 00:01:05,940 It's calling the width method on the cout object. 18 00:01:06,600 --> 00:01:09,400 There's also a manipulator version, which is designed 19 00:01:09,400 --> 00:01:14,390 to be used inline as a stream insertion. In this case, set w10. 20 00:01:15,290 --> 00:01:19,590 As you probably guessed, the manipulator versions are simply overloading the insertion operator 21 00:01:19,590 --> 00:01:21,590 to make this easy to use. 22 00:01:22,090 --> 00:01:25,590 To use the manipulators, you must include io manip. 23 00:01:25,890 --> 00:01:29,080 The code we'll write will focus mainly on manipulator usage, 24 00:01:29,080 --> 00:01:31,080 but I'll also show you how to use the methods. 25 00:01:32,580 --> 00:01:37,480 In the next few videos, we'll go over some of the most commonly used stream manipulators. 26 00:01:37,480 --> 00:01:38,480 Here's a preview. 27 00:01:39,140 --> 00:01:42,140 We'll first start by looking at formatting Boolean types. 28 00:01:42,140 --> 00:01:44,840 We've already seen this used in the course in a few places. 29 00:01:45,340 --> 00:01:49,940 We can use bool alpha and no bool alpha to display Boolean values 30 00:01:49,940 --> 00:01:54,540 as either the strings true or false or the integers 1 and 0. 31 00:01:55,530 --> 00:01:57,890 Then we'll look at formatting integer types. 32 00:01:57,890 --> 00:02:03,280 These manipulators allow us to display integers base 10, base 16 or base 8 33 00:02:03,580 --> 00:02:06,940 as well as to decide whether we want the base prefix to show 34 00:02:06,940 --> 00:02:09,440 and in uppercase or lowercase. 35 00:02:09,440 --> 00:02:14,340 We can also decide if we want positive numbers to show the plus symbol in front of the number. 36 00:02:15,000 --> 00:02:17,300 Then we'll switch over to floating point numbers. 37 00:02:17,300 --> 00:02:20,660 Again, we've already seen some of these manipulators used in the course. 38 00:02:20,660 --> 00:02:23,650 We can use fixed point, we can set the precision, 39 00:02:23,650 --> 00:02:26,640 we can show the number in scientific notation format, 40 00:02:26,640 --> 00:02:30,000 and again, we can decide whether we want trailing 0s printed, 41 00:02:30,000 --> 00:02:32,500 and whether we want the plus symbol displayed. 42 00:02:33,380 --> 00:02:37,740 Then we'll learn how to output numbers and strings in fields of fixed width. 43 00:02:38,100 --> 00:02:41,700 And we can see how we can left or right justify them within that field. 44 00:02:42,600 --> 00:02:46,600 We'll also be able to fill empty space within the field to a specific character. 45 00:02:47,600 --> 00:02:51,200 Finally, there are several other formatters that can be very useful. 46 00:02:51,860 --> 00:02:54,080 Endline, we've already seen and used. 47 00:02:54,080 --> 00:02:58,440 Flush can be used when we want to be sure that we flush out any stream buffers, 48 00:02:58,440 --> 00:03:00,940 but it doesn't add a new line like endline does. 49 00:03:01,820 --> 00:03:05,820 The last few we really won't talk about much, but I'm including them here for your reference. 50 00:03:05,820 --> 00:03:09,180 Skip white space, no skip white space and white space 51 00:03:09,180 --> 00:03:11,680 are typically used with input streams when 52 00:03:11,680 --> 00:03:16,180 when you have very specific use cases about dealing with white space. 53 00:03:16,980 --> 00:03:20,980 Okay. So in the next video, we'll start with the Boolean formatters. I'll see you there.