WEBVTT

00:00.350 --> 00:07.270
C plus plus 11 introduce another way to use the for loop, which is intended to be used with the containers.

00:07.280 --> 00:11.930
So the C plus plus standard library contains templates for the container classes.

00:11.960 --> 00:18.430
These classes contain a collection of objects and provide access to those items in a standard way.

00:18.440 --> 00:23.790
The standard way is to iterate through a collection using an iterator object.

00:23.810 --> 00:30.260
More details about how to do it is will begin in the next lecture here, but this syntax requires an

00:30.260 --> 00:34.710
understanding of pointers and iterators so we will not cover them here.

00:34.730 --> 00:42.050
So the range based for loop gives a simple mechanism to access items in a container without explicitly

00:42.050 --> 00:43.490
using iterators.

00:43.490 --> 00:47.390
So the syntax is simple here for here, for loop.

00:47.390 --> 00:50.300
Here for declaration.

00:51.770 --> 00:52.940
An expression.

00:53.590 --> 00:56.860
And after that, the loop statement begins.

00:57.070 --> 00:58.960
Loop statement here.

00:58.990 --> 01:05.590
So the first thing is to pointed out that there are only two expressions and they are separated by the

01:05.590 --> 01:06.100
colon.

01:07.030 --> 01:12.790
So the first expression is used to declare the loop variable, which is of the type of the items in

01:12.790 --> 01:14.890
the collection being iterated throughout.

01:14.890 --> 01:21.280
And the second expression is gives an access to the collection in the C plus plus times.

01:21.280 --> 01:28.810
The collection that can be used are those that define a begin and end function that gives a access to

01:28.810 --> 01:34.780
iterators and also stack based arrays and the set based arrays that the compiler knows the size of.

01:34.780 --> 01:42.670
Here, the standard library defines a container object vector here in this here, let's create a vector

01:42.670 --> 01:51.010
and the vector template is a class that contains items of the type specified in the angled braces like

01:51.010 --> 01:53.110
that here vector.

01:55.390 --> 01:58.280
And we're going to create a string vector here.

01:58.300 --> 01:58.960
String vector.

01:58.960 --> 02:05.510
As you can see, we got an error because we have to include the vector library here.

02:05.530 --> 02:07.820
Import vector library, as you can see here.

02:07.840 --> 02:10.930
So we imported vector library and.

02:14.220 --> 02:19.890
So, you know, the vector is initialized in a special way that is new to a C plus plus 11.

02:19.890 --> 02:22.620
And this is called the list initialization.

02:22.620 --> 02:29.730
So this syntax allows you to specify the initial values of the vector in a list between curly braces

02:29.730 --> 02:30.180
here.

02:30.180 --> 02:38.520
So let's make an my cars variable and make it, for example, BMW, M.

02:39.990 --> 02:41.280
Fourth here.

02:43.360 --> 02:46.450
Portiere Fiat here.

02:47.200 --> 02:49.630
Volkswagen we're going to add here.

02:51.430 --> 02:52.570
Volkswagen.

02:52.750 --> 02:57.070
And we're going to add another Audi here.

02:57.490 --> 02:58.210
Here.

02:58.920 --> 03:03.690
And we're going to create a for loop for integer E.

03:04.820 --> 03:05.600
Zero.

03:05.630 --> 03:15.680
While Integer is less than my cast size and increase e by one and see out.

03:16.860 --> 03:17.250
Uh.

03:17.280 --> 03:18.000
Okay.

03:18.740 --> 03:20.770
See out my cars.

03:20.780 --> 03:23.200
Dot add e.

03:24.690 --> 03:26.250
And end line.

03:28.950 --> 03:30.030
And line here.

03:33.790 --> 03:34.510
So.

03:36.390 --> 03:37.230
Here.

03:39.000 --> 03:49.200
The vector class has a member of called member function called size that the called through the dot

03:49.200 --> 03:55.860
operator here which means the call this function on this object that returns the number of items in

03:55.860 --> 03:56.550
the vector.

03:56.550 --> 04:00.060
So each item is accessed.

04:01.160 --> 04:03.440
Using add function here.

04:04.240 --> 04:06.880
Passing the object index.

04:07.600 --> 04:11.550
The one big problem with this code is that it uses random access.

04:11.560 --> 04:15.790
That is, it accesses each item using its index.

04:15.790 --> 04:22.990
So this is a property of vector, but standard library container types do not have random access.

04:23.350 --> 04:27.100
Now we're going to create another range based loop here.

04:29.840 --> 04:31.020
Arranged baseball.

04:31.050 --> 04:32.520
This is going to be a range based loop.

04:34.420 --> 04:35.470
Based.

04:36.950 --> 04:41.420
Slope here and let's create the previous example we did here.

04:41.420 --> 04:48.770
So for string model, my car model.

04:50.390 --> 04:53.330
Oh, yes, my car model here.

04:54.970 --> 04:56.320
And my car's.

04:59.430 --> 05:00.870
And see out.

05:03.180 --> 05:07.320
My car model and inline.

05:07.500 --> 05:08.310
So.

05:09.590 --> 05:11.390
Let's print it again.

05:13.160 --> 05:13.940
And.

05:14.800 --> 05:16.120
Here say out.

05:17.810 --> 05:19.100
Uh, just to make.

05:19.100 --> 05:20.180
Make it look nice.

05:23.350 --> 05:24.460
Look here.

05:33.690 --> 05:34.350
Here.

05:34.710 --> 05:38.190
So this is the first example and this is the second.

05:38.280 --> 05:41.580
So actually the output is the same.

05:41.910 --> 05:47.350
They look, they look same, but each item is accessed using the add function here.

05:47.370 --> 05:53.460
But in this leg here, the we just use the range based loop here.

05:56.400 --> 06:01.110
So the standard library containers type, as I said earlier, do not have random access.

06:01.110 --> 06:05.430
And this for loop uses the range based for here.

06:05.700 --> 06:09.130
So actually we can create another work.

06:09.300 --> 06:16.020
So as you know, this type here works with any of the standard container types and for arrays allocated

06:16.020 --> 06:16.950
on the stack here.

06:16.950 --> 06:18.900
So we're going to create another here.

06:19.290 --> 06:27.120
Um, let's make it my car years here, my cars here.

06:28.410 --> 06:28.890
Here.

06:29.250 --> 06:31.980
And, for example.

06:33.210 --> 06:33.560
Um.

06:34.770 --> 06:36.030
2002.

06:36.060 --> 06:37.500
2005.

06:38.340 --> 06:39.960
1989.

06:41.230 --> 06:42.000
Okay.

06:42.240 --> 06:43.380
And how many?

06:43.440 --> 06:44.670
Yeah, we have five.

06:44.700 --> 06:46.350
Let's create a five to make it.

06:46.890 --> 06:51.090
Which are we going to print it here or make it for where you want to?

06:51.420 --> 06:53.130
Uh, you want access here.

06:53.910 --> 06:55.740
And 2010.

06:57.140 --> 07:01.190
So and let's print it out using this range based for.

07:01.520 --> 07:04.550
For loop here, for integer.

07:05.030 --> 07:06.830
Uh, here, here.

07:07.010 --> 07:10.820
And my cars here and here.

07:10.850 --> 07:15.560
C out my here and.

07:16.100 --> 07:16.900
And line.

07:19.750 --> 07:20.170
Here.

07:20.200 --> 07:22.120
As you can see, we printed this as well.

07:22.120 --> 07:28.450
In this case, the compiler knows the size of the array because the compiler has allocated the array

07:28.450 --> 07:29.050
here.

07:29.050 --> 07:31.360
And so it can determine the range.

07:31.390 --> 07:36.880
The range based for loop will iterate through all the items in container.

07:36.880 --> 07:43.720
But as with the previous version, you can leave the for loop using the break, return, throw or go

07:43.720 --> 07:44.200
to here.

07:44.200 --> 07:51.820
So and you can indicate that the next loop should be executed using the condition continue statement

07:51.820 --> 07:53.950
here so we can make it like that here.

07:54.820 --> 07:55.480
Okay.

07:55.480 --> 07:56.380
If.

07:57.990 --> 07:58.410
Make it.

07:58.560 --> 07:59.970
Let's make an if statement.

07:59.970 --> 08:01.710
Write an if statement here.

08:01.950 --> 08:02.760
So.

08:03.850 --> 08:12.400
If our year equals to two 1989, make it just an.

08:13.480 --> 08:14.050
Okay?

08:14.320 --> 08:16.000
No, actually.

08:16.870 --> 08:17.100
It.

08:17.230 --> 08:21.070
Uh, the year car is too old.

08:21.100 --> 08:21.970
See out.

08:23.090 --> 08:24.920
Car is to.

08:26.460 --> 08:26.940
All.

08:28.120 --> 08:29.590
Old classic.

08:31.000 --> 08:32.440
We'll make it classic car.

08:32.740 --> 08:36.700
And of course, we have to end line here to make it look nice.

08:36.700 --> 08:41.740
And else here, just print the year of the car here.

08:42.010 --> 08:43.210
Make it here.

08:43.210 --> 08:55.090
As you can see here, when it comes to the 1989, it shows the if statement is executed and else the

08:55.120 --> 08:58.300
C out here is just printed to the screen here.
