WEBVTT

00:00.180 --> 00:07.050
Hello again! In this video, we are going to look at the standard library array. C++ already has arrays.

00:07.080 --> 00:14.400
These are built into the language. They were inherited from C, in code like that. Built-in arrays are

00:14.430 --> 00:19.740
faster than using vectors, and they are directly compatible with code written in C, but there are some

00:19.740 --> 00:22.110
serious issues in using them.

00:23.100 --> 00:27.960
The most obvious is, if you have an array on the heap, then you need to manage its memory yourself.

00:28.440 --> 00:29.700
But vector will get around

00:29.700 --> 00:32.160
that. If you have an array on the stack,

00:32.160 --> 00:33.210
there are still some issues.

00:33.930 --> 00:37.430
The biggest problem is, there is no direct way to find the number of elements.

00:38.340 --> 00:43.710
If you want to know the number of elements, then either you need to have a variable which stores, it or else

00:43.710 --> 00:44.970
you need to calculate it.

00:46.920 --> 00:52.350
The language has several places where an array has to be converted into a pointer. And the compiler will

00:52.350 --> 00:54.660
do that for you, even if you do not want it to.

00:55.740 --> 01:00.960
In particular, if you pass an array as an argument to a function, or return it from a function call, then

01:00.960 --> 01:02.490
it will be converted to a pointer.

01:03.090 --> 01:06.390
And that means you need to provide another argument with the number of elements.

01:07.290 --> 01:12.390
And also, another limitation is that you are not allowed to assign one built-in array to another.

01:12.930 --> 01:21.180
To overcome these problems, Modern C++ provides the library array. This is in the library namespace std.

01:21.870 --> 01:23.640
And it is in the array header.

01:24.600 --> 01:26.880
It is a templated type with two parameters.

01:26.880 --> 01:31.560
We need to give the type of the element and the number of elements when we create an object.

01:32.460 --> 01:36.360
So this means that the number of elements in the array is part of the type of the object.

01:36.960 --> 01:41.820
If we try to use an array which has the wrong number of elements, then the compiler will not allow

01:41.820 --> 01:48.840
that. The interface is similar to containers from the standard library, but it is still just as fast as

01:48.840 --> 01:49.860
using a built-in array.

01:51.240 --> 01:52.980
It never "decays" into a pointer.

01:53.340 --> 01:58.080
If you have a library array object, it always remains a library array object.

01:58.950 --> 02:03.420
It also has information about its size, the number of elements. And if you want that, you can just

02:03.420 --> 02:05.220
get that, by calling a member function.

02:07.010 --> 02:10.460
And finally, it supports iterators, which makes writing loops a lot simpler.

02:12.290 --> 02:15.170
A library array object can only be created on the stack.

02:15.590 --> 02:18.980
If you want one on the heap, then you can use the standard vector.

02:20.510 --> 02:25.250
The compiler will allocate enough memory to hold all the elements on the stack. That will be a contiguous

02:25.250 --> 02:26.390
block, with no gaps in it.

02:26.960 --> 02:32.330
And that means that the compiler must be able to find out the number of elements, so that must be something

02:32.330 --> 02:33.770
which can be found at compile time.

02:35.540 --> 02:40.250
One difference from other containers: if you default-construct a string or vector, you will get

02:40.250 --> 02:41.120
an empty container.

02:41.840 --> 02:47.510
If you default construct a library array, then you will get an array with the right number of elements.

02:47.780 --> 02:50.450
And that is possible, because the number of elements is part of the type.

02:51.410 --> 02:53.810
These elements are going to be default-initialized.

02:54.110 --> 02:58.910
And if the elements have built-in type, that means their initial value is going to be undefined.

03:01.510 --> 03:07.720
So we need to give the type of the elements and the number of elements, when we create an array object.

03:08.680 --> 03:14.920
We can use a list initializer just like we do for vectors. And then we can use array indexing, just like

03:14.920 --> 03:16.030
we do with the built-in array.

03:19.350 --> 03:25.150
For writing loops, arrrays support iterators, so we can write a traditional iterator loop.

03:27.740 --> 03:32.540
Or a range-for loop. if we want to write an index loop, we can do that as well.

03:33.380 --> 03:38.240
And we are also allowed to assign array, provided the elements have the same type and they have the

03:38.240 --> 03:39.500
same number of elements.

03:44.480 --> 03:50.090
The interface for the array is very similar to the vector, so we can find the number of elements and

03:50.090 --> 03:56.780
whether the array is empty. We can access elements by index, with and without bounds checking.

03:58.980 --> 04:04.680
We can find the first and last element, and we can also get a built-in array, which contains all the

04:04.680 --> 04:05.130
data.

04:06.390 --> 04:11.850
The issue with function calls is that the array is converted to a pointer, so we need to pass the number

04:11.850 --> 04:13.410
of elements as a separate argument.

04:15.390 --> 04:19.410
This means we either need to know the number of elements, or we need to calculate it.

04:20.010 --> 04:24.060
And you can do it like that. Or if you want to get really clever, there is also a template trick.

04:25.770 --> 04:30.300
And then we have to pass the number of elements as the argument. With a library

04:30.330 --> 04:34.980
We can just pass it directly, because the number of elements is part of the type.

04:42.420 --> 04:43.800
Okay, so that is it for this video.

04:44.460 --> 04:47.940
I will see you next time, but until then, keep coding!
