Assigning a standard array to another one is very convenient and in this case,
it makes more sense than utilizing the copy function.
However, if we wanted to, we could have used this function on standard arrays,
too.
We just need to find a way to provide the correct pointers
to the first elements of both arrays,
and a pointer to the first memory address after the
last element of the first array.
And that's actually easy.
To get the location of the first element,
we can use the begin function,
and to get the location of the memory after the last element,
we can use the end function.
These two functions provided by the standard array are also known as iterators.
The other container classes from the standard library implement these iterators,
too.
Iterators provide an interface for algorithm functions, like this one.
They bridge the gap between algorithms and containers,
because algorithms need to know how to iterate over all of the
elements inside of a specific container.
This may seem silly in this case,
because why doesn't this copy algorithm just know that
the array starts at the index of 0 and finishes at the
index of 0 plus the array size?
But don't forget that these algorithms have to work on all kinds of containers,
and most of them are not as simple as this basic array.
By providing a common interface, algorithms can do what they do,
without the knowledge of the underlying data structure implementation.
I will define the begin and end functions for the myArray class.
Iterators are a broad topic.
There are all kinds of iterator categories: contiguous,
random access, bi‑directional, forward, and input iterators.
We could have a whole course on this topic.
That is why I will treat this lesson as a soft introduction to how we can
utilize these two basic functions to extend the abilities of the array to
work with other parts of the standard library.
The begin function returns a pointer to the first element,
and the end function returns a pointer to the place after the last element.
Okay, now let's go down to the main function.
I will create a new array with three integers.
Since myArray class now implements these two new functions,
we can loop over this array with the range‑based for loop,
which looks much cleaner and shorter than a regular for loop. If I try
to compile this now, you will see that it works.
The range‑based for loop will use the iterators in the background to figure
out how to go through all of the elements of the array.
This is what the implementation of the slope looks like under the hood. A
local pointer is initialized to the beginning of the array, and we
increment this pointer until the address of the pointer matches the end
of the array. And in each iteration,
we de‑reference the pointer to get the value of the pointed‑to element, and
we store that value inside of a placeholder variable.
The name of this alias is defined here, and that is why we
can use it as a normal integer variable.
This is one important use case for iterators.
Another one is utilizing functions from the algorithm library.
We already saw how the copy function works. Now, let's pick another one.
For example, let's use the sort function,
which will sort the elements from the array.
We need to pass both iterator functions to determine
the range inside of the array.
In this case, we want to work with the whole array,
and if I compile the program again,
you can see that elements are now sorted by value. Iterators
provided an interface for this sort algorithm.