STL algorithm算法mismatch(37)

mismatch原型:

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);
该函数是用来查找两个序列中第一对不匹配的元素。第一个序列为[first1.last1),第二个序列从first2開始。

比如:

序列1:1,3,5,7,9

序列2:1,3,8,8,9

第一对不匹配的元素为(5,8)

假设第一个序列和第二个序列的前部全然同样,比如

1,2,3,4

1,2,3,4,5

则返回make_pari(last1,5);

使用operator==来进行比較。

行为类似于:

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);
}
一个简单的样例:
#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;


}





执行结果:



——————————————————————————————————————————————————————————————————

//写的错误或者不好的地方请多多指导,能够在以下留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我改动,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

author:天下无双

Email:coderguang@gmail.com

2014-9-19

于GDUT

——————————————————————————————————————————————————————————————————




posted @ 2015-01-17 10:18  phlsheji  阅读(362)  评论(0编辑  收藏  举报