C++ STL algorithm - std::mismatch | Modern Cpp Series Ep. 144

preview_player
Показать описание

►Lesson Description: In this lesson we discuss the mismatch algorithm which reports back the first pair of mismatches. When comparing two sequences, if they are not the same size, then their is some undefined behavior, so we'll right a little helper function to deal with that. This is a nice example of how the algorithm building blocks can help us write safer code and code that is more resilient.

►Please like and subscribe to help the channel!
Рекомендации по теме
Комментарии
Автор

If two vectors match, aren't .fist and .second both == .end() of the corresponding vectors? If it is so, line 15 is problematic (deref of .end())! It prints zeros for matching vectors -- that is suspicious. What would you function do if it were given two empty vectors?
Thank you.

rudalert
Автор

Thanks professor for the video, well I will try to use this mismatch function in user defined data structure, I guess I have to use custom comparator

monty
Автор

At the 8:30 mark in the video, on line 10 due to an "out of range vector invalidation", this code will work in Visual Studio 2022 as: auto result1 = std::mismatch(v1.begin(), v2.end() - 1, v2.begin());

MrTNVolsFan
Автор

Also on line 19. Notice the -1 added at the end of v1.end(). Hope this helps with out-of-range invalidation iterators errors in vector data structures!

MrTNVolsFan
Автор

9:11 is UB; you're dereferencing the end iterator.

masheroz
join shbcf.ru