Hello again! In this video, we are going to look at the standard library array. C++ already has arrays.
These are built into the language. They were inherited from C, in code like that. Built-in arrays are
faster than using vectors, and they are directly compatible with code written in C, but there are some
serious issues in using them.
The most obvious is, if you have an array on the heap, then you need to manage its memory yourself.
But vector will get around
that. If you have an array on the stack,
there are still some issues.
The biggest problem is, there is no direct way to find the number of elements.
If you want to know the number of elements, then either you need to have a variable which stores, it or else
you need to calculate it.
The language has several places where an array has to be converted into a pointer. And the compiler will
do that for you, even if you do not want it to.
In particular, if you pass an array as an argument to a function, or return it from a function call, then
it will be converted to a pointer.
And that means you need to provide another argument with the number of elements.
And also, another limitation is that you are not allowed to assign one built-in array to another.
To overcome these problems, Modern C++ provides the library array. This is in the library namespace std.
And it is in the array header.
It is a templated type with two parameters.
We need to give the type of the element and the number of elements when we create an object.
So this means that the number of elements in the array is part of the type of the object.
If we try to use an array which has the wrong number of elements, then the compiler will not allow
that. The interface is similar to containers from the standard library, but it is still just as fast as
using a built-in array.
It never "decays" into a pointer.
If you have a library array object, it always remains a library array object.
It also has information about its size, the number of elements. And if you want that, you can just
get that, by calling a member function.
And finally, it supports iterators, which makes writing loops a lot simpler.
A library array object can only be created on the stack.
If you want one on the heap, then you can use the standard vector.
The compiler will allocate enough memory to hold all the elements on the stack. That will be a contiguous
block, with no gaps in it.
And that means that the compiler must be able to find out the number of elements, so that must be something
which can be found at compile time.
One difference from other containers: if you default-construct a string or vector, you will get
an empty container.
If you default construct a library array, then you will get an array with the right number of elements.
And that is possible, because the number of elements is part of the type.
These elements are going to be default-initialized.
And if the elements have built-in type, that means their initial value is going to be undefined.
So we need to give the type of the elements and the number of elements, when we create an array object.
We can use a list initializer just like we do for vectors. And then we can use array indexing, just like
we do with the built-in array.
For writing loops, arrrays support iterators, so we can write a traditional iterator loop.
Or a range-for loop. if we want to write an index loop, we can do that as well.
And we are also allowed to assign array, provided the elements have the same type and they have the
same number of elements.
The interface for the array is very similar to the vector, so we can find the number of elements and
whether the array is empty. We can access elements by index, with and without bounds checking.
We can find the first and last element, and we can also get a built-in array, which contains all the
data.
The issue with function calls is that the array is converted to a pointer, so we need to pass the number
of elements as a separate argument.
This means we either need to know the number of elements, or we need to calculate it.
And you can do it like that. Or if you want to get really clever, there is also a template trick.
And then we have to pass the number of elements as the argument. With a library
We can just pass it directly, because the number of elements is part of the type.
Okay, so that is it for this video.
I will see you next time, but until then, keep coding!