I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com

//

Return first position where two ranges differ

Definition

The mismatch() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.

Interface

#include <algorithm>
template < class InputIterator1, class InputIterator2 >
   pair < InputIterator1, InputIterator2 > mismatch(
      InputIterator1 first1, 
      InputIterator1 last1,
      InputIterator2 first2
    );
template < class InputIterator1, 
           class InputIterator2, 
           class BinaryPredicate >
   pair < InputIterator1, InputIterator2 > mismatch(
      InputIterator1 first1, 
      InputIterator1 last1,
      InputIterator2 first2
      BinaryPredicate comp
   );

Parameters:
Parameter Description
first1 An input iterator addressing the position of the first element in the first range to be tested
last1 An input iterator addressing the position one past the final element in the first range to be tested
first2 An input iterator addressing the position of the first element in the second range to be tested
comp User-defined predicate function object that defines the condition to be satisfied if two elements are to be taken as equivalent. A binary predicate takes two arguments and returns true when satisfied and false when not satisfied

Description

Mismatch compares two ranges element by element either for equality or equivalent in a sense specified by a binary predicate and locates the first position where a difference occurs. The two versions of mismatch differ by test that apply.

The first version of mismatch searches the range [first1,last1) and the range beginning at first2 for the first pair of mismatched (unequal) values.

The second version of mismatch finds the first iterator i in [first1, last1) such that binary_pred (*i, *(first2 + (i - first1)) is false.

Return Value

The return value is a pair of iterators addressing the positions of the mismatch in the two ranges, the first component iterator to the position in the first range and the second component iterator to the position in the second range.

Complexity

The time complexity of the algorithm is linear in the number of elements contained in the range, at most last1 - first1 comparisons.
Example:

Example - mismatch algorithm
Problem
The following example demonstrates both forms of mismatch():
Workings
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
 
using namespace std;
 
int main()
{
  vector<int> coll1;
  list<int> coll2;
 
  INSERT_ELEMENTS(coll1,1,6);
 
  for (int i=1; i<=16; i*=2) 
  {
    col12.push_back(i);
  }
  coll2.push_back(3);
 
  PRINT_ELEMENTS(coll1,"coll1: ");
  PRINT_ELEMENTS(coll2,"coll2: ");
 
  //find first mismatch
  pair<vector<int>::iterator,list<int>::iterator> values;
  values = mismatch (coll1.begin(), coll1.end(), //first range
                          coll2.begin());    //second range
  if (values.first == coll1.end()) 
    cout <<"no mismatch"<<endl;     
  else
    cout <<"first mismatch: "<<*values.first<<" and "<<*values.second<<endl;

/*find first position where the element of coll1 is not *less than the corresponding element of coll2 */

values = mismatch (coll1.begin(), coll1.end(), //first range col12. begin(), //second range less_equal<int>() ) //criterion if (values.first == coll1.end()) cout <<"always less-or-equal"<<endl; else cout <<"not less-or-equal: "<<*values.first<<" and " <<*values.second<<endl;

return 0; }
Solution
Output:

coll1: 1 2 3 4 5 6
coll2: 1 2 4 8 16 3
first mismatch: 3 and 4
not less-or-equal: 6 and 3
References
  • Nicolai M. Josuttis: "The C++ Standard Library"