C++ std::mismatch() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

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.

C++
std::mismatch(a.begin(), a.end(), b.begin());
std::mismatch(a.begin(), a.end(), b.begin(), b.end());
CallWhat 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
C++
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

ParameterDescription
first1, last1Required. The first range.
first2, last2The second range. The version with only first2 assumes it is long enough; the four-argument version (C++14) is safer.
predOptional. A function that decides whether two elements are equal.

Return Value

Return ValueDescription
std::pairfirst points into the first range and second into the second range, both at the first difference.
End iteratorsIf no difference is found, first equals last1.

C++ Examples

Example 1 — First difference

Show the first place where two vectors differ.

C++
#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;
}

How It Works

  1. mismatch() compares a[0] with b[0], a[1] with b[1], and so on.
  2. The first two pairs match. The third pair is 3 against 9.
  3. 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.

C++
#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

  1. The four-argument version stops safely at the end of the shorter word.
  2. code is the same in both words. Then t meets b.
  3. The distance from the start is 4, the length of the shared prefix.

Example 3 — Identical ranges

See what a full match looks like.

C++
#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

  1. Every pair of elements is equal, so the loop never stops early.
  2. The returned first is then equal to a.end().
  3. Checking p.first == a.end() is how you detect that there is no difference.

Frequently Asked Questions

In C++, std::mismatch() finds the first position where two ranges differ. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns a pair of iterators pointing at the first position where the two ranges differ.
equal() only says true or false. mismatch() also tells you where the first difference is.
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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