Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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.

  1. 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 container
    end_iter1: Last iterator to 1st container
    start_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
    
  2. 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 2nd
    bool 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
    



Last Updated : 06 Jul, 2017
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads