1 00:00:00,780 --> 00:00:02,280 So far in this course. 2 00:00:02,310 --> 00:00:07,090 Initializer list have been treated as a kind of C plus plus 11 construct. 3 00:00:07,140 --> 00:00:09,150 And a bit like the bulletin arrays. 4 00:00:09,330 --> 00:00:17,610 So, in fact, when you use the initializer list syntax using the braces here, the compiler actually 5 00:00:17,610 --> 00:00:23,410 creates an instance of the templated initialize list class here. 6 00:00:23,430 --> 00:00:30,180 So if an initializer list is used to initialize another type, for example, in this case the vector 7 00:00:30,180 --> 00:00:30,840 here. 8 00:00:33,920 --> 00:00:43,990 Uh, then the compiler creates an initialize list object and with the given values between the braces 9 00:00:43,990 --> 00:00:51,190 here, so and the container object is initializing the initializer list iterator. 10 00:00:51,190 --> 00:00:59,080 So this ability to create an initialized list object from a braced initializer list can be used to give 11 00:00:59,080 --> 00:01:01,300 a function a variable number of parameters. 12 00:01:01,300 --> 00:01:03,430 So I'll beat all of the parameters. 13 00:01:03,430 --> 00:01:06,160 Must be the same type in this case here. 14 00:01:06,160 --> 00:01:11,980 So in order to use this initializer list, we have to firstly include the initializer. 15 00:01:13,080 --> 00:01:14,400 Initializer list. 16 00:01:15,000 --> 00:01:16,290 Header file here. 17 00:01:16,290 --> 00:01:23,190 And then we're going to create a new function named sum and. 18 00:01:23,970 --> 00:01:25,980 But you can return the integer here. 19 00:01:26,130 --> 00:01:28,380 Sum and initial. 20 00:01:34,490 --> 00:01:36,290 List here and. 21 00:01:37,170 --> 00:01:37,890 Then. 22 00:01:42,370 --> 00:01:48,040 This initializer list will take the integer values here and make it values. 23 00:01:48,580 --> 00:01:52,670 So this sum here takes the parameter values. 24 00:01:52,690 --> 00:01:58,300 This will take one parameter and the type of initializer list integer here. 25 00:01:58,300 --> 00:02:03,940 So which here integer sum equals zero here, for example. 26 00:02:03,940 --> 00:02:07,900 And we're going to create a for loop integer values. 27 00:02:08,440 --> 00:02:11,590 We're going to iterate throughout this parameter. 28 00:02:12,630 --> 00:02:13,320 Values. 29 00:02:13,320 --> 00:02:16,050 And with this for loop. 30 00:02:16,080 --> 00:02:28,050 We're going to use this as the we're going to some increment, some by one at every or some by e here 31 00:02:28,080 --> 00:02:30,300 at every iterating here. 32 00:02:31,160 --> 00:02:35,000 And we're going to after this, we're going to return the. 33 00:02:35,960 --> 00:02:37,970 Add some integer here. 34 00:02:38,150 --> 00:02:44,000 So in the main function we're going to create, we're going to make actually the main function is going 35 00:02:44,000 --> 00:02:50,960 to just print something and call this some, uh, function here that we created here. 36 00:02:51,350 --> 00:02:53,210 So C out. 37 00:02:54,960 --> 00:02:55,650 Some. 38 00:02:57,270 --> 00:02:59,040 Here and. 39 00:02:59,850 --> 00:03:00,630 End line. 40 00:03:00,840 --> 00:03:06,990 Firstly, we're going to call just an empty sum and see out sum. 41 00:03:07,200 --> 00:03:09,450 Here we're going to pass the values here. 42 00:03:09,450 --> 00:03:16,500 In this case, we're going to pass the negative values starting from six minus six, negative six here. 43 00:03:17,910 --> 00:03:22,770 Five, four, three, two. 44 00:03:23,870 --> 00:03:27,410 One and here and line. 45 00:03:28,820 --> 00:03:29,780 So. 46 00:03:30,660 --> 00:03:31,680 C out. 47 00:03:32,130 --> 00:03:33,240 C out. 48 00:03:33,270 --> 00:03:33,840 Some. 49 00:03:33,840 --> 00:03:41,400 And this case we're going to make give it a ten, 20, 30 and end line here. 50 00:03:41,400 --> 00:03:50,790 So in this case, we're going to get the if we pass the NON-OBJECT to this some function, we're going 51 00:03:50,790 --> 00:03:51,720 to get a zero. 52 00:03:51,720 --> 00:03:55,680 In this case, we're going to get the -2021. 53 00:03:55,680 --> 00:04:02,910 And in this case, we going to get the 16, as you can see here. 54 00:04:02,910 --> 00:04:11,400 And the sum function here has a single parameter initializer list integer here, which can only be initialized 55 00:04:11,400 --> 00:04:14,730 with the list of integers here. 56 00:04:14,730 --> 00:04:21,180 So the initializer list class has a very few functions because it only exists to give access to the 57 00:04:21,180 --> 00:04:23,340 values in the brace list here. 58 00:04:23,340 --> 00:04:32,860 So most significantly, it implements a size function that returns the number of items in the list here. 59 00:04:32,860 --> 00:04:35,470 So you can use values that size. 60 00:04:35,500 --> 00:04:40,780 As you can see, this will return the number of items in the list. 61 00:04:41,200 --> 00:04:42,160 Let me mention it. 62 00:04:42,160 --> 00:04:44,860 So when you download this code, you're going to see. 63 00:04:45,570 --> 00:04:50,760 Returns, number of items in the list. 64 00:04:51,330 --> 00:04:52,650 So here. 65 00:04:53,710 --> 00:04:58,110 And size function returns the number of items in the list. 66 00:04:58,120 --> 00:04:59,200 So. 67 00:05:00,660 --> 00:05:07,680 And the begin and end functions in return pointer to the first item in the list and to the position 68 00:05:07,680 --> 00:05:09,060 after the last item. 69 00:05:09,060 --> 00:05:16,950 So these two functions are needed to give iterator access to the list and it enables you to use the 70 00:05:16,950 --> 00:05:20,730 object with with the range for loop here. 71 00:05:20,730 --> 00:05:26,220 So as you can see here, we when we compile the code, we're going to get the zero -20. 72 00:05:26,220 --> 00:05:29,550 Why 21 and 60 here? 73 00:05:29,820 --> 00:05:35,790 So in this example, the main function here calls this function three times the sum function, three 74 00:05:35,790 --> 00:05:40,980 times each time with the priest initializer list here, as you can see here. 75 00:05:41,100 --> 00:05:47,670 So and the function will return the sum of some of the items in the list here. 76 00:05:47,670 --> 00:05:56,610 And as you can see, it, will the sum here multiply or will some of the numbers in the list? 77 00:05:56,610 --> 00:06:02,830 So clearly, this technique means that the each item in the variable parameter list has to be the same 78 00:06:02,830 --> 00:06:09,130 type or type that can in can be converted to a specify specified type. 79 00:06:09,130 --> 00:06:14,320 So you will have the same result if the parameter had been vector. 80 00:06:14,320 --> 00:06:21,880 And the difference is that the initializer initializer list parameter requires the list initialization 81 00:06:21,880 --> 00:06:22,210 here. 82 00:06:22,210 --> 00:06:26,950 So if we make it like this two point make, give it the 2.1. 83 00:06:26,950 --> 00:06:33,010 As you can see here, we are going to get error because the initializer list can only you can just only 84 00:06:33,010 --> 00:06:38,110 pass the values that has the same type as this parameter here.