区间比较算法
(1)equal()
该函数可以实现两个容器对象的比较,如果两个对象相等,函数返回true,只能比较属于同一类型的不同变量。
template <class InputIterator1, class InputIterator2>
bool equal (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2);
template <class InputIterator1, class InputIterator2, class BinaryPredicate>
bool equal (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, BinaryPredicate pred);
template <class InputIterator1, class InputIterator2>
bool equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )
{
while (first1!=last1)
{
if ( ! (*first1 == *first2) ) // or: if( !pred(*first1,*first2) ) version 2
return false;
++first1;
++first2;
}
return true;
}
用法示例:
#include <iostream>
#include <algorithm> // std::equal
#include <vector>
bool mypredicate(int i, int j) {
return (i == j);
}
int main() {
int myints[] = { 20,40,60,80,100 };
std::vector<int>myvector(myints, myints + 5); // myvector: 20 40 60 80 100
if (std::equal(myvector.begin(), myvector.end(), myints))
std::cout << "The contents of both sequences are equal.\n";
else
std::cout << "The contents of both sequences differ.\n";
myvector[3] = 81; // myvector: 20 40 60 81 100
if (std::equal(myvector.begin(), myvector.end(), myints, mypredicate))
std::cout << "The contents of both sequences are equal.\n";
else
std::cout << "The contents of both sequences differ.\n";
return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool relationship(int e1, int e2)
{
int tmp = e1 + 2;
return e2 == tmp;
}
void Print(vector<int>& v)
{
vector<int>::iterator it;
for (it = v.begin(); it != v.end(); it++)
cout << *it << ", ";
cout << endl;
}
void main()
{
int arr1[] = { 1,2,3,4,5,6,7 };
int arr2[] = { 3,4,5,6,7,8,9 };
vector<int> v1(arr1, arr1 + 7);
vector<int> v2(arr2, arr2 + 7);
cout << "vector v1: " << endl;
Print(v1);
cout << "vector v2: " << endl;
Print(v2);
bool eq = equal(v1.begin(), v1.end(), v2.begin());
if (eq)
{
cout << "v1 == v2" << endl;
}
else
{
cout << "v1 != to v2" << endl;
}
bool eq2 = equal(v1.begin(), v1.end(), v2.begin(), relationship);
if (eq2)
{
cout << "v2[i]==v1[i]+2" << endl;
}
else
{
cout << "v2[i]!=v1[i]+2" << endl;
}
}
(2)mismatch()用于寻找两个容器对象之间两两相异的对应元素,如果没有找到相异点,也并不代表两个对象完全相同,因为两个对象的容量不一定相同。要判断相同,就要使用equal算法。如果没有找到相异点,函数返回一个pair,以第一个对象的end元素和第二个对象的对应元素组成。如果找到相异点,则相异点的对应元素组成一个pair,并返回
template <class InputIterator1, class InputIterator2>
pair<InputIterator1, InputIterator2> mismatch (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2);
template <class InputIterator1, class InputIterator2, class BinaryPredicate>
pair<InputIterator1, InputIterator2> mismatch (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, BinaryPredicate pred);
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 <list>
#include <vector>
using namespace std;
void Print(int& Ele)
{
cout << Ele << ", ";
}
void main()
{
int arr1[] = { 1,2,3,4,7,5 };
int arr2[] = { 1,2,3,5,6,4 };
list<int> l1(arr1,arr1+6);
vector<int> l2(arr2,arr2+6);
pair<list<int>::iterator, vector<int>::iterator> p1;
for_each(l1.begin(), l1.end(), Print);
cout << endl;
for_each(l2.begin(), l2.end(), Print);
cout << endl;
p1 = mismatch(l1.begin(), l1.end(), l2.begin());
if (p1.first == l1.end())
{
cout << "no mismatch." << endl;
}
else
{
cout << "The first mismatch: ";
cout << *p1.first << ", " << *p1.second << endl;
}
p1 = mismatch(l1.begin(), l1.end(), l2.begin(), less_equal<int>());
if (p1.first == l1.end())
{
cout << "no mismatch." << endl;
}
else
{
cout << "The first mismatch: ";
cout << *p1.first << ", " << *p1.second << endl;
}
}