WEBVTT

00:00.150 --> 00:04.140
Hello again! In this video, we are going to look at min and max algorithms.

00:05.340 --> 00:10.830
The standard library provides quite a few algorithms for finding the largest or smallest elements,

00:10.830 --> 00:11.850
or sometimes both.

00:12.540 --> 00:19.410
There are various functions which can take two arguments, an initializer list in C++11, or the

00:19.410 --> 00:20.850
traditional iterator range.

00:21.780 --> 00:27.300
By default, these will use the less-than operator of the element, or the argument, to find out the

00:27.300 --> 00:28.380
largest or smallest.

00:28.770 --> 00:32.310
But we can provide our own comparison operator, as a predicate.

00:33.150 --> 00:37.620
And I think, probably, rather than having lots of very similar slides, the best way to demonstrate this

00:37.620 --> 00:38.580
is to have a program.

00:39.840 --> 00:42.930
So here it is. We start off with a couple of string variables.

00:43.920 --> 00:47.640
The less-than operator will do alphabetical comparisons.

00:48.060 --> 00:54.030
So "first_word" will be less than "word2". The basic functions

00:54.030 --> 00:56.310
are just max and min with two arguments.

00:56.640 --> 00:59.280
And these will return the largest or the smallest element.

01:01.850 --> 01:07.670
We can also provide a predicate, for example, if we want to use the size of the string and not the

01:08.030 --> 01:14.660
alphabetical order of the characters. We can pass an initialize the list, and this will return

01:14.660 --> 01:17.510
the largest or the smallest element in the list.

01:18.830 --> 01:23.090
So we would expect to get "words" for the max(), and "collection" for the min().

01:25.950 --> 01:32.040
There is minmax(), which will return a pair of values. So this will find both the minimum and the maximum.

01:32.610 --> 01:35.610
You can either give it 2 arguments or an initializer list.

01:36.780 --> 01:43.930
The first member of the returned pair will be the smallest argument or element, and the second member

01:43.980 --> 01:44.880
will be the largest.

01:47.260 --> 01:50.200
min_element() and max_element() will take an iterator range.

01:50.620 --> 01:57.280
We are going to use this vector as a container, to provide the iterator range. Then we pass the iterator

01:57.280 --> 01:58.870
range to max_element().

01:59.410 --> 02:02.760
And this will return an [iterator] to the largest element in here.

02:02.770 --> 02:04.600
So that will be the "words".

02:06.520 --> 02:09.290
And again, we can provide a predicate if we want to.

02:09.310 --> 02:14.350
So this will return an iterator to the shortest element in the iterator range, which will

02:14.350 --> 02:15.340
be the letter 'a'.

02:17.830 --> 02:22.780
And then finally, we have minimax_element(), which combines minmax() and these two functions.

02:23.350 --> 02:25.990
So this will take an iterator range, like these two do.

02:26.470 --> 02:29.620
And it will return a pair of iterators, like minmax() does.

02:30.430 --> 02:35.230
And then the first member of the pair will be an iterator to the smallest element, and the second member

02:35.230 --> 02:37.540
will be an iterator to the largest element.

02:39.070 --> 02:40.300
So let's see what we get.

02:43.120 --> 02:48.880
So "word2" is greater than "first_word", "first_word" has more characters than "word2", and we get the

02:48.880 --> 02:50.320
expected values from those.

02:52.380 --> 02:58.500
Okay, so you may not find all of these useful, but they are there. Probably the most useful is just the version

02:58.500 --> 03:03.840
which takes two arguments and then the initializer list as well, perhaps.

03:05.010 --> 03:06.780
Okay, so that is it for this video.

03:07.140 --> 03:08.010
I will see you next time.

03:08.250 --> 03:10.080
Until then, keep coding!
