mismath是比较两个容器元素的不同的一个函数,会找到第一个不同的元素,函数形式有两种:
第一种:
template<class _InIt1,
class _InIt2> inline
pair<_InIt1, _InIt2>
mismatch(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2)
第二种:
template<class _InIt1,
class _InIt2,
class _Pr> inline
pair<_InIt1, _InIt2>
mismatch(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2, _Pr _Pred)
程序示例:
#include<iostream>
#include<vector>
#include<list>
#include<algorithm>
#include<functional>
using namespace std;
void print(int ele) {
cout << ele << " ";
}
int main(){
vector<int> vec;
list<int> l;
vec.push_back(1);
vec.push_back(4);
vec.push_back(2);
vec.push_back(7);
vec.push_back(3);
l.push_back(1);
l.push_back(4);
l.push_back(5);
l.push_back(7);
l.push_back(3);
for_each(vec.begin(), vec.end(), print);
cout << endl;
for_each(l.begin(), l.end(), print);
cout << endl;
pair<vector<int>::iterator, list<int>::iterator> p;
p = mismatch(vec.begin(), vec.end(), l.begin());
if (p.first == vec.end()) {
cout << "no different" << endl;
}
else {
cout << "different is :";
cout << *p.first << " " << *p.second << endl;
}
p = mismatch(vec.begin(), vec.end(), l.begin(), less_equal<int>());
if (p.first == vec.end()) {
cout << "no different" << endl;
}
else {
cout << "different is :";
cout << *p.first << " " << *p.second << endl;
}
system("pause");
return 0;
}
输出结果:
第一种找到第一个两个容器中不相同的元素,第二种找到第一个第一个容器的元素大于第二个容器的元素的位置,less_equal是比较元素值小于等于返回true的函数