(原創) 如何使用find_first_of() algorithm? (C/C++) (STL)
find() algorithm一次只能找一個條件,若要同時找多個條件,需使用find_first_of()。
find_first_of()允許我們將要找的條件先放到另外一個container中,然後一起搜尋,結果傳回第一個找到的iterator。
此範例中我們想找出第一個出現的母音,因為母音有aeiou,若用find(),就得搜尋5次,若用find_first_of(),只需一行就可找出第一個母音。
1/*
2(C) OOMusou 2006 http://oomusou.cnblogs.com
3
4Filename : GenericAlgo_find_first_of.cpp
5Compiler : Visual C++ 8.0 / ISO C++
6Description : Demo how to use find_first_of() algorithm
7Release : 12/14/2006 1.0
8*/
9#include <iostream>
10#include <algorithm>
11#include <string>
12
13using namespace std;
14int main() {
15 string s = "To be or not to be is a question";
16 string vowel = "aeiou";
17
18 // first vowel in s
19 cout << "First vowel in s " << endl;
20 {
21 string::iterator c = find_first_of(s.begin(), s.end(), vowel.begin(), vowel.end());
22 if (c != s.end()) cout << *c << endl;
23 }
24
25 cout << endl;
26
27 // All vowel in s using while(), more readable
28 cout << "All vowel in s using while()" << endl;
29 string::iterator s_begin = s.begin();
30 while(s_begin != s.end()) {
31 string::iterator c = find_first_of(s_begin, s.end(), vowel.begin(), vowel.end());
32 if (c != s.end()) {
33 cout << *c++ << " ";
34 s_begin = c;
35 }
36 else {
37 break;
38 }
39 }
40
41 cout << endl << endl;
42
43 // all vowel in s usng for, more compact
44 cout << "All vowel in s using for()" << endl;
45 for(string::iterator s_begin = s.begin(),c; s_begin != s.end(); s_begin = c) {
46 c = find_first_of(s_begin, s.end(), vowel.begin(), vowel.end());
47 if (c != s.end()) cout << *c++ << " "; else break;
48 }
49
50 cout << endl << endl;
51
52 return 0;
53}
2(C) OOMusou 2006 http://oomusou.cnblogs.com
3
4Filename : GenericAlgo_find_first_of.cpp
5Compiler : Visual C++ 8.0 / ISO C++
6Description : Demo how to use find_first_of() algorithm
7Release : 12/14/2006 1.0
8*/
9#include <iostream>
10#include <algorithm>
11#include <string>
12
13using namespace std;
14int main() {
15 string s = "To be or not to be is a question";
16 string vowel = "aeiou";
17
18 // first vowel in s
19 cout << "First vowel in s " << endl;
20 {
21 string::iterator c = find_first_of(s.begin(), s.end(), vowel.begin(), vowel.end());
22 if (c != s.end()) cout << *c << endl;
23 }
24
25 cout << endl;
26
27 // All vowel in s using while(), more readable
28 cout << "All vowel in s using while()" << endl;
29 string::iterator s_begin = s.begin();
30 while(s_begin != s.end()) {
31 string::iterator c = find_first_of(s_begin, s.end(), vowel.begin(), vowel.end());
32 if (c != s.end()) {
33 cout << *c++ << " ";
34 s_begin = c;
35 }
36 else {
37 break;
38 }
39 }
40
41 cout << endl << endl;
42
43 // all vowel in s usng for, more compact
44 cout << "All vowel in s using for()" << endl;
45 for(string::iterator s_begin = s.begin(),c; s_begin != s.end(); s_begin = c) {
46 c = find_first_of(s_begin, s.end(), vowel.begin(), vowel.end());
47 if (c != s.end()) cout << *c++ << " "; else break;
48 }
49
50 cout << endl << endl;
51
52 return 0;
53}
執行結果
First vowel in s
o
All vowel in s using while()
o e o o o e i a u e i o
All vowel in s using for()
o e o o o e i a u e i o
請按任意鍵繼續 . .
o
All vowel in s using while()
o e o o o e i a u e i o
All vowel in s using for()
o e o o o e i a u e i o
請按任意鍵繼續 . .
21,22行為find_first_of的標準用法,有兩個input range。
我們似乎無法滿足若只能找到一個母音的結果,若我們想找到所有的母音呢?若找到第一個母音後,則繼續從找到的位置的下一個繼續找,依直到找不到為止,29行到39行使用while(),這種寫法可讀性較佳,也顯示了while()仍有其價值,44行到48行使用for(),程式較精簡,不過可讀性較差,需要一點程度。