Mismatch prototypes:
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 two sequences. The first sequence is [First1.last1], and the second sequence starts from First2.
Like what:
Sequence 1:1,3,5,7,9
Sequence 2:1,3,8,8,9
The first pair of mismatched elements is (5,8)
Suppose that the first sequence and the front part of the second sequence are exactly the same, such as
1,2,3,4
1,2,3,4,5
Then return to Make_pari (last1,5);
Use operator== to compare.
Behavior is similar to:
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;}
Execution Result:
——————————————————————————————————————————————————————————————————
Write the wrong or bad place please a lot of guidance, can be in the following message or click on the top left email address to send me an e-mail, point out my errors and shortcomings, so that I change, better share to everyone, thank you.
Reprint Please specify source: http://blog.csdn.net/qq844352155
Author: unparalleled
Email:[email protected]
2014-9-19
From Gdut
——————————————————————————————————————————————————————————————————
STL algorithm algorithm mismatch (37)