C++字串查詢函數詳解

2020-07-16 10:04:38
在 C 語言和 C++ 語言中,可用於實現字串查詢功能的函數非常多。在 STL 中,字串的查詢功能可以實現多種功能,比如說:
  • 搜尋單個字元、搜尋子串;
  • 實現前向搜尋、後向搜尋;
  • 分別實現搜尋第一個和最後一個滿足條件的字元(或子串);

若查詢 find() 函數和其他函數沒有搜尋到期望的字元(或子串),則返回 npos;若搜尋成功,則返回搜尋到的第 1 個字元或子串的位置。其中,npos 是一個無符號整數值,初始值為 -1。當搜尋失敗時, npos 表示“沒有找到(not found)”或“所有剩佘字元”。

值得注意的是,所有查詢 find() 函數的返回值均是 size_type 型別,即無符號整數型別。該返回值用於表明字串中元素的個數或者字元在字串中的位置。

下面分別介紹和字元查詢相關的函數。

find()函數和 rfind()

find() 函數的原型主要有以下 4 種:

size_type find (value_type _Chr, size_type _Off = 0) const;
//find()函數的第1個引數是被搜尋的字元、第2個引數是在源串中開始搜尋的下標位置
size_type find (const value_type* _Ptr , size_type _Off = 0) const;
//find()函數的第1個引數是被搜尋的字串,第2個引數是在源串中開始搜尋的下標位置
size_type find (const value_type* _Ptr, size_type _Off = 0, size_type _Count) const;
//第1個引數是被搜尋的字串,第2個引數是源串中開始搜尋的下標,第3個引數是關於第1個引數的字元個數,可能是 _Ptr 的所有字元數,也可能是 _Ptr 的子串宇符個數
size_type find (const basic_string& _Str, size_type _Off = 0) const;
//第1個引數是被搜尋的字串,第2引數是在源串中開始搜尋的下標位置

rfind() 函數的原型和find()函數的原型類似,引數情況也類似。只不過 rfind() 函數適用於實現逆向查詢。

find() 函數和 rfind() 函數的使用方法參見如下程式:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
    string str_ch (" for");
    string str (" Hi, Peter, I'm sick. Please bought some drugs for me.");
    string::size_type m= str.find ('P', 5);
    string::size_type rm= str.rfind('P', 5);
    cout << "Example - find() : The position (forward) of 'P' is: " << (int) m << endl;
    cout << "Example - rfind(): The position (reverse) of 'P' is: " << (int) rm << endl;
    string::size_type n = str.find (" some", 0);
    string::size_type rn = str.rfind (" some", 0);
    cout << "Example - find () : The position (forward) of 'some' is: " << (int) n << endl;
    cout << "Example - rfind () : The position (reverse) of 'some' is: " << (int) rn << endl;
    string::size_type mo = str.find (" drugs", 0, 5);
    string::size_type rmo = str.rfind (" drugs", 0, 5);
    cout << "Example - find(): The position (forward) of 'drugs' is: " << (int) mo << endl;
    cout << "Example - rfind(): The position (reverse) of 'drugs' is: " << (int) rmo << endl;
    string::size_type no = str.find (str_ch, 0);
    string::size_type rno = str.rfind(str_ch, 0);
    cout << "Example - find (): The position of 'for' is: " << (int) no << endl;
    cout << "Example - rfind(): The position of 'for' is: " << (int) rno << endl;
    cin.get ();
}
程式的執行結果為:

Example - find() : The position (forward) of 'P' is: 5
Example - rfind(): The position (reverse) of 'P' is: 5
Example - find () : The position (forward) of 'some' is: 35
Example - rfind () : The position (reverse) of 'some' is: -1
Example - find(): The position (forward) of 'drugs' is: 40
Example - rfind(): The position (reverse) of 'drugs' is: -1
Example - find (): The position of 'for' is: 46
Example - rfind(): The position of 'for' is: -1

find_first_of()函數和 find_last_of()函數

find_first_of() 函數可實現在源串中搜尋某字串的功能,該函數的返回值是被搜尋字串的第 1 個字元第 1 次出現的下標(位置)。若查詢失敗,則返回 npos。

find_last_of() 函數同樣可實現在源串中搜尋某字串的功能。與 find_first_of() 函數所不同的是,該函數的返回值是被搜尋字串的最後 1 個字元的下標(位置)。若查詢失敗,則返回 npos。

上述兩個函數的原型分別為:

size_type find_first_not_of (value_type_Ch, size_type_Off = 0) const; size_type find_first_of (const value_type* _Ptr, size_type _Off = 0) const;
size_type find_first_of (const value_type* _Ptr, size_type_Off, size_type_Count) const;
size_type find_first_of (const basic_string & _Str, size_type_Off = 0) const;
size_type find_last_of (value_type _Ch, size_type_Off = npos) const;
size_type find_last_of (const value_type* _Ptr, size_type_Off = npos) const;
size_type find_last_of (const value_type* _Ptr, size_type _Off, size_type _Count) const;
size_type find_last_of (const basic_string& _Str, size_type_Off = npos) const;

下面的程式範例詳細闡述了 find_first_of() 函數和 find_last_of() 函數的使用方法。這兩個函數和 find() 函數及 rfind() 函數的使用方法相同,具體引數的意義亦相同。
#include <iostream>
#include <string>
using namespace std;
int main ()
{
    string str_ch ("for");
    string str("Hi, Peter, I'm sick. Please bought some drugs for me. ");
    int length = str.length();
    string::size_type m = str.find_first_of ('P', 0);
    string::size_type rm = str.find_last_of ('P', (length - 1));
    cout << "Example - find_first_of (): The position (forward) of 'P' is: " << (int) m << endl;
    cout << "Example - find_last_of (): The position (reverse) of 'P' is: " << (int) rm << endl;
    string:: size_type n = str.find_first_of ("some", 0);
    string:: size_type rn = str.find_last_of ("some", (length -1));
    cout << "Example - find_first_of(): The position (forward) of 'some' is: " << (int) n << endl;
    cout << "Example - find_last_of(): The position (reverse) of 'some' is: " << (int) rn << endl;
    string:: size_type mo = str.find_first_of ("drugs", 0, 5);
    string:: size_type rmo = str.find_last_of ("drugs", (length-1), 5);
    cout << "Example - find_first_of () : The position (forward) of 'drugs' is: " << (int) mo << endl;
    cout << "Example - find_last_of () : The position (reverse) of 'drugs' is: " << (int) rmo << endl;
    string::size_type no = str.find_first_of (str_ch, 0);
    string::size_type rno = str.find_last_of (str_ch, (length -1));
    cout << "Example - find_first_of() : The position of 'for' is: " << (int) no << endl;
    cout << "Example - find_last_of () : The position of 'for' is: " << (int) rno << endl;
    cin.get();
    return 0;
}
程式執行結果:

Example - find_first_of (): The position (forward) of 'P' is: 4
Example - find_last_of (): The position (reverse) of 'P' is: 21
Example - find_first_of(): The position (forward) of 'some' is: 5
Example - find_last_of(): The position (reverse) of 'some' is: 51
Example - find_first_of () : The position (forward) of 'drugs' is: 8
Example - find_last_of () : The position (reverse) of 'drugs' is: 48
Example - find_first_of() : The position of 'for' is: 8
Example - find_last_of () : The position of 'for' is: 48

find_first_not_of()函數和 find_last_not_of()函數

find_first_not_of() 函數的函數原型為:

size_type find_first_not_of (value_type _Ch, size_type_Off = 0) const;
size_type find_first_not_of (const value_type * _Ptr, size_type_Off = 0) const;
size_type find_first_not_of (const value_type* _Ptr, size_type_Off, size_type_Count) const;
size_type find_first_not_of (const basic_string & _Str, size_type_Off = 0) const;

find_first_not_of() 函數可實現在源字串中搜尋與指定字元(串)不相等的第 1 個字元;find_last_not_of() 函數可實現在源字串中搜尋與指定字元(串)不相等的最後 1 個字元。這兩個函數的引數意義和前面幾個函數相同,它們的使用方法和前面幾個函數也基本相同。詳見下面的程式:
#include < iostream >
#include <string>
using namespace std;
int main ()
{
    string str_ch (" for");
    string str ("Hi, Peter, I'm sick. Please bought some drugs for me.");
    int length = str.length ();
    string::size_type m= str.find_first_not_of ('P',0);
    string::size_type rm= str.find_last_not_of ('P', (length -1);
    cout << "Example - find_first_of (): The position (forward) of 'P' is: " << (int) m << endl;
    cout << "Example - find_last_of (): The position (reverse) of 'P' is: " << (int) rm << endl;
    string:: size_type n = str.find_first_not_of ("some", 0);
    string:: size_type rn = str.find_last_not_of ("some", (length -1));
    cout << "Example - find_first_of (): The position (forward) of 'some' is: " << (int) n << endl;
    cout << "Example - find_last_of (): The position (reverse) of 'some' is: " << (int) rn << endl;
    string:: size_type mo = str.find_first_not_of ("drugs", 0, 5);
    string:: size_type rmo = str.find_last_not_of ("drugs", (length-1), 5);
    cout << "Example - find_first_of (): The position (forward) of 'drugs' is: " << (int) mo << endl;
    cout << "Example - find_last_of (): The position (reverse) of 'drugs' is: " << (int) rno << endl;
    string::size_type no = str.find_first_not_of (str_ch, 0);
    string::size_type rno = str.find_last_not_of (str_ch, (length-1));
    cout << "Example - find_first_of (): The position of 'for' is: " << (int) no << endl;
    cout << "Example - find_last_of () : The position of 'for' is: " << (int) rno << endl;
    cin.get ();
    return 0;
}
程式執行結果為:

Example - find_first_of (): The position (forward) of 'P' is: 0
Example - find_last_of (): The position (reverse) of 'P' is: 52
Example - find_first_of (): The position (forward) of 'some' is: 0
Example - find_last_of (): The position (reverse) of 'some' is: 52
Example - find_first_of (): The position (forward) of 'drugs' is: 0
Example - find_last_of (): The position (reverse) of 'drugs' is: 52
Example - find_first_of (): The position of 'for' is: 0
Example - find_last_of () : The position of 'for' is: 52

本小節主要講述 C++ STL 中的字串查詢函數。對於所述的 6 個查詢函數,它們的使用形式大致相同,對於每個函數均配備了範例作為參考。請讀者能認真對照例題,深刻理解這 6 個函數的使用方法,仔細體會函數每個引數的意義。