C++ STL算法系列5---equal() , mismatch()

 

equal和mismatch算法的功能是比较容器中的两个区间内的元素。这两个算法各有3个参数first1,last1和first2.如果对 于区间[first1,last1)内所有的first1+i,first1+i和first2所在位置处的元素都相等,则equal算法返回真,否则返 回假。mismatch算法的返回值是由两个迭代器first1+i和first2+i组成的一个pair,表示第1对不相等的元素的位置。如果没有找到 不相等的元素,则返回last1和first2+(last1-first1)。因此,语句equal(first1,last1,first2)和mismatch(first1,last1,first2).first==last1是等价的.

 1 // Illustrating the generic equal and mismatch algorithms
 2 #include <iostream>
 3 #include <cassert>
 4 #include <algorithm>
 5 #include <string>
 6 #include <list>
 7 #include <deque>
 8 #include <vector>
 9 using namespace std;
10 
11 int main()
12 {
13   cout << "Illustrating the generic equal "
14        << "and mismatch algorithms." << endl;
15   list<string> driver_list;
16   vector<string> vec;
17   deque<string> deq;
18 
19   driver_list.insert(driver_list.end(), "Clark");
20   driver_list.insert(driver_list.end(), "Rindt");
21   driver_list.insert(driver_list.end(), "Senna");
22 
23   vec.insert(vec.end(), "Clark");
24   vec.insert(vec.end(), "Rindt");
25   vec.insert(vec.end(), "Senna");
26   vec.insert(vec.end(), "Berger");
27 
28   deq.insert(deq.end(), "Clark");
29   deq.insert(deq.end(), "Berger");
30 
31   // Show that driver_list and the first 3 elements of
32   // vec are equal in all corresponding positions:
33   assert (equal(driver_list.begin(), driver_list.end(),
34                 vec.begin()));
35 
36   // Show that deq and the first 2 elements of driver_list
37   // are not equal in all corresponding positions:
38   assert (!equal(deq.begin(), deq.end(),
39                  driver_list.begin()));
40 
41   // Find the corresponding positions in deq and driver_list
42   // at which unequal elements first occur:
43   pair<deque<string>::iterator, list<string>::iterator>
44     pair1 = mismatch(deq.begin(), deq.end(),
45                      driver_list.begin());
46 
47   if (pair1.first != deq.end())
48     cout << "First disagreement in deq and driver_list:\n  "
49          << *(pair1.first) << " and " << *(pair1.second)
50          << endl;
51   return 0;
52 }

 

equal算法类似于mismatch,equal算法也是逐一比较两个序列的元素是否相等,只是equal函数的返回值为bool值 true/false,不是返回迭代器值。它有如下两个原型,如果迭代器区间[first1,last1)和迭代器区间[first2, first2+(last1 - first1))上的元素相等(或者满足二元谓词判断条件binary_pred) ,返回true,否则返回false。

  函数原型:

 1 template<class InputIterator1, class InputIterator2>
 2    bool equal(
 3       InputIterator1 _First1, 
 4       InputIterator1 _Last1, 
 5       InputIterator2 _First2
 6       );
 7 template<class InputIterator1, class InputIterator2, class BinaryPredicate>
 8    bool equal(
 9       InputIterator1 _First1, 
10       InputIterator1 _Last1, 
11       InputIterator2 _First2, 
12       BinaryPredicate _Comp
13       );
14  

example:

利用二元谓词判断条件absEqual,判断出两个vector向量容器的元素均绝对值相等。

 1 #include <algorithm>  
 2 #include <vector>  
 3 #include <iostream>  
 4   
 5 using namespace std;  
 6   
 7 bool absEqual(int a, int b)  
 8 {  
 9     return (a == abs(b) || b == abs(a)) ? true : false;  
10 }  
11   
12 int main()  
13 {  
14     vector<int> ivect1(5);  
15     vector<int> ivect2(5);  
16   
17     for (vector<int>::size_type i = 0; i < ivect1.size(); ++i)  
18     {  
19         ivect1[i] = i;  
20         ivect2[i] = (-1) * i;  
21     }  
22     if ( equal( ivect1.begin(), ivect1.end(), ivect2.begin(), absEqual ) )  
23     {  
24         cout << "ivect1 和 ivect2 元素的绝对值完全相等" << endl;  
25     }   
26     else  
27     {  
28         cout << "ivect1 和 ivect2 元素的绝对值不完全相等" << endl;  
29     }  
30     return 0;  
31 }  

 

你可能感兴趣的:(match)