C plus plus 11 introduce another way to use the for loop, which is intended to be used with the containers.
So the C plus plus standard library contains templates for the container classes.
These classes contain a collection of objects and provide access to those items in a standard way.
The standard way is to iterate through a collection using an iterator object.
More details about how to do it is will begin in the next lecture here, but this syntax requires an
understanding of pointers and iterators so we will not cover them here.
So the range based for loop gives a simple mechanism to access items in a container without explicitly
using iterators.
So the syntax is simple here for here, for loop.
Here for declaration.
An expression.
And after that, the loop statement begins.
Loop statement here.
So the first thing is to pointed out that there are only two expressions and they are separated by the
colon.
So the first expression is used to declare the loop variable, which is of the type of the items in
the collection being iterated throughout.
And the second expression is gives an access to the collection in the C plus plus times.
The collection that can be used are those that define a begin and end function that gives a access to
iterators and also stack based arrays and the set based arrays that the compiler knows the size of.
Here, the standard library defines a container object vector here in this here, let's create a vector
and the vector template is a class that contains items of the type specified in the angled braces like
that here vector.
And we're going to create a string vector here.
String vector.
As you can see, we got an error because we have to include the vector library here.
Import vector library, as you can see here.
So we imported vector library and.
So, you know, the vector is initialized in a special way that is new to a C plus plus 11.
And this is called the list initialization.
So this syntax allows you to specify the initial values of the vector in a list between curly braces
here.
So let's make an my cars variable and make it, for example, BMW, M.
Fourth here.
Portiere Fiat here.
Volkswagen we're going to add here.
Volkswagen.
And we're going to add another Audi here.
Here.
And we're going to create a for loop for integer E.
Zero.
While Integer is less than my cast size and increase e by one and see out.
Uh.
Okay.
See out my cars.
Dot add e.
And end line.
And line here.
So.
Here.
The vector class has a member of called member function called size that the called through the dot
operator here which means the call this function on this object that returns the number of items in
the vector.
So each item is accessed.
Using add function here.
Passing the object index.
The one big problem with this code is that it uses random access.
That is, it accesses each item using its index.
So this is a property of vector, but standard library container types do not have random access.
Now we're going to create another range based loop here.
Arranged baseball.
This is going to be a range based loop.
Based.
Slope here and let's create the previous example we did here.
So for string model, my car model.
Oh, yes, my car model here.
And my car's.
And see out.
My car model and inline.
So.
Let's print it again.
And.
Here say out.
Uh, just to make.
Make it look nice.
Look here.
Here.
So this is the first example and this is the second.
So actually the output is the same.
They look, they look same, but each item is accessed using the add function here.
But in this leg here, the we just use the range based loop here.
So the standard library containers type, as I said earlier, do not have random access.
And this for loop uses the range based for here.
So actually we can create another work.
So as you know, this type here works with any of the standard container types and for arrays allocated
on the stack here.
So we're going to create another here.
Um, let's make it my car years here, my cars here.
Here.
And, for example.
Um.
2002.
2005.
1989.
Okay.
And how many?
Yeah, we have five.
Let's create a five to make it.
Which are we going to print it here or make it for where you want to?
Uh, you want access here.
And 2010.
So and let's print it out using this range based for.
For loop here, for integer.
Uh, here, here.
And my cars here and here.
C out my here and.
And line.
Here.
As you can see, we printed this as well.
In this case, the compiler knows the size of the array because the compiler has allocated the array
here.
And so it can determine the range.
The range based for loop will iterate through all the items in container.
But as with the previous version, you can leave the for loop using the break, return, throw or go
to here.
So and you can indicate that the next loop should be executed using the condition continue statement
here so we can make it like that here.
Okay.
If.
Make it.
Let's make an if statement.
Write an if statement here.
So.
If our year equals to two 1989, make it just an.
Okay?
No, actually.
It.
Uh, the year car is too old.
See out.
Car is to.
All.
Old classic.
We'll make it classic car.
And of course, we have to end line here to make it look nice.
And else here, just print the year of the car here.
Make it here.
As you can see here, when it comes to the 1989, it shows the if statement is executed and else the
C out here is just printed to the screen here.