Mismatch prototype:
STD: Mismatch
Equality (1) |
template <class InputIterator1, class InputIterator2> pair<InputIterator1, InputIterator2> mismatch (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2); |
Predicate (2) |
template <class InputIterator1, class InputIterator2, class BinaryPredicate> pair<InputIterator1, InputIterator2> mismatch (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred); |
This function is used to find the first pair of mismatched elements in the two sequences. The first sequence is [first1.last1), and the second sequence starts from first2.
For example:
Sequence
Sequence 2: 1, 3, 8, and 9
The first pair of mismatched elements is (5, 8)
If the first sequence is identical to the first of the second sequence, for example
1, 2, 3, 4
1, 2, 3, 4, 5
Make_pari (last1, 5) is returned );
Use operator = for comparison.
The behavior is similar:
template <class InputIterator1, class InputIterator2> pair<InputIterator1, InputIterator2> mismatch (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 ){ while ( (first1!=last1) && (*first1==*first2) ) // or: pred(*first1,*first2), for version 2 { ++first1; ++first2; } return std::make_pair(first1,first2);}
A simple example:
#include <iostream>#include <algorithm>#include <vector>using namespace std;void mmismatch(){ vector<int> vi{3,5,4,1}; vector<int> v2{3,5,5,1}; cout<<"vi="; for(int i:vi) cout<<i<<" "; cout<<endl; cout<<"v2="; for(int i:v2) cout<<i<<" "; cout<<endl; auto it=mismatch(vi.begin(),vi.end(),v2.begin()); cout<<"*it.first="<<*it.first<<" ,*it.second="<<*it.second<<endl; vector<int> v3{3,5,4,1,6}; cout<<"v3="; for(int i:v3) cout<<i<<" "; cout<<endl; auto it2=mismatch(vi.begin(),vi.end(),v3.begin()); cout<<"*it2.first="<<*it2.first<<" ,*it2.second="<<*it2.second<<endl;}
Running result:
------------------------------------------------------------------
// For more instructions on writing errors or poor information, you can leave a message below or click the email address in the upper left corner to send an email to me, pointing out my errors and deficiencies, so that I can modify them, thank you for sharing it.
Reprinted please indicate the source: http://blog.csdn.net/qq844352155
Author: unparalleled
Email: [email protected]
2014-9-19
Yu gdut
------------------------------------------------------------------
STL algorithm mismatch (37)