#include <algorithm> #include <list> #include <utility> #include <iostream> using namespace std; /************************************************************************/ /* */ template<class _InIt1, class _InIt2> inline pair<_InIt1, _InIt2> __CLRCALL_OR_CDECL my_Mismatch(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2) { // return [_First1, _Last1) and [_First2, _Last2) mismatch #if _HAS_ITERATOR_DEBUGGING _DEBUG_RANGE(_First1, _Last1); if (_First1 != _Last1) _DEBUG_POINTER(_First2); #endif /* _HAS_ITERATOR_DEBUGGING */ //for (; _First1 != _Last1 && _Pred(*_First1, *_First2); ) for (; _First1 != _Last1 && *_First1 == *_First2; ) ++_First1, ++_First2; return (pair<_InIt1, _InIt2>(_First1, _First2)); } /************************************************************************/ class equal_and_odd{ public: bool operator()( int ival1, int ival2 ) { // are the two values equal, and either // both zero, or both odd? return ( ival1 == ival2 && ( ival1 == 0 || ival1%2 )); } }; int main() { int ia[] = { 0,1,1,2,3,5,8,13 }; int ia2[] = { 0,1,1,2,4,6,10 }; pair<int*,int*> pair_ia = mismatch( ia, ia+7, ia2 ); // generates: first mismatched pair: ia: 3 and ia2: 4 cout << "first mismatched pair: ia: " << *pair_ia.first << " and ia2: " << *pair_ia.second << endl; list< int > ilist( ia, ia + 7 ); list< int > ilist2( ia2, ia2 + 7 ); typedef list< int >::iterator iter; pair< iter,iter > pair_ilist = mismatch( ilist.begin(), ilist.end(), ilist2.begin(), equal_and_odd() ); // generates: // first mismatched pair either not equal or not odd: // ilist: 2 and ilist2: 2 cout << "first mismatched pair either not equal " << "or not odd: /n/tilist: " << *pair_ilist.first << " and ilist2: " << *pair_ilist.second << endl; return 0; }
并行的比较两个序列,指出第一个元素不匹配的位置,它返回一对iterator
标识出第一个元素不匹配的位置