The C++ Standard Library p353
find_first_of属于非变动性算法,它的两种形式如下:
ForwardIterator1 find_first_of(
ForwardIterator1 _First1,
ForwardIterator1 _Last1,
ForwardIterator2 _First2,
ForwardIterator2 _Last2
);
ForwardIterator1 find_first_of(
ForwardIterator1 _First1,
ForwardIterator1 _Last1,
ForwardIterator2 _First2,
ForwardIterator2 _Last2,
BinaryPredicate _Comp
);
先看第一种,书中代码简略如下
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 // 省略部分代码,INSERT_ELEMENTS为插入元素到容器中,PRINT_ELEMENTS为打印容器内所有元素
2 vector<int> coll;
3 list<int> searchcoll;
4 INSERT_ELEMENTS(coll, 1, 11);
5 INSERT_ELEMENTS(searchcoll, 3, 6);
6 PRINT_ELEMENTS(coll, "coll: ");
7 PRINT_ELEMENTS(searchcoll, "searchcoll: ");
8 vector<int>::iterator pos;
9 // search first occurrence of an element of searchcoll in coll
10 pos = find_first_of(coll.begin(), coll.end(), // range
11 searchcoll.begin(), // beginning of search set
12 searchcoll.end()); // end of search set
13 cout << "first element of searchcoll in coll is element "
14 << distance(coll.begin(), pos) + 1
15 << endl;
16 // search last occurrence of an element of searchcoll in coll
17 vector<int>::reverse_iterator rpos;
18 rpos = find_first_of(coll.rbegin(), coll.rend(),
19 searchcoll.begin(),
20 searchcoll.end());
21 cout << "last element of searchcoll in coll is element "
22 << distance(coll.begin(), rpos.base())
23 << endl;
24 /*
25 * coll: 1 2 3 4 5 6 7 8 9 10 11
26 * searchcoll: 3 4 5 6
27 * first element of searchcoll in coll is element 3
28 * last element of searchcoll in coll is element 6
29 */
问题出在第18,这里有点疑问,就是逆向查找以找到searchcoll在coll中的最后一个元素位置,我开始以为既然查找的区间都是使用的逆向迭代器,那么searchcoll也应该使用searchcoll.rbegin(),searchcoll.rend(),更为奇怪的是更改后结果竟然完全相同。后来仔细看了下书才发现(看书不认真,bs下自己~),根据书中所说,find_first_of是返回第一个在源、目标区间出现的元素位置。原来目标区间是一个集合,而这个查找也是集合之间的比较,所以逆正都是无所谓。验证如下:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 vector<int> coll;
2 list<int> searchcoll;
3 // INSERT_ELEMENTS(coll, 1, 11);
4 // INSERT_ELEMENTS(searchcoll, 3, 6);
5 // insert elements by hand
6 coll.push_back(11);
7 coll.push_back(4);
8 coll.push_back(5);
9 coll.push_back(2);
10 coll.push_back(7);
11 coll.push_back(8);
12 coll.push_back(10);
13 coll.push_back(9);
14
15 searchcoll.push_back(12);
16 searchcoll.push_back(8);
17 searchcoll.push_back(7);
18 searchcoll.push_back(4);
19
20 PRINT_ELEMENTS(coll, "coll: ");
21 PRINT_ELEMENTS(searchcoll, "searchcoll: ");
22 vector<int>::iterator pos;
23 // search first occurrence of an element of searchcoll in coll
24 pos = find_first_of(coll.begin(), coll.end(), // range
25 searchcoll.begin(), // beginning of search set
26 searchcoll.end()); // end of search set
27 cout << "first element of searchcoll in coll is element "
28 << distance(coll.begin(), pos) + 1
29 << endl;
30 // search last occurrence of an element of searchcoll in coll
31 vector<int>::reverse_iterator rpos;
32 rpos = find_first_of(coll.rbegin(), coll.rend(),
33 searchcoll.begin(),
34 searchcoll.end());
35 cout << "last element of searchcoll in coll is element "
36 << distance(coll.begin(), rpos.base())
37 << endl;
38 /*
39 * coll: 11 4 5 2 7 8 10 9
40 * searchcoll: 12 8 7 4
41 * first element of searchcoll in coll is element 2
42 * last element of searchcoll in coll is element 6
43 */