std::mismatch() in C++ with examples

In this tutorial, we are going to learn about a useful function of C++ STL that is the mismatch() function. This function is defined in the ‘algorithm’ header in C++. This function helps us to find mismatches in elements of two containers. Let’s read in detail about it further in this tutorial.

std::mismatch() in C++

There are two versions of the std::mismatch() function. We will discuss them one by one.

mismatch(iter1_begin, iter1_end, iter2_begin())

In the above syntax of mismatch() function, there are three parameters. The first two parameters define the boundary of the first container i.e. start and last iterator. The third parameter is the starting iterator for the second iterator.

The function returns a pair pointer where the first element of the pair points to the position of the first mismatched element of the first container and the second element of the pair points to the position of the first mismatched element of the second container. If no mismatch is found, then the resulting pair points to the position after the last element of the first container and the position of the corresponding element in the second container, respectively.

Have a look at the below code for a thorough understanding of the above.

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main()
{
  vector<int> vector1= {10, 20, 30, 40};
  vector<int> vector2= {10, 20, 33, 40};
  
  vector<int> vector3= {10, 20, 30};
  vector<int> vector4= {10, 20, 30, 40, 50};
  
  pair< vector<int>::iterator,  vector<int>::iterator > mismatch_pair; 
  
  mismatch_pair = mismatch(vector1.begin(), vector1.end(), vector2.begin());
  
  cout << "The first mismatch pair from vector1 and vector2 is: ";
  cout << *mismatch_pair.first << " " << *mismatch_pair.second << endl;
  
  mismatch_pair = mismatch(vector3.begin(), vector3.end(), vector4.begin());
  
  cout << "The first mismatch pair from vector3 and vector4 is: ";
  cout << *mismatch_pair.first << " " << *mismatch_pair.second;
  
  return 0;
}

Output:

The first mismatch pair from vector1 and vector2 is: 30 33
The first mismatch pair from vector3 and vector4 is: 0 40

mismatch(iter1_begin, iter1_end, iter2_begin(), predicate_function)

This is almost similar to the above version of the mismatch() function. Here, we take a fourth argument which is a predicate function. This is used to obtain user-defined mismatch. The predicate function is basically a bololean function and it returns either true or false.

See the below given code.

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

bool predicate_function(int a, int b)
{
    return (a < b);
}

int main()
{
  vector<int> vector1= {10, 20, 30, 40};
  vector<int> vector2= {11, 22, 33, 40};
  
  vector<int> vector3= {10, 20, 30};
  vector<int> vector4= {11, 22, 33, 44, 55};
  
  pair< vector<int>::iterator,  vector<int>::iterator > mismatch_pair; 
  
  mismatch_pair = mismatch(vector1.begin(), vector1.end(), vector2.begin(), predicate_function);
  
  cout << "The first mismatch pair from vector1 and vector2 is: ";
  cout << *mismatch_pair.first << " " << *mismatch_pair.second << endl;
  
  mismatch_pair = mismatch(vector3.begin(), vector3.end(), vector4.begin(), predicate_function);
  
  cout << "The first mismatch pair from vector3 and vector4 is: ";
  cout << *mismatch_pair.first << " " << *mismatch_pair.second;
  
  return 0;
}

Output:

The first mismatch pair from vector1 and vector2 is: 40 40
The first mismatch pair from vector3 and vector4 is: 0 44

Also read: std::distance() in C++

Leave a Reply

Your email address will not be published. Required fields are marked *