Home C++ Algorithms Functions mismatch() C++ std::mismatch() Function Definition In C++, std::mismatch() walks through two ranges side by side and stops at the first place where the elements are not equal. It returns a pair of iterators, one for each range.
It is a quick way to measure a common prefix, or to show exactly where two sequences start to differ.
Syntax Add #include <algorithm>, then call std::mismatch(). Start with these two lines.
std::mismatch(a.begin(), a.end(), b.begin());
std::mismatch(a.begin(), a.end(), b.begin(), b.end()); Call What it does std::mismatch(a.begin(), a.end(), b.begin());Compare a with b. b must be at least as long. std::mismatch(a.begin(), a.end(), b.begin(), b.end());Safe version for ranges of different length (C++14).
Library declarations template<class InputIt1, class InputIt2>
std::pair<InputIt1, InputIt2>
mismatch(InputIt1 first1, InputIt1 last1, InputIt2 first2);
// C++14: both ranges are bounded
template<class InputIt1, class InputIt2>
std::pair<InputIt1, InputIt2>
mismatch(InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2); Parameters Parameter Description first1, last1 Required. The first range. first2, last2 The second range. The version with only first2 assumes it is long enough; the four-argument version (C++14) is safer. pred Optional. A function that decides whether two elements are equal.
Return Value Return Value Description std::pairfirst points into the first range and second into the second range, both at the first difference.End iterators If no difference is found, first equals last1.
C++ Examples 1. First difference 2. Common prefix 3. Identical ranges Example 1 — First difference Show the first place where two vectors differ.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> a = {1, 2, 3, 4};
std::vector<int> b = {1, 2, 9, 4};
auto p = std::mismatch(a.begin(), a.end(), b.begin());
if (p.first != a.end()) {
std::cout << "First difference at index " << (p.first - a.begin())
<< ": " << *p.first << " vs " << *p.second << '\n';
}
return 0;
} First difference at index 2: 3 vs 9 How It Works mismatch() compares a[0] with b[0], a[1] with b[1], and so on.The first two pairs match. The third pair is 3 against 9. p.first points into a and p.second points into b at that spot.Example 2 — Common prefix Count how many letters two words share at the start.
#include <algorithm>
#include <iostream>
#include <string>
int main() {
std::string s1 = "codetofun";
std::string s2 = "codebase";
auto p = std::mismatch(s1.begin(), s1.end(), s2.begin(), s2.end());
std::cout << "Common prefix length: " << (p.first - s1.begin()) << '\n';
return 0;
} How It Works The four-argument version stops safely at the end of the shorter word. code is the same in both words. Then t meets b.The distance from the start is 4, the length of the shared prefix. Example 3 — Identical ranges See what a full match looks like.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> a = {5, 6, 7};
std::vector<int> b = {5, 6, 7};
auto p = std::mismatch(a.begin(), a.end(), b.begin());
if (p.first == a.end()) {
std::cout << "Ranges match completely\n";
}
return 0;
} How It Works Every pair of elements is equal, so the loop never stops early. The returned first is then equal to a.end(). Checking p.first == a.end() is how you detect that there is no difference. Frequently Asked Questions What does std::mismatch() do? In C++, std::mismatch() finds the first position where two ranges differ. Include <algorithm> to use it.
Which header and C++ version do I need for std::mismatch()? Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
What does std::mismatch() return? It returns a pair of iterators pointing at the first position where the two ranges differ.
How is mismatch() different from equal()? equal() only says true or false. mismatch() also tells you where the first difference is.
What does mismatch() return? A std::pair of iterators, one for each range, both pointing at the first position where the values are not equal.
Next: move() Moves the elements of a range into another place instead of copying them.
move() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
18 people found this page helpful
Helpful Share Copy link Suggestion