Hello again! In this video, we are going to continue looking at searching algorithms. The mismatch
algorithm takes two iterator ranges, and it will try to find the first element in the ranges, which
is different. If, for example, we give it these two vectors,
it will notice that the element at the fourth index is different, and it will return a pair of iterators.
The first member of this pair will be an iterator to the element in vector one.
And the second member will be an iterator to the element in vector two.
So here we have those two vectors.
We have a simple function for printing them out.
And then we call the mismatch algorithm. So we give the iterator range for vec1 and the
iterator range for vec2.
And this will find the first mismatched element in the range.
If the algorithm gets to the end of either one of the vectors, without finding a discrepancy, then
it will put the end iterator in the member for that vector.
So there we are.
It found the mismatch at index 3. And the element in vec1 is 3 and the element in vec2
is 2.
And we just did that by getting the iterators from the pair and then dereferencing them.
There are three algorithms which will take an iterator range and a predicate callable object, and
they will check whether this predicate is true for some of the elements, all of the elements or none of
the elements.
So all_of() will return true if the predicate is true for every element.
none_of()
will return
true, if the predicate is false for every elements. And any_of() will return true if the predicate
is true for some of the elements.
So here is our vector.
We have two lambda expressions, one for an odd elemen and one for an even element.
And we use those as the predicates for those functions.
So we have all the permutations.
So if all the elements are odd, then we expect to see that. If they are all even we expect to see that.
If some elements are odd - and so on.
So with this vector, some elements are odd and some elements are even. If I change this, so all the
elements are odd, then we will get a different result.
And there we are. All the elements are odd, some elements are odd. So this will return true if all
are as well, and no elements in this vector are even.
And just to make sure, let's do it for even numbers.
OK, I am sure you get the idea!
We all know about the find() algorithm, but if you have elements which are already sorted, then find
is not the most optimal algorithm.
You can actually use a much better one, which is much faster, and that is binary search.
So this works the same way. We give it the iterator range, and the thing that we looking for.
And it returns true if the range contains that value or that object.
So we have our vector again.
We sort it before we call binary search, because otherwise the binary search will not give the
right answer.
And then we call it. So we give the iterator range of the vector and we are looking for the element
with value 4.
So the vector contains 4, and it does not contain 8.
And finally, the includes algorithm.
This takes two iterator ranges.
Again, it assumes that the iterator ranges are sorted.
This will be return a bool
If the first range contains all the elements in the second range, this will return the true, otherwise
false.
So let's look at this.
So we are going to use strings now. Our famous
"Hello world" string and also the vowels string.
So we sort the strings first, because the algorithm expects that, and then we call includes(). we give
the iterator range for "Hello world" and the iterator range for vowels.
So if "Hello world" contains every character in vowels at least once, this will return true.
So what do you think we'll get?
So these are the sorted strings that the algorithm will work on. And "Hello world" does not contain
all the characters in vowels.
If we replace this with a string which does contain all the vowels, then we should get a different
answer.
And there we are, so now we do have all the vowels in our search string.
Okay, so that's it for this video.
I'll see you next time, but until then, keep coding!