mismatch |
Search two sequences for a mismatched item.
Use a pair of iterators i
and j to traverse two sequences, starting at first1
and first2 , respectively. Return the iterator pair (
i, j ) when either their respective elements mismatch or i
reaches last1 . The first version uses operator==
to perform the comparisons, whereas the second version uses binary_pred
.
#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 binary_pred
);
Time complexity is linear, as
at most ( last1 - first1
) comparisons are performed. Space complexity is constant.
#include <iostream>
#include <algorithm>
int n1[ 5 ] = { 1, 2, 3, 4, 5 };
int n2[ 5 ] = { 1, 2, 3, 4, 5 };
int n3[ 5 ] = { 1, 2, 3, 2, 1 };
void
main()
{
pair< int*, int* > result;
result = mismatch( n1, n1 + 5, n2 );
if ( result.first ==( n1 + 5 ) && result.second ==( n2 + 5 ) )
cout << "n1 and n2 are the same\n";
else
cout << "Mismatch at offset: " << ( result.first - n1 ) << "\n";
result = mismatch( n1, n1 + 5, n3 );
if ( result.first ==( n1 + 5 ) && result.second ==( n3 + 5 ) )
cout << "n1 and n3 are the same\n";
else
cout << "Mismatch at offset: " << ( result.first - n1 ) << "\n";
}
n1 and n2 are the same
Mismatch at offset: 3
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
void
main()
{
vector< int > v1( 10 );
vector< int > v2( v1.size() );
for ( size_t i = 0; i < v1.size(); ++i )
{
v1[ i ] = i;
v2[ i ] = i;
}
pair< vector< int >::iterator, vector< int >::iterator > result =
mismatch
(
v1.begin(),
v1.end(),
v2.begin()
);
if ( result.first == v1.end() && result.second == v2.end() )
cout << "v1 and v2 are the same\n";
else
cout << "mismatch at index: " << ( result.first - v1.begin() ) << "\n";
v2[ v2.size() / 2 ] = 42;
result = mismatch( v1.begin(), v1.end(), v2.begin() );
if ( result.first == v1.end() && result.second == v2.end() )
cout << "v1 and v2 are the same\n";
else
cout << "mismatch at index: " << ( result.first - v1.begin() ) << "\n";
}
v1 and v2 are the same
mismatch at index: 5
#include <iostream>
#include <string.h>
#include <algorithm>
const unsigned size = 5;
char* n1[ size ] = { "Brett", "Graham", "Jack", "Mike", "Todd" };
bool
str_equal( const char* a_, const char* b_ )
{
return ::strcmp( a_, b_ ) == 0 ? 1 : 0;
}
void
main()
{
char* n2[ size ];
copy( n1, n1 + 5, n2 );
pair< char**, char** > result
(
mismatch( n1, n1+ size, n2, str_equal )
);
if ( result.first == n1 + size && result.second == n2 + size )
cout << "n1 and n2 are the same\n";
else
cout << "mismatch at index: " << ( result.first - n1 ) << "\n";
n2[ 2 ] = "QED";
result = mismatch( n1, n1 + size, n2, str_equal );
if ( result.first == n2 + size && result.second == n2 + size )
cout << "n1 and n2 are the same\n";
else
cout << "mismatch at index: " << ( result.first - n1 ) << "\n";
}
n1 and n2 are the same
mismatch at index: 2
Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.