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 }
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 }