String 中字符串查找Word文档下载推荐.docx

上传人:b****6 文档编号:20853671 上传时间:2023-01-26 格式:DOCX 页数:13 大小:30.28KB
下载 相关 举报
String 中字符串查找Word文档下载推荐.docx_第1页
第1页 / 共13页
String 中字符串查找Word文档下载推荐.docx_第2页
第2页 / 共13页
String 中字符串查找Word文档下载推荐.docx_第3页
第3页 / 共13页
String 中字符串查找Word文档下载推荐.docx_第4页
第4页 / 共13页
String 中字符串查找Word文档下载推荐.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

String 中字符串查找Word文档下载推荐.docx

《String 中字符串查找Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《String 中字符串查找Word文档下载推荐.docx(13页珍藏版)》请在冰豆网上搜索。

String 中字符串查找Word文档下载推荐.docx

Searchesastringinaforwarddirectionforthefirstoccurrenceofasubstringthatmatchesaspecifiedsequenceofcharacters

最简单的查找操作就是find函数了。

用于寻找实参指定的内容。

如果能找到的话,则返回第一次匹配的下标值;

如果找不到,则返回npos。

在此,我们首先定义一个字符串。

Strings(“helloworld”);

//定义一个string变量s

String:

size_typepos=s.find(args);

//args–>

参数

if(pos!

=string:

npos)

cout<

<

pos;

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

constvalue_type*_Ptr,size_type_Off=0)const;

//在s中,从下标_off标记的位置开始,查找指针_ptr所指向的C风格的以空字符\0结束的字符串数组的前_count个字符

constvalue_type*_Ptr,size_type_Off,size_type_Count)const;

//在s中,从下标_off标记的位置开始,查找string对象_str,_off的默认值为0

constbasic_string<

CharType,Traits,Allocator>

&

_Str,size_type_Off=0)const;

2.参数定义:

_ChThecharactervalueforwhichthememberfunctionistosearch.

_OffIndexofthepositionatwhichthesearchistobegin.

_PtrTheC-stringforwhichthememberfunctionistosearch.

_CountThenumberofcharacters,countingforwardfromthefirstcharacter,intheC-stringforwhichthememberfunctionistosearch.

_StrThestringforwhichthememberfunctionistosearch.

3.实例解说:

#include<

string>

iostream>

usingnamespacestd;

intmain()

{

stringname("

AnnaBelle"

);

intpos1=name.find("

Anna"

//intpos1=name.find("

2);

//string:

size_typepos1=name.find("

//basic_string<

char>

pos1;

}

实例

2、Rfind 

从末端起反向查找子串

Msdn:

rfindmethod

1.参数定义

size_typerfind(

value_type_Ch,

size_type_Off=npos//默认为npos=-1.赋值给unsigned,则为最大值。

)const;

constvalue_type*_Ptr,

size_type_Off=npos

constvalue_type*_Ptr,

size_type_Off,

size_type_Count

constbasic_string<

&

_Str,

2.实例解说

intmain()

usingnamespacestd;

stringtext("

Mississippi"

string:

size_typefirst_pos=text.find("

is"

//return1;

size_typelast_pos=text.rfind("

//return4;

first_pos<

endl<

last_pos;

3、Find_first_of 

查找任意字符 

第一次出现的位置

如果在查找字符串时希望匹配任意指定的字符。

看例子。

find_first_ofmethod

size_typefind_first_not_of(

size_type_Off=0

size_typefind_first_of(

2.实例:

stringnumerics("

0123456789"

r2d2"

size_typepos=name.find_first_of(numerics);

"

Foundnumberatindex:

pos<

endl;

Elementis:

name[pos]<

4、find_first_not_of 

查找不在参数里的字符,返回第一个位置

find_first_not_of

除了寻找匹配的位置外,还可以调用find_first_not_of函数查找第一个与实参不匹配的位置。

value_type_Ch,

size_type_Off=0

constvalue_type*_Ptr,

size_type_Off=0

constvalue_type*_Ptr,

size_type_Off,

size_type_Count

_Str,

size_type_Off=0

03714p3"

size_typepos=name.find_first_not_of(numerics);

5、find_last_of 

查找任意字符的最后一次出现。

find_last_of

size_typefind_last_of(

6、find_last_not_of 

从末端起,反向查找不再参数里的字符。

2.

实例:

1、findmsdn实例

chartext1[]="

12345"

;

chartext2[]="

012345678901234567890123456789012345678901234567890"

//Thefirstmemberfunction

//searchesforasinglecharacterinastring

stringstr1("

HelloEveryone"

);

cout<

"

Theoriginalstringstr1is:

<

str1<

endl;

text2<

text1<

//游标卡尺

basic_string<

size_typeindexCh1a,indexCh1b;

indexCh1a=str1.find("

e"

3);

//'

e'

if(indexCh1a!

cout<

Theindexofthe1st'

foundafterthe3rd"

<

positioninstr1is:

indexCh1a<

else

Thecharacter'

wasnotfoundinstr1."

indexCh1b=str1.find("

x"

//默认初始下标

if(indexCh1b!

Theindexofthe'

x'

foundinstr1is:

indexCh1b<

endl<

TheCharacter'

wasnotfoundinstr1."

cout<

=============================================================================="

//Thesecondmemberfunctionsearchesastring

//forasubstringasspecifiedbyaC-string

stringstr2("

Letmemakethisperfectlyclear."

Theoriginalstringstr2is:

endl<

str2<

size_typeindexCh2a,indexCh2b;

constchar*cstr2="

perfect"

indexCh2a=str2.find(cstr2,5);

if(indexCh2a!

Theindexofthe1stelementof'

perfect'

after\nthe5thpositioninstr2is:

indexCh2a<

Thesubstring'

wasnotfoundinstr2."

constchar*cstr2b="

imperfectly"

indexCh2b=str2.find(cstr2b,0);

if(indexCh2b!

imperfect'

after\nthe5thpositioninstr3is:

indexCh2b<

//Thethirdmemberfunctionsearchesastring

stringstr3("

Thisisasamplestringforthisprogram"

Theoriginalstringstr3is:

str3<

size_typeindexCh3a,indexCh3b;

constchar*cstr3a="

sample"

indexCh3a=str3.find(cstr3a);

if(indexCh3a!

Theindexofthe1stelementofsample"

instr3is:

indexCh3a<

wasnotfoundinstr3."

constchar*cstr3b="

for"

indexCh3b=str3.find(cstr3b,indexCh3a+1,2);

if(indexCh3b!

Theindexofthenextoccurrenceof'

for'

isin"

str3beginsat:

indexCh3b<

Thereisnonextoccurrenceof'

instr3."

//Thefourthmemberfunctionsearchesastring

//forasubstringasspecifiedbyastring

stringstr4("

clearlythisperfectlyunclear."

Theoriginalstringstr4is:

str4<

size_typeindexCh4a,indexCh4b;

stringstr4a("

clear"

indexCh4a=str4.find(str4a,5);

if(indexCh4a!

clear'

after\nthe5thpositioninstr4is:

indexCh4a<

wasnotfoundinstr4."

stringstr4b("

indexCh4b=str4.find(str4b);

if(indexCh4b!

instr4is:

indexCh4b<

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.

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 党团工作 > 思想汇报心得体会

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1