大佬教程收集整理的这篇文章主要介绍了C++字符串查找函数详解,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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",5); String::size_type rmo = str.rfind (" drugs",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,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,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",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_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,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",5); String:: size_type rmo = str.find_LAST_Not_of ("drugs",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++字符串查找函数详解全部内容,希望文章能够帮你解决C++字符串查找函数详解所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。