WEBVTT

00:00:00.640 --> 00:00:05.990
Assigning a standard array to another one is very convenient and in this case,

00:00:05.990 --> 00:00:09.250
it makes more sense than utilizing the copy function.

00:00:09.640 --> 00:00:14.410
However, if we wanted to, we could have used this function on standard arrays,

00:00:14.410 --> 00:00:14.860
too.

00:00:15.240 --> 00:00:17.890
We just need to find a way to provide the correct pointers

00:00:17.890 --> 00:00:19.890
to the first elements of both arrays,

00:00:19.890 --> 00:00:23.150
and a pointer to the first memory address after the

00:00:23.150 --> 00:00:25.260
last element of the first array.

00:00:25.640 --> 00:00:26.920
And that's actually easy.

00:00:26.920 --> 00:00:28.790
To get the location of the first element,

00:00:28.790 --> 00:00:30.480
we can use the begin function,

00:00:30.480 --> 00:00:33.760
and to get the location of the memory after the last element,

00:00:33.760 --> 00:00:35.750
we can use the end function.

00:00:36.140 --> 00:00:40.720
These two functions provided by the standard array are also known as iterators.

00:00:41.540 --> 00:00:46.280
The other container classes from the standard library implement these iterators,

00:00:46.280 --> 00:00:46.670
too.

00:00:46.670 --> 00:00:51.650
Iterators provide an interface for algorithm functions, like this one.

00:00:52.040 --> 00:00:55.520
They bridge the gap between algorithms and containers,

00:00:55.520 --> 00:00:59.610
because algorithms need to know how to iterate over all of the

00:00:59.610 --> 00:01:02.260
elements inside of a specific container.

00:01:02.840 --> 00:01:04.860
This may seem silly in this case,

00:01:04.860 --> 00:01:07.550
because why doesn't this copy algorithm just know that

00:01:07.550 --> 00:01:10.850
the array starts at the index of 0 and finishes at the

00:01:10.850 --> 00:01:13.550
index of 0 plus the array size?

00:01:13.940 --> 00:01:17.760
But don't forget that these algorithms have to work on all kinds of containers,

00:01:17.760 --> 00:01:21.050
and most of them are not as simple as this basic array.

00:01:21.440 --> 00:01:25.470
By providing a common interface, algorithms can do what they do,

00:01:25.470 --> 00:01:29.560
without the knowledge of the underlying data structure implementation.

00:01:30.740 --> 00:01:35.260
I will define the begin and end functions for the myArray class.

00:01:35.640 --> 00:01:37.740
Iterators are a broad topic.

00:01:37.740 --> 00:01:40.420
There are all kinds of iterator categories: contiguous,

00:01:40.780 --> 00:01:44.960
random access, bi‑directional, forward, and input iterators.

00:01:45.340 --> 00:01:47.620
We could have a whole course on this topic.

00:01:47.630 --> 00:01:51.470
That is why I will treat this lesson as a soft introduction to how we can

00:01:51.470 --> 00:01:55.600
utilize these two basic functions to extend the abilities of the array to

00:01:55.600 --> 00:01:58.560
work with other parts of the standard library.

00:01:59.040 --> 00:02:02.180
The begin function returns a pointer to the first element,

00:02:02.180 --> 00:02:06.960
and the end function returns a pointer to the place after the last element.

00:02:08.039 --> 00:02:11.050
Okay, now let's go down to the main function.

00:02:11.170 --> 00:02:14.370
I will create a new array with three integers.

00:02:14.370 --> 00:02:17.800
Since myArray class now implements these two new functions,

00:02:17.800 --> 00:02:21.130
we can loop over this array with the range‑based for loop,

00:02:21.140 --> 00:02:27.080
which looks much cleaner and shorter than a regular for loop. If I try

00:02:27.080 --> 00:02:29.950
to compile this now, you will see that it works.

00:02:30.640 --> 00:02:35.230
The range‑based for loop will use the iterators in the background to figure

00:02:35.230 --> 00:02:38.160
out how to go through all of the elements of the array.

00:02:39.140 --> 00:02:43.400
This is what the implementation of the slope looks like under the hood. A

00:02:43.400 --> 00:02:46.750
local pointer is initialized to the beginning of the array, and we

00:02:46.750 --> 00:02:50.650
increment this pointer until the address of the pointer matches the end

00:02:50.650 --> 00:02:53.260
of the array. And in each iteration,

00:02:53.260 --> 00:02:57.840
we de‑reference the pointer to get the value of the pointed‑to element, and

00:02:57.840 --> 00:03:01.150
we store that value inside of a placeholder variable.

00:03:01.840 --> 00:03:05.850
The name of this alias is defined here, and that is why we

00:03:05.850 --> 00:03:08.560
can use it as a normal integer variable.

00:03:09.540 --> 00:03:12.140
This is one important use case for iterators.

00:03:12.660 --> 00:03:16.760
Another one is utilizing functions from the algorithm library.

00:03:17.140 --> 00:03:21.960
We already saw how the copy function works. Now, let's pick another one.

00:03:22.340 --> 00:03:24.830
For example, let's use the sort function,

00:03:24.830 --> 00:03:27.160
which will sort the elements from the array.

00:03:28.040 --> 00:03:31.060
We need to pass both iterator functions to determine

00:03:31.060 --> 00:03:33.030
the range inside of the array.

00:03:33.040 --> 00:03:36.130
In this case, we want to work with the whole array,

00:03:36.130 --> 00:03:39.040
and if I compile the program again,

00:03:39.050 --> 00:03:44.040
you can see that elements are now sorted by value. Iterators

00:03:44.040 --> 00:03:47.660
provided an interface for this sort algorithm.
