find() algorithm一次只能找一個條件,若要同時找多個條件,需使用find_first_of()。

find_first_of()允許我們將要找的條件先放到另外一個container中,然後一起搜尋,結果傳回第一個找到的iterator。

此範例中我們想找出第一個出現的母音,因為母音有aeiou,若用find(),就得搜尋5次,若用find_first_of(),只需一行就可找出第一個母音。

 1}


執行結果

(原創) 如何使用find_first_of() algorithm? (C/C++) (STL)First vowel in s
(原創) 如何使用find_first_of() algorithm? (C/C++) (STL)o
(原創) 如何使用find_first_of() algorithm? (C/C++) (STL)
(原創) 如何使用find_first_of() algorithm? (C/C++) (STL)All vowel in s using while()
(原創) 如何使用find_first_of() algorithm? (C/C++) (STL)o e o o o e i a u e i o
(原創) 如何使用find_first_of() algorithm? (C/C++) (STL) 
(原創) 如何使用find_first_of() algorithm? (C/C++) (STL)All vowel in s using for()
(原創) 如何使用find_first_of() algorithm? (C/C++) (STL)o e o o o e i a u e i o
(原創) 如何使用find_first_of() algorithm? (C/C++) (STL)
(原創) 如何使用find_first_of() algorithm? (C/C++) (STL)請按任意鍵繼續 . . 


21,22行為find_first_of的標準用法,有兩個input range。

我們似乎無法滿足若只能找到一個母音的結果,若我們想找到所有的母音呢?若找到第一個母音後,則繼續從找到的位置的下一個繼續找,依直到找不到為止,29行到39行使用while(),這種寫法可讀性較佳,也顯示了while()仍有其價值,44行到48行使用for(),程式較精簡,不過可讀性較差,需要一點程度。

相关文章: