11.2.1 find_first_of函数

来源:互联网 发布:网站注册域名查询 编辑:程序博客网 时间:2024/06/13 11:54
find_first_of的参数:
这个算法带有两对迭代器参数来标记两段元素范围,
在第一个范围内查找与第二段范围中任意元素匹配的元素,然后返回一个迭代器,指向第一个匹配的元素。

如果找不到匹配的元素,则返回第一个范围内的end迭代器。
[root@localhost testc++]# vi find_first_of.cpp      
1 #include <iostream>
2 #include <algorithm>
3 #include <numeric>
4 #include <list>
5 #include <string>
6 using namespace std;
7
8 int main()
9 {
10 // program for illustration purposes only:
11 // there are much faster ways to solve this problem
12 size_t cnt = 0;
13 string sval;
14 list<string> roster1;
15 list<string> roster2;
16 cout << "Enter roster1 : " << endl;
17 while( cin >> sval)
18 roster1.push_back(sval);
19 cin.clear();//能够进行两个cin输入
20 cout << "Enter roster2 : " << endl;
21 while( cin >> sval)
22 roster2.push_back(sval);
23 list<string>::iterator it = roster1.begin();
24 // look in roster1 for any name also in roster2
25 while ((it = find_first_of(it,roster1.end(),
26 roster2.begin(),roster2.end()))
27 != roster1.end()) {
28 ++cnt;
29 // we got a match ,increment it to look in the rest of roster1
30 ++it;
31 }
32 cout << "Found " << cnt
33 << " names on both roster2" << endl;
34 return 0;
35 }
[root@localhost testc++]# g++ find_first_of.cpp
[root@localhost testc++]# ./a.out
Enter roster1 :
aa
bb
Enter roster2 :
bb
cc
Found 1 names on both roster2
[root@localhost testc++]#