WEBVTT

00:00.060 --> 00:05.610
Hello again! In this video, we are going to look at copying algorithms. A copying algorithm

00:05.610 --> 00:06.720
does what you might expect.

00:07.050 --> 00:10.320
It will copy elements from one iterator range into another one.

00:11.070 --> 00:14.550
The main use for this is for populating sequential containers.

00:15.000 --> 00:20.610
So we have one container with some data in it, and we can use that data to populate an empty container.

00:22.230 --> 00:24.420
The basic algorithm is called copy().

00:24.900 --> 00:25.260
We give

00:25.260 --> 00:28.800
it the iterator range of the container that we are copying from.

00:29.250 --> 00:33.330
And then an iterator range in the container where we want to copy the data to.

00:34.140 --> 00:38.600
This assumes that the vector is the right size, or that you are using an insert

00:38.640 --> 00:39.240
iterator.

00:42.480 --> 00:44.640
So here is some code for printing that out.

00:46.080 --> 00:51.960
We have a vector that we are going to copy, and then we have our call to copy().

00:52.710 --> 00:55.290
So the first version with a vector of the correct size.

00:56.070 --> 00:59.430
Then we have another attempt with a insert iterator.

01:00.120 --> 01:02.460
And then finally, we use a handwritten loop.

01:03.090 --> 01:04.740
So these should all give the same answers.

01:05.850 --> 01:06.300
Okay.

01:08.870 --> 01:14.630
There's also a copy_n() algorithm, which will copy the first "n" elements from the range.

01:15.290 --> 01:19.310
So we can have copy_n(), the start of the iterator range,

01:19.580 --> 01:21.980
and then the number of elements that we want to copy.

01:22.670 --> 01:26.900
So that is equivalent to just leaping over the first two elements of the vector.

01:29.530 --> 01:36.610
And then, using the same vector again, we call copy_n() to copy two elements, and

01:36.610 --> 01:38.380
we also have the handwritten equivalent.

01:40.540 --> 01:41.110
So there we are.

01:41.350 --> 01:48.080
We only get the first two elements of the vector copied. And the final algorithm is

01:48.140 --> 01:54.940
copy_if(). So, this is a version of copy(), which will only do the copy if a predicate returns true.

01:55.810 --> 01:58.900
So we call this with the same arguments and a callable object.

01:59.490 --> 02:04.720
We are going to use our favourite lambda expression, which returns true for odd numbers and false for

02:04.720 --> 02:05.470
even numbers.

02:06.100 --> 02:11.830
And this will only copy the odd numbers from "vec" into "vec2". The even numbers will be ignored.

02:15.030 --> 02:22.650
And there is our call to copy_if(). And if we were doing this by hand, then we would have an if statement

02:22.920 --> 02:23.880
which does the same thing.

02:26.160 --> 02:30.480
So we get the odd numbers copied, but the even number is ignored.

02:32.430 --> 02:36.480
So you can think of copy_if() as acting as a kind of filter between the two vectors.

02:37.350 --> 02:45.180
So we have the data in "vec" and it gets passed into "vec2", but only if it meets this criterion.

02:45.780 --> 02:48.000
If it does not meet the criterion, it gets filtered out.

02:48.690 --> 02:53.570
So here, we are filtering out the even numbers, but you could do anything you want to really. So

02:53.570 --> 02:57.000
you can apply whatever filter is needed for your problem.

02:58.200 --> 02:59.760
Okay, so that is it for this video.

03:00.180 --> 03:03.060
I'll see you next time, but until then, keep coding!
