WEBVTT

00:00.780 --> 00:02.280
So far in this course.

00:02.310 --> 00:07.090
Initializer list have been treated as a kind of C plus plus 11 construct.

00:07.140 --> 00:09.150
And a bit like the bulletin arrays.

00:09.330 --> 00:17.610
So, in fact, when you use the initializer list syntax using the braces here, the compiler actually

00:17.610 --> 00:23.410
creates an instance of the templated initialize list class here.

00:23.430 --> 00:30.180
So if an initializer list is used to initialize another type, for example, in this case the vector

00:30.180 --> 00:30.840
here.

00:33.920 --> 00:43.990
Uh, then the compiler creates an initialize list object and with the given values between the braces

00:43.990 --> 00:51.190
here, so and the container object is initializing the initializer list iterator.

00:51.190 --> 00:59.080
So this ability to create an initialized list object from a braced initializer list can be used to give

00:59.080 --> 01:01.300
a function a variable number of parameters.

01:01.300 --> 01:03.430
So I'll beat all of the parameters.

01:03.430 --> 01:06.160
Must be the same type in this case here.

01:06.160 --> 01:11.980
So in order to use this initializer list, we have to firstly include the initializer.

01:13.080 --> 01:14.400
Initializer list.

01:15.000 --> 01:16.290
Header file here.

01:16.290 --> 01:23.190
And then we're going to create a new function named sum and.

01:23.970 --> 01:25.980
But you can return the integer here.

01:26.130 --> 01:28.380
Sum and initial.

01:34.490 --> 01:36.290
List here and.

01:37.170 --> 01:37.890
Then.

01:42.370 --> 01:48.040
This initializer list will take the integer values here and make it values.

01:48.580 --> 01:52.670
So this sum here takes the parameter values.

01:52.690 --> 01:58.300
This will take one parameter and the type of initializer list integer here.

01:58.300 --> 02:03.940
So which here integer sum equals zero here, for example.

02:03.940 --> 02:07.900
And we're going to create a for loop integer values.

02:08.440 --> 02:11.590
We're going to iterate throughout this parameter.

02:12.630 --> 02:13.320
Values.

02:13.320 --> 02:16.050
And with this for loop.

02:16.080 --> 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

02:28.080 --> 02:30.300
at every iterating here.

02:31.160 --> 02:35.000
And we're going to after this, we're going to return the.

02:35.960 --> 02:37.970
Add some integer here.

02:38.150 --> 02:44.000
So in the main function we're going to create, we're going to make actually the main function is going

02:44.000 --> 02:50.960
to just print something and call this some, uh, function here that we created here.

02:51.350 --> 02:53.210
So C out.

02:54.960 --> 02:55.650
Some.

02:57.270 --> 02:59.040
Here and.

02:59.850 --> 03:00.630
End line.

03:00.840 --> 03:06.990
Firstly, we're going to call just an empty sum and see out sum.

03:07.200 --> 03:09.450
Here we're going to pass the values here.

03:09.450 --> 03:16.500
In this case, we're going to pass the negative values starting from six minus six, negative six here.

03:17.910 --> 03:22.770
Five, four, three, two.

03:23.870 --> 03:27.410
One and here and line.

03:28.820 --> 03:29.780
So.

03:30.660 --> 03:31.680
C out.

03:32.130 --> 03:33.240
C out.

03:33.270 --> 03:33.840
Some.

03:33.840 --> 03:41.400
And this case we're going to make give it a ten, 20, 30 and end line here.

03:41.400 --> 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

03:50.790 --> 03:51.720
to get a zero.

03:51.720 --> 03:55.680
In this case, we're going to get the -2021.

03:55.680 --> 04:02.910
And in this case, we going to get the 16, as you can see here.

04:02.910 --> 04:11.400
And the sum function here has a single parameter initializer list integer here, which can only be initialized

04:11.400 --> 04:14.730
with the list of integers here.

04:14.730 --> 04:21.180
So the initializer list class has a very few functions because it only exists to give access to the

04:21.180 --> 04:23.340
values in the brace list here.

04:23.340 --> 04:32.860
So most significantly, it implements a size function that returns the number of items in the list here.

04:32.860 --> 04:35.470
So you can use values that size.

04:35.500 --> 04:40.780
As you can see, this will return the number of items in the list.

04:41.200 --> 04:42.160
Let me mention it.

04:42.160 --> 04:44.860
So when you download this code, you're going to see.

04:45.570 --> 04:50.760
Returns, number of items in the list.

04:51.330 --> 04:52.650
So here.

04:53.710 --> 04:58.110
And size function returns the number of items in the list.

04:58.120 --> 04:59.200
So.

05:00.660 --> 05:07.680
And the begin and end functions in return pointer to the first item in the list and to the position

05:07.680 --> 05:09.060
after the last item.

05:09.060 --> 05:16.950
So these two functions are needed to give iterator access to the list and it enables you to use the

05:16.950 --> 05:20.730
object with with the range for loop here.

05:20.730 --> 05:26.220
So as you can see here, we when we compile the code, we're going to get the zero -20.

05:26.220 --> 05:29.550
Why 21 and 60 here?

05:29.820 --> 05:35.790
So in this example, the main function here calls this function three times the sum function, three

05:35.790 --> 05:40.980
times each time with the priest initializer list here, as you can see here.

05:41.100 --> 05:47.670
So and the function will return the sum of some of the items in the list here.

05:47.670 --> 05:56.610
And as you can see, it, will the sum here multiply or will some of the numbers in the list?

05:56.610 --> 06:02.830
So clearly, this technique means that the each item in the variable parameter list has to be the same

06:02.830 --> 06:09.130
type or type that can in can be converted to a specify specified type.

06:09.130 --> 06:14.320
So you will have the same result if the parameter had been vector.

06:14.320 --> 06:21.880
And the difference is that the initializer initializer list parameter requires the list initialization

06:21.880 --> 06:22.210
here.

06:22.210 --> 06:26.950
So if we make it like this two point make, give it the 2.1.

06:26.950 --> 06:33.010
As you can see here, we are going to get error because the initializer list can only you can just only

06:33.010 --> 06:38.110
pass the values that has the same type as this parameter here.
