String 中字符串查找.docx
- 文档编号:7727715
- 上传时间:2023-01-26
- 格式:DOCX
- 页数:13
- 大小:30.28KB
String 中字符串查找.docx
《String 中字符串查找.docx》由会员分享,可在线阅读,更多相关《String 中字符串查找.docx(13页珍藏版)》请在冰豆网上搜索。
String中字符串查找
String的用法
String类仅仅查找操作就为我们提供了6种查找函数,每种函数以不同形式的find命名。
find、rfind、find_first_of 、find_first_not_of、find_last_of 、find_last_not_of
这些都是const成员函数,const也就是说,它们只是为了针对某种用途去寻找子串的位置,并不能够改变它们所作用的那个串的值。
这些操作全都返回string:
:
size_type类型的值,也就是说,返回的结果是一个unsigned值(即,无符号整形)。
要么是以下标形式查找匹配所发生的位置(也就是返回当前查找到的子串下标);或者返回一个名为string:
:
npos类型的值,说明查找没有匹配,也就是找不到你所查找的字符。
就这么两种形式,要么是返回下标,要么返回npos。
每个查找操作都有4个重载版本,每个版本使用不同的参数形式。
基本上,这些操作的不同之处在于查找的到底是单个字符,还是另一个string字符串,或者c风格的以空字符结束的字符串,还是用字符数组给出的特定数目的字符集合。
比如说:
size_typefind(constbasic_string&s,size_typei=0)const;
const修饰函数参数是它最广泛的一种用途,它表示函数体中不能修改参数的值(包括参数本身的值或者参数其中包含的值)。
注:
basic_string:
:
npos
Anunsignedintegralvalueinitializedto–1thatindicateseither"notfound"or"allremainingcharacters"whenasearchfunctionfails.
staticconstsize_typenpos=-1;
1、Find查找子串第一次出现的位置
Searchesastringinaforwarddirectionforthefirstoccurrenceofasubstringthatmatchesaspecifiedsequenceofcharacters
最简单的查找操作就是find函数了。
用于寻找实参指定的内容。
如果能找到的话,则返回第一次匹配的下标值;如果找不到,则返回npos。
在此,我们首先定义一个字符串。
Strings(“helloworld”);//定义一个string变量s
String:
:
size_typepos=s.find(args);//args–>参数
if(pos!
=string:
:
npos)
cout< Msdn: findmethod 1.4种重载形式: =s.find(); value_type相当于char //在s中,从下标_off标记的位置开始,查找字符_ch,_off的默认值为0. size_typefind( value_type_Ch,size_type_Off=0)const; //在s中,从下标_off标记的位置开始,查找指针_ptr所指向的C风格的以空字符\0结束的字符串,_off的默认值为0 size_typefind( constvalue_type*_Ptr,size_type_Off=0)const; //在s中,从下标_off标记的位置开始,查找指针_ptr所指向的C风格的以空字符\0结束的字符串数组的前_count个字符 size_typefind( constvalue_type*_Ptr,size_type_Off,size_type_Count)const; //在s中,从下标_off标记的位置开始,查找string对象_str,_off的默认值为0 size_typefind( constbasic_string 2.参数定义: _ChThecharactervalueforwhichthememberfunctionistosearch. _OffIndexofthepositionatwhichthesearchistobegin. _PtrTheC-stringforwhichthememberfunctionistosearch. _CountThenumberofcharacters,countingforwardfromthefirstcharacter,intheC-stringforwhichthememberfunctionistosearch. _StrThestringforwhichthememberfunctionistosearch. 3.实例解说: #include #include usingnamespacestd; intmain() { stringname("AnnaBelle"); intpos1=name.find("Anna"); //intpos1=name.find("Anna",2); //string: : size_typepos1=name.find("Anna",2); //basic_string : size_typepos1=name.find("Anna",2); cout< } Msdn: 实例 2、Rfind 从末端起反向查找子串 Msdn: rfindmethod 1.参数定义 size_typerfind( value_type_Ch, size_type_Off=npos//默认为npos=-1.赋值给unsigned,则为最大值。 )const; size_typerfind( constvalue_type*_Ptr, size_type_Off=npos )const; size_typerfind( constvalue_type*_Ptr, size_type_Off, size_type_Count )const; size_typerfind( constbasic_string size_type_Off=npos )const; 2.实例解说 #include #include intmain() { usingnamespacestd; stringtext("Mississippi"); string: : size_typefirst_pos=text.find("is");//return1; string: : size_typelast_pos=text.rfind("is");//return4; cout< } 3、Find_first_of 查找任意字符 第一次出现的位置 如果在查找字符串时希望匹配任意指定的字符。 看例子。 Msdn: find_first_ofmethod 1.参数定义 size_typefind_first_not_of( value_type_Ch, size_type_Off=0 )const; size_typefind_first_of( constvalue_type*_Ptr, size_type_Off=0 )const; size_typefind_first_of( constvalue_type*_Ptr, size_type_Off, size_type_Count )const; size_typefind_first_of( constbasic_string size_type_Off=0 )const; 2.实例: #include #include intmain() { usingnamespacestd; stringnumerics("0123456789"); stringname("r2d2"); string: : size_typepos=name.find_first_of(numerics); cout<<"Foundnumberatindex: "< cout<<"Elementis: "< } 4、find_first_not_of 查找不在参数里的字符,返回第一个位置 Msdn: find_first_not_of 除了寻找匹配的位置外,还可以调用find_first_not_of函数查找第一个与实参不匹配的位置。 1.参数定义 size_typefind_first_not_of( value_type_Ch, size_type_Off=0 )const; size_typefind_first_not_of( constvalue_type*_Ptr, size_type_Off=0 )const; size_typefind_first_not_of( constvalue_type*_Ptr, size_type_Off, size_type_Count )const; size_typefind_first_not_of( constbasic_string size_type_Off=0 )const; 2.实例解说 #include #include intmain() { usingnamespacestd; stringnumerics("0123456789"); stringname("03714p3"); string: : size_typepos=name.find_first_not_of(numerics); cout<<"Foundnumberatindex: "< cout<<"Elementis: "< } 5、find_last_of 查找任意字符的最后一次出现。 Msdn: find_last_of 1.参数定义 size_typefind_last_of( value_type_Ch, size_type_Off=npos )const; size_typefind_last_of( constvalue_type*_Ptr, size_type_Off=npos )const; size_typefind_last_of( constvalue_type*_Ptr, size_type_Off, size_type_Count )const; size_typefind_last_of( constbasic_string size_type_Off=npos )const; 2.实例解说 6、find_last_not_of 从末端起,反向查找不再参数里的字符。 1.参数定义 2. 实例: 1、findmsdn实例 #include #include chartext1[]="12345"; chartext2[]="012345678901234567890123456789012345678901234567890"; intmain() { usingnamespacestd; //Thefirstmemberfunction //searchesforasinglecharacterinastring stringstr1("HelloEveryone"); cout<<"Theoriginalstringstr1is: "< cout< basic_string : size_typeindexCh1a,indexCh1b; indexCh1a=str1.find("e",3);//'e' if(indexCh1a! =string: : npos) cout<<"Theindexofthe1st'e'foundafterthe3rd" <<"positioninstr1is: "< else cout<<"Thecharacter'e'wasnotfoundinstr1."< indexCh1b=str1.find("x");//默认初始下标 if(indexCh1b! =string: : npos) cout<<"Theindexofthe'x'foundinstr1is: " < else cout<<"TheCharacter'x'wasnotfoundinstr1." < cout<<"=============================================================================="< //Thesecondmemberfunctionsearchesastring //forasubstringasspecifiedbyaC-string stringstr2("Letmemakethisperfectlyclear."); cout<<"Theoriginalstringstr2is: "< cout< basic_string : size_typeindexCh2a,indexCh2b; constchar*cstr2="perfect"; indexCh2a=str2.find(cstr2,5); if(indexCh2a! =string: : npos) cout<<"Theindexofthe1stelementof'perfect'" <<"after\nthe5thpositioninstr2is: " < else cout<<"Thesubstring'perfect'wasnotfoundinstr2." < constchar*cstr2b="imperfectly"; indexCh2b=str2.find(cstr2b,0);//默认初始下标 if(indexCh2b! =string: : npos) cout<<"Theindexofthe1stelementof'imperfect'" <<"after\nthe5thpositioninstr3is: " < else cout<<"Thesubstring'imperfect'wasnotfoundinstr2." < cout<<"=============================================================================="< //Thethirdmemberfunctionsearchesastring //forasubstringasspecifiedbyaC-string stringstr3("Thisisasamplestringforthisprogram"); cout<<"Theoriginalstringstr3is: "< cout< basic_string : size_typeindexCh3a,indexCh3b; constchar*cstr3a="sample"; indexCh3a=str3.find(cstr3a);//默认初始下标 if(indexCh3a! =string: : npos) cout<<"Theindexofthe1stelementofsample" <<"instr3is: "< else cout<<"Thesubstring'perfect'wasnotfoundinstr3." < constchar*cstr3b="for"; indexCh3b=str3.find(cstr3b,indexCh3a+1,2); if(indexCh3b! =string: : npos) cout<<"Theindexofthenextoccurrenceof'for'isin" <<"str3beginsat: "< else cout<<"Thereisnonextoccurrenceof'for'instr3." < cout<<"=============================================================================="< //Thefourthmemberfunctionsearchesastring //forasubstringasspecifiedbyastring stringstr4("clearlythisperfectlyunclear."); cout<<"Theoriginalstringstr4is: "< cout< basic_string : size_typeindexCh4a,indexCh4b; stringstr4a("clear"); indexCh4a=str4.find(str4a,5); if(indexCh4a! =string: : npos) cout<<"Theindexofthe1stelementof'clear'" <<"after\nthe5thpositioninstr4is: " < else cout<<"Thesubstring'clear'wasnotfoundinstr4." < stringstr4b("clear"); indexCh4b=str4.find(str4b);//默认初始下标 if(indexCh4b! =string: : npos) cout<<"Theindexofthe1stelementof'clear'" <<"instr4is: " < else cout<<"Thesubstring'clear'wasnotfoundinstr4." < } 2、const const修饰成员函数 const修饰类的成员函数,则该成员函数不能修改类中任何非const成员函数。 一般写在函数的最后来修饰。 classA { … voidfunction()const;//常成员函数,它不改变对象的成员变量.也不能调用类中任何非const成员函数。 } 对于const类对象/指针/引用,只能调用类的const成员函数,因此,const修饰成员函数的最重要作用就是限制对于const对象的使用。 const修饰类对象/对象指针/对象引用 const修饰类对象表示该对象为常量对象,其中的任何成员都不能被修改。 对于对象指针和对象引用也是一样。 const修饰的对象,该对象的任何非const成员函数都不能被调用,因为任何非const成员函数会有修改成员变量的企图。 例如: classAAA { voidfunc1(); voidfunc2()const; } constAAAaObj; aObj.func1();× aObj.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- String 中字符串查找 字符串 查找