C++ 11 introduce another way to use the for loop, which is intended to be used with the containers,
though the C++ Standard library contains templates for the container classes.
These classes contain collection of objects and provide access to those items in a standard way.
The standard way is to iterate throughout collection using an iterator object.
More details about how to do it will begin in the next lecture here, but the 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 it.
Reuters.
So the syntax is simple here for here, for loop, here for declaration.
And expression.
And after that, the loop statement begins the loop statement here.
So the first thing is to point out that there are only two expressions and they are separated by the
column.
So the first expression is used to declare the loop variable, which is of the type of the items in
the collection being iterated fraud.
And the second expression is gives an access to the collection.
In a C++ terms, the collection that can be used are those that define a begin and end function that
gives an access to iterators and also stack based arrays and the set based arrays that the compiler
knows the size of here.
This type of 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 import the vector library and.
So, you know, the vector is initialized in a special way that is new to a C++ 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 it my course variable and make it, for example, BMW.
Fourth year.
For their part here, Volkswagen, we're going to add here.
Volkswagen.
And we're going to add another Audi here.
Here.
And they're going to create a for loop for integer E.
Zero.
While integer is less than my car such size and imp increase e by one and C out.
Uh, okay.
Theof my car start at e!
And.
And like.
And then here.
So.
Here.
The vector class has a member of card member function called size.
The the cultural, the 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 at function here.
Passing the objects index.
The one big problem with this code is that it uses random access that is, accesses each item using
its index.
So this is 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 dangerous slope.
Based.
Look here and let's create the previous example we did here.
So for string model, my car model.
Yes, my car model here.
And my cars.
And see out.
My car model and in life.
So.
That's Prince again.
And.
Say out.
Just make make it look nice.
Move 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 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 arranged 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 are located
on the stack here.
So we're going to create another here.
Let's make it my car here.
So here, my car here.
Here, and for example.
2000 to 2005.
1989.
Okay.
And how many?
Yeah, we have five.
Let's create a file to make it.
Which we going to print it here or make it four where you want to.
You want access here and 2010.
So and let's print it out using this range based four for loop here for integer year here and my cards
here and here.
See out my year 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 trove 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.
If.
Make.
Let's make an if statement right in his statement here.
So.
If our year equals 2 to 1989, make it, uh, just an.
No, actually.
Um.
The year car is too old.
See out.
Car is too.
All.
Old classic.
We'll make it classic car.
And of course, we have the end line here to make it look nice.
And else here, just print a 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 see out here is just printed to the screen here.