Hello again! In this video, we are going to have a practical demonstration of lambda expressions.
I am going to take the equal_strings function we wrote earlier in the course and rewrite that, using
an algorithm and a lambda expression.
We wrote the equal_strings function in the video on Character Functions. It takes two string arguments,
and it compares each character in the string, after converting them to uppercase.
So here is the code, which we have seen before.
We have a test() function and a main().
So this will ignore differences in the case, but it will not ignore different characters.
We are going to use the equal() algorithm.
This compares the elements in two containers.
It takes 4 arguments: an iterator range for the first container, and an iterator range for the
second container, and it returns bool.
This will go through and compare each element in the first container with the corresponding element
in the second container. As soon as it encounters a discrepancy - a mismatch - it returns false. If it gets
through the first container without finding any mismatches,
then it returns true.
If the first container has more elements, it returns false, regardless of the data.
If the second container has more elements, then those extra elements are ignored.
By default, this will use the equality operator to compare the elements. And if the containers are
strings, then that means it is going to do a case-sensitive comparison of each character in the string.
There is an overload of the equal algorithm, which takes a callable object as an optional extra arguments.
And we can use this predicate to change the way that the equal() function compares the elements.
This predicate is going to be called instead of the equality operator so we can make this do whatever
we want for - to change the criteria for equality.
In our example, we want two characters to be equal if they have the same value after being converted
to uppercase.
The lambda expression is going to be called with one character from each of the two strings. Then we
convert each character to uppercase. And then we compare them, and we return true or false, depending
on whether they are equal or not.
And that is it!
So here is the version with the lambda expression. You will notice this is much shorter.
The entire program fits on a single screen (almost!), whereas before the equal_string() function took up
an entire screen.
So here is the equal strings function,
again. It just calls the equal() algorithm.
So we pass the iterator range for the two string arguments.
The first string and the second string argument.
Then we have our lambda expression, which takes a character from each string. And then, in the body,
we convert the characters to uppercase and compare them. And the rest of the program is the same.
And there we are.
So the advantage of this is, we replaced a screen full of code, with lots of things that could go wrong, with
just one simple statement.
And perhaps this statement is quite complex if you are not used to it, but it does become second nature.
You can actually format it slightly differently, to make it a bit easier to read. Depending on your
style.
OK, so that is it for this video.
I will see you next time, but until then, keep coding!