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

06 Jul, 2017
C++ STL has lots of useful functions that helps us to achieve various programming tasks. One such function is “mismatch()” . This function, defined in “algorithm” header file, helps to compare 2 containers for mismatches. This function has 2 versions. Both are discussed in this article.
mismatch( start_iter1, end_iter1, start_iter2 ) This version of mismatch only test for inequality.Here, there are 3 arguments,start_iter1: Beginning iterator to 1st containerend_iter1: Last iterator to 1st containerstart_iter2: Starting iterator to the 2nd iterator from where comparison is desired to begin.This function returns the 1st mismatch pair pointer, first element pointing to position of first mismatch element of 1st container, second element pointing to position of first mismatch element of 2nd container. If no mismatch is found, 1st element points to position after last element of 1st container and 2nd points to corresponding position in 2nd container.// C++ code to demonstrate the working of // mismatch( start_iter1, end_iter1, start_iter2 )  #include<iostream>#include<algorithm> #include<vector>using namespace std;  int main(){          // initializing vectors    vector<int> v1 = { 1, 10, 15, 20 };    vector<int> v2 = { 1, 10, 25, 30, 45 };    vector<int> v3 = { 1, 10, 15, 20 };    vector<int> v4 = { 1, 10, 15, 20, 24 };          // declaring pointer pair    pair< vector<int>::iterator,    vector<int>::iterator > mispair;          // using mismatch() to search for 1st mismatch    mispair = mismatch(v1.begin(), v1.end(), v2.begin());          // printing the mismatch pair    // 1st mismatch at 15 and 25    cout << "The 1st mismatch element of 1st container : ";    cout << *mispair.first << endl;          cout << "The 1st mismatch element of 2nd container : ";    cout << *mispair.second << endl;          // using mismatch() to search for 1st mismatch    mispair = mismatch(v3.begin(), v3.end(), v4.begin());          // printing the mismatch pair    // no mismatch    // points to position after last 0 and corresponding 24    cout << "The returned value from 1st container is : ";    cout << *mispair.first << endl;          cout << "The returned value from 2nd container is : ";    cout << *mispair.second << endl;      }Output:The 1st mismatch element of 1st container : 15
The 1st mismatch element of 2nd container : 25
The returned value from 1st container is  : 0
The returned value from 2nd container is  : 24
mismatch( start_iter1, end_iter1, start_iter2, comparator) : This function is almost similar to the working as the above mentioned version, but it offers to find not only equality mismatches, but also other user-defined and desired mismatches via user- defined comparator function that is sent as 4th argument and returns a boolean true or false.// C++ code to demonstrate the working of// mismatch( start_iter1, end_iter1, start_iter2, comparator )  #include<iostream>#include<algorithm> #include<vector>using namespace std;  // comparator function// returns true when element from // 1st element is greater than 2ndbool compare(int a, int b){       return (a>b);}  int main(){          // initializing vectors    vector<int> v1 = { 23, 13, 15, 20 };    vector<int> v2 = { 1, 10, 25, 30, 45 };    vector<int> v3 = { 12, 100, 152, 204 };    vector<int> v4 = { 1, 10, 15, 20, 24 };          // declaring pointer pair    pair< vector<int>::iterator,    vector<int>::iterator > mispair;          // using mismatch() to search for 1st mismatch    mispair = mismatch(v1.begin(), v1.end(), v2.begin(), compare);          // printing the mismatch pair    // 1st mismatch at 15 and 25    // 15 is 1st element less than 2nd at same position    cout << "The 1st mismatch element of 1st container : ";    cout << *mispair.first << endl;          cout << "The 1st mismatch element of 2nd container : ";    cout << *mispair.second << endl;          // using mismatch() to search for 1st mismatch    mispair = mismatch(v3.begin(), v3.end(), v4.begin(), compare);          // printing the mismatch pair    // no mismatch    // all elements in 1st container are greater than 2nd    // points to position after last 0 and corresponding 24    cout << "The returned value from 1st container is  : ";    cout << *mispair.first << endl;          cout << "The returned value from 2nd container is  : ";    cout << *mispair.second << endl;      }Output:The 1st mismatch element of 1st container : 15
The 1st mismatch element of 2nd container : 25
The returned value from 1st container is  : 0
The returned value from 2nd container is  : 24

mismatch( start_iter1, end_iter1, start_iter2 ) This version of mismatch only test for inequality.Here, there are 3 arguments,start_iter1: Beginning iterator to 1st containerend_iter1: Last iterator to 1st containerstart_iter2: Starting iterator to the 2nd iterator from where comparison is desired to begin.This function returns the 1st mismatch pair pointer, first element pointing to position of first mismatch element of 1st container, second element pointing to position of first mismatch element of 2nd container. If no mismatch is found, 1st element points to position after last element of 1st container and 2nd points to corresponding position in 2nd container.// C++ code to demonstrate the working of // mismatch( start_iter1, end_iter1, start_iter2 )  #include<iostream>#include<algorithm> #include<vector>using namespace std;  int main(){          // initializing vectors    vector<int> v1 = { 1, 10, 15, 20 };    vector<int> v2 = { 1, 10, 25, 30, 45 };    vector<int> v3 = { 1, 10, 15, 20 };    vector<int> v4 = { 1, 10, 15, 20, 24 };          // declaring pointer pair    pair< vector<int>::iterator,    vector<int>::iterator > mispair;          // using mismatch() to search for 1st mismatch    mispair = mismatch(v1.begin(), v1.end(), v2.begin());          // printing the mismatch pair    // 1st mismatch at 15 and 25    cout << "The 1st mismatch element of 1st container : ";    cout << *mispair.first << endl;          cout << "The 1st mismatch element of 2nd container : ";    cout << *mispair.second << endl;          // using mismatch() to search for 1st mismatch    mispair = mismatch(v3.begin(), v3.end(), v4.begin());          // printing the mismatch pair    // no mismatch    // points to position after last 0 and corresponding 24    cout << "The returned value from 1st container is : ";    cout << *mispair.first << endl;          cout << "The returned value from 2nd container is : ";    cout << *mispair.second << endl;      }Output:The 1st mismatch element of 1st container : 15
The 1st mismatch element of 2nd container : 25
The returned value from 1st container is  : 0
The returned value from 2nd container is  : 24

This function returns the 1st mismatch pair pointer, first element pointing to position of first mismatch element of 1st container, second element pointing to position of first mismatch element of 2nd container. If no mismatch is found, 1st element points to position after last element of 1st container and 2nd points to corresponding position in 2nd container.
// C++ code to demonstrate the working of // mismatch( start_iter1, end_iter1, start_iter2 )  #include<iostream>#include<algorithm> #include<vector>using namespace std;  int main(){          // initializing vectors    vector<int> v1 = { 1, 10, 15, 20 };    vector<int> v2 = { 1, 10, 25, 30, 45 };    vector<int> v3 = { 1, 10, 15, 20 };    vector<int> v4 = { 1, 10, 15, 20, 24 };          // declaring pointer pair    pair< vector<int>::iterator,    vector<int>::iterator > mispair;          // using mismatch() to search for 1st mismatch    mispair = mismatch(v1.begin(), v1.end(), v2.begin());          // printing the mismatch pair    // 1st mismatch at 15 and 25    cout << "The 1st mismatch element of 1st container : ";    cout << *mispair.first << endl;          cout << "The 1st mismatch element of 2nd container : ";    cout << *mispair.second << endl;          // using mismatch() to search for 1st mismatch    mispair = mismatch(v3.begin(), v3.end(), v4.begin());          // printing the mismatch pair    // no mismatch    // points to position after last 0 and corresponding 24    cout << "The returned value from 1st container is : ";    cout << *mispair.first << endl;          cout << "The returned value from 2nd container is : ";    cout << *mispair.second << endl;      }
Output:
The 1st mismatch element of 1st container : 15
The 1st mismatch element of 2nd container : 25
The returned value from 1st container is  : 0
The returned value from 2nd container is  : 24

mismatch( start_iter1, end_iter1, start_iter2, comparator) : This function is almost similar to the working as the above mentioned version, but it offers to find not only equality mismatches, but also other user-defined and desired mismatches via user- defined comparator function that is sent as 4th argument and returns a boolean true or false.// C++ code to demonstrate the working of// mismatch( start_iter1, end_iter1, start_iter2, comparator )  #include<iostream>#include<algorithm> #include<vector>using namespace std;  // comparator function// returns true when element from // 1st element is greater than 2ndbool compare(int a, int b){       return (a>b);}  int main(){          // initializing vectors    vector<int> v1 = { 23, 13, 15, 20 };    vector<int> v2 = { 1, 10, 25, 30, 45 };    vector<int> v3 = { 12, 100, 152, 204 };    vector<int> v4 = { 1, 10, 15, 20, 24 };          // declaring pointer pair    pair< vector<int>::iterator,    vector<int>::iterator > mispair;          // using mismatch() to search for 1st mismatch    mispair = mismatch(v1.begin(), v1.end(), v2.begin(), compare);          // printing the mismatch pair    // 1st mismatch at 15 and 25    // 15 is 1st element less than 2nd at same position    cout << "The 1st mismatch element of 1st container : ";    cout << *mispair.first << endl;          cout << "The 1st mismatch element of 2nd container : ";    cout << *mispair.second << endl;          // using mismatch() to search for 1st mismatch    mispair = mismatch(v3.begin(), v3.end(), v4.begin(), compare);          // printing the mismatch pair    // no mismatch    // all elements in 1st container are greater than 2nd    // points to position after last 0 and corresponding 24    cout << "The returned value from 1st container is  : ";    cout << *mispair.first << endl;          cout << "The returned value from 2nd container is  : ";    cout << *mispair.second << endl;      }Output:The 1st mismatch element of 1st container : 15
The 1st mismatch element of 2nd container : 25
The returned value from 1st container is  : 0
The returned value from 2nd container is  : 24

// C++ code to demonstrate the working of// mismatch( start_iter1, end_iter1, start_iter2, comparator )  #include<iostream>#include<algorithm> #include<vector>using namespace std;  // comparator function// returns true when element from // 1st element is greater than 2ndbool compare(int a, int b){       return (a>b);}  int main(){          // initializing vectors    vector<int> v1 = { 23, 13, 15, 20 };    vector<int> v2 = { 1, 10, 25, 30, 45 };    vector<int> v3 = { 12, 100, 152, 204 };    vector<int> v4 = { 1, 10, 15, 20, 24 };          // declaring pointer pair    pair< vector<int>::iterator,    vector<int>::iterator > mispair;          // using mismatch() to search for 1st mismatch    mispair = mismatch(v1.begin(), v1.end(), v2.begin(), compare);          // printing the mismatch pair    // 1st mismatch at 15 and 25    // 15 is 1st element less than 2nd at same position    cout << "The 1st mismatch element of 1st container : ";    cout << *mispair.first << endl;          cout << "The 1st mismatch element of 2nd container : ";    cout << *mispair.second << endl;          // using mismatch() to search for 1st mismatch    mispair = mismatch(v3.begin(), v3.end(), v4.begin(), compare);          // printing the mismatch pair    // no mismatch    // all elements in 1st container are greater than 2nd    // points to position after last 0 and corresponding 24    cout << "The returned value from 1st container is  : ";    cout << *mispair.first << endl;          cout << "The returned value from 2nd container is  : ";    cout << *mispair.second << endl;      }
Output:
The 1st mismatch element of 1st container : 15
The 1st mismatch element of 2nd container : 25
The returned value from 1st container is  : 0
The returned value from 2nd container is  : 24

This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
cpp-algorithm-library
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.