孤独的猫

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

类属算法search的功能是:给定两个迭代器区间,将后一个区间内的对象作为一个子序列,并在前一个区间内查找出现该子序列的第一个位置。

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 }

posted on 2011-06-01 21:22  孤独的猫  阅读(1663)  评论(0编辑  收藏  举报