STL algorithm算法mismatch(37)

来源:互联网 发布:打谱软件overture4.0 编辑:程序博客网 时间:2024/04/27 18:38

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

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




0 0
原创粉丝点击