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引數是在源串中開始搜尋的下標位置
#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
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;
#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
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;
#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