歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C++ string類中的字符串查找

C++ string類中的字符串查找

日期:2017/3/1 10:25:29   编辑:Linux編程
類string提供了大量查找功能和搜索功能,其中比較常用的查找和搜索函數是find()函數、
find_first_not_of()函數、find_first_of()函數、find_last_not_of()函數、find_last_of()函數、rfind()等。

find()函數的語法如下所示:
(1) size_type find(E c, size_type pos = npos) const;用來查找單個字符在字符串中出現的位置並返回
該位置基於0的索引值。
(2) size_type find(const E *s, size_type pos = npos) const;用來查找以空字符結尾的字符數組在
字符串中出現的位置並返回該位置基於0索引值。
(3) size_type find(const E *s, size_type pos, size_type n = npos) const;用來查找以空字符結尾的
字符數組在字符串中出現的位置並返回該位置基於0索引值,它是從npos開始查找的。
(4) size_type find(const basic_string &str, size_type pos = npos) const;用來查找字符串並且返回
該搜索的字符串的基於0索引值的位置值,它是從npos開始查找的。


find()函數的功能是從std::string對象的頭部順序找目標值,如果找到返回該目標值出現的位置,如果沒有在
字符串對象中找到目標對象,返回值為-1。


rfind()函數的語法如下所示:
(1) size_type rfind(E c, size_type pos = npos) const;用來反向查找單個字符在字符串中出現的位置
並返回該位置基於0的索引值。
(2) size_type rfind(const E *s, size_type pos = npos) const;用來反向查找以空字符結尾的字符數組
在字符串中出現的位置並返回該位置基於0索引值。
(3) size_type rfind(const E *s, size_type pos, size_type n = npos) const;用來反向查找以空字符
結尾的字符數組在字符串中出現的位置並返回該位置基於0索引值,它是從npos開始查找的。
(4) size_type rfind(const basic_string &str, size_type pos = npos) const;用來反向查找字符串並且
返回出現該搜索的字符串的基於0索引值的位置值,它是從npos開始查找的。
rfind()函數的功能是從std::sring對象的尾部反向查找目標值,如果找到返回該目標值出現的位置,如果沒有
在字符串對象中找到目標對象,返回值為-1。


find_first_not_of()函數的常見語法如下所示:
size_type find_first_not_of(E c, size_type pos = 0) const;
size_type find_first_not_of(const E *s, size_type pos = 0) const;
size_type find_first_not_of(const E *s, size_type pos, size_type n) const;
size_type find_first_not_of(const basic_string &str, size_type pos = 0) const;
該函數的功能是在string對象中查找對象,如果在string出現了完全不匹配的字符,字符串或以空字符結尾的
字符數組時,系統顯示第一次出現這種情形的位置。如果定義了pos,從pos開始搜索。


find_first_of()函數的常見語法如下所示:
size_t find_first_of( const string& str, size_t pos = 0 ) const;
size_t find_first_of( const char* s, size_t pos, size_t n ) const;
size_t find_first_of( const char* s, size_t pos = 0 ) const;
size_t find_first_of( char c, size_t pos = 0 ) const;
該函數的功能是在string對象中查找對象,如果在string出現了任意完全匹配的字符,字符串或以空字符結尾
的字符數組時,系統顯示第一次出現這種情形的位置。如果定義了pos,從pos開始搜索。只要在string對象中出現了
匹配對象,立即返回位置。


find_last_not_of()函數的常見語法如下所示:
size_t find_last_not_of( const string& str, size_t pos = npos ) const;
size_t find_last_not_of( const char* s, size_t pos, size_t n ) const;
size_t find_last_not_of( const char* s, size_t pos = npos ) const;
size_t find_last_not_of( char c, size_t pos = npos ) const;
該函數的功能是在string對象中反向查找對象,如果在string出現了任意完全不匹配的字符,字符串或以空
字符結尾的字符數組時,系統顯示第一次完全不匹配時出現的位置。如果定義了pos,從pos反向開始搜索。只要
在string對象中出現了完全不匹配的對象,立即返回位置值。


find_last_of()函數的常見語法如下所示:
size_t find_last_of( const string& str, size_t pos = npos ) const;
size_t find_last_of( const char* s, size_t pos, size_t n ) const;
size_t find_last_of( const char* s, size_t pos = npos ) const;
size_t find_last_of( char c, size_t pos = npos ) const;
該函數的共是在string對象中反向查找對象,如果在string出現了任意完全匹配的字符,字符串或以空字符結
尾的字符數組時,系統顯示第一次出現這種情形的位置。如果定義了pos,從pos開始反向搜索。只要在string對象
中出現了匹配的對象,則立即返回位置值。

字符串查找實例find_first_of()

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main(int argc, char *argv[])
  5. {
  6. string str1("Heartbeat");
  7. string str2("abcde");
  8. int iPos = 0;
  9. cout << "The string to search is '" << str1.c_str() << "'" << endl;
  10. //find the first instance in str1 of any characters in str2
  11. iPos = str1.find_first_of(str2, 0);
  12. cout << "Element in '" << str2.c_str() << "' found at position " << iPos << endl;
  13. //start looking in the third position
  14. iPos = str1.find_first_of(str2, 2);
  15. cout << "Element in '" << str2.c_str() << "' found at position " << iPos << endl;
  16. //use an array of the element type as the set of elements to search for;
  17. //look for anything after the fourth position
  18. char achVowels[] = {'a', 'e', 'i', 'o', 'u'};
  19. iPos = str1.find_first_of(achVowels, 4, sizeof(achVowels));
  20. cout << "Element in '";
  21. for (int i = 0; i < sizeof(achVowels); ++i)
  22. {
  23. cout << achVowels[i];
  24. }
  25. cout << "' found at position " << iPos << endl;
  26. //use a string literal to specify the set of elements
  27. char szVowels[] = "aeiou";
  28. iPos = str1.find_first_of(szVowels, 0);
  29. cout << "Element in '" << szVowels << "' found at position " << iPos << endl;
  30. //look for a specific character beginning in the third position
  31. iPos = str1.find_first_of('e', 2);
  32. cout << "'e' found at position " << iPos << endl;
  33. return 0;
  34. }
運行結果為:

The string to search is 'Heartbeat'
Element in 'abcde' found at position 1
Element in 'abcde' found at position 2
Element in 'aeiou' found at position 6
Element in 'aeiou' found at position 1
'e' found at position 6

Copyright © Linux教程網 All Rights Reserved