다음을 통해 공유


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.

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

  • _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.

Return Value

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 If there is no difference between the elements in the ranges compared or if the binary predicate in the second version is satisfied by all element pairs from the two ranges, then the first component iterator points to the position one past the final element in the first range and the second component iterator to position one past the final element tested in the second range.

Remarks

The range to be searched must be valid; all pointers must be dereferenceable and the last position is reachable from the first by incrementation.

The time complexity of the algorithm is linear in the number of elements contained in the range.

The operator== used to determine the equality between elements must impose an equivalence relation between its operands.

Example

// alg_mismatch.cpp
// compile with: /EHsc
#include <vector>
#include <list>
#include <algorithm>
#include <iostream>

// Return whether second element is twice the first
bool twice ( int elem1, int elem2 )
{
   return elem1 * 2 == elem2;
}

int main( ) 
{
   using namespace std;
   vector <int> v1, v2;
   list <int> L1;
   vector <int>::iterator Iter1, Iter2;
   list <int>::iterator L1_Iter, L1_inIter;

   int i;
   for ( i = 0 ; i <= 5 ; i++ )
   {
      v1.push_back( 5 * i );
   }

   int ii;
   for ( ii = 0 ; ii <= 7 ; ii++ )
   {
      L1.push_back( 5 * ii );
   }

   int iii;
   for ( iii = 0 ; iii <= 5 ; iii++ )
   {
      v2.push_back( 10 * iii );
   }
   
   cout << "Vector v1 = ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")" << endl;

   cout << "List L1 = ( " ;
   for ( L1_Iter = L1.begin( ) ; L1_Iter!= L1.end( ) ; L1_Iter++ )
      cout << *L1_Iter << " ";
   cout << ")" << endl;

   cout << "Vector v2 = ( " ;
   for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )
      cout << *Iter2 << " ";
      cout << ")" << endl;

   // Testing v1 and L1 for mismatch under identity
   pair<vector <int>::iterator, list <int>::iterator> results1;
   results1 = mismatch (v1.begin( ), v1.end( ), L1.begin( ));

   if ( results1.first == v1.end( ) )
      cout << "The two ranges do not differ."
           << endl;
   else
      cout << "The first mismatch is between "
           << *results1.first << " & " << *results1.second
           << endl;

   // Modifying L1
   L1_inIter = L1.begin( );
   L1_inIter++;
   L1_inIter++;
   L1.insert(L1_inIter, 100);
   cout << "Modified L1 = ( " ;
   for ( L1_Iter = L1.begin( ) ; L1_Iter!= L1.end( ) ; L1_Iter++ )
      cout << *L1_Iter << " ";
   cout << ")" << endl;

   // Testing v1 with modified L1 for mismatch under identity
   results1 = mismatch ( v1.begin( ), v1.end( ), L1.begin( ) );

   if ( results1.first == v1.end( ) )
      cout << "The two ranges do not differ."
           << endl;
   else
      cout << "The first mismatch is between "
           << *results1.first << " & " << *results1.second
           << endl;

   // Test v1 and v2 for mismatch under the binary predicate twice
   pair<vector <int>::iterator, vector <int>::iterator> results2;
   results2 = mismatch ( v1.begin( ), v1.end( ), v2.begin( ), twice );

   if ( results2.first == v1.end( ) )
      cout << "The two ranges do not differ under the binary "
           << "predicate twice." << endl;
   else
      cout << "The first mismatch is between "
           << *results2.first << " & " << *results2.second
           << endl;
}
Vector v1 = ( 0 5 10 15 20 25 )
List L1 = ( 0 5 10 15 20 25 30 35 )
Vector v2 = ( 0 10 20 30 40 50 )
The two ranges do not differ.
Modified L1 = ( 0 5 100 10 15 20 25 30 35 )
The first mismatch is between 10 & 100
The two ranges do not differ under the binary predicate twice.

Requirements

Header: <algorithm>

Namespace: std

See Also

Reference

Standard Template Library

Other Resources

<algorithm> Members