STL algorithm mismatch (37)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.