C++STLアルゴリズムシリーズ5---equal(),mismatch()
13988 ワード
equalとmismatchアルゴリズムの機能は,コンテナ内の2つの区間内の要素を比較することである.この2つのアルゴリズムにはそれぞれ3つのパラメータfirst 1,last 1,first 2がある.区間[first 1,last 1)内のすべてのfirst 1+iに対して、first 1+iとfirst 2が存在する位置の要素が等しい場合、equalアルゴリズムは真を返し、そうでない場合は偽を返す.mismatchアルゴリズムの戻り値は、2つの反復器first 1+iとfirst 2+iからなるpairであり、第1対の等しくない要素の位置を表す.等しくない要素が見つからない場合、last 1とfirst 2+iを返す(last1-first1).したがって、文equal(first 1,last 1,first 2)とmismatch(first 1,last 1,first 2)とがある.first=last 1は等価です.
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:
"
49 << *(pair1.first) << " and " << *(pair1.second)
50 << endl;
51 return 0;
52 }
equalアルゴリズムはmismatchに類似しており、equalアルゴリズムも2つのシーケンスの要素が等しいかどうかを逐一比較しているが、equal関数の戻り値はbool値true/falseであり、反復器値ではない.反復器区間[first 1,last 1)と反復器区間[first 2,first 2+(last 1−first 1))の要素が等しい(または二元述語判定条件binary_predを満たす)場合、trueを返し、そうでない場合falseを返す2つのプロトタイプがある.
関数のプロトタイプ:
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:
2元述語判定条件absEqualを用いて,2つの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 }