资源描述
单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,std:string,String in C+Standard Library,std:string,Constructor,构造函数,Assign,赋值,Append,附加,Insert,插入,Concatenate,拼接,Sub string,子串,Erase,删除,Replace,替换,Compare,比较,Size,大小,Elements,元素,Find,查找,To C string,C,风格串,Swap,交换,I/O,输入输出,1.Constructor,string,(),string,(,s),string,(,s,pos,n),string,(,p),string,(,p,n),string,(n,c),2.Assign,x.,assign,(s),x.,assign,(s,pos,n),x.,assign,(p),x.,assign,(p,n),x.,assign,(n,c),x,=,s,x,=,p,x,=,c,3.Append,x.,append,(s),x.,append,(s,pos,n),x.,append,(p),x.,append,(p,n),x.,append,(n,c),x,+=,s,x,+=,p,x,+=,c,4.,Insert,x.,insert,(pos,s),x.,insert,(pos,s,pos2,n),x.,insert,(pos,p),x.,insert,(pos,p,n),x.,insert,(pos,n,c),5.,Concatenate,x,+,s,x,+,p,p,+,x,x,+,c,c,+,x,6.Sub string,x.,substr,(pos=0,n=,npos,),NOTE:,std:string:,npos,表示串中字符的最大位置,i.e.,x.,substr,(pos,n),x.,substr,(pos),x.,substr,(),string x=“,abcdef,”;,x.,substr,(2,3);/”,cde,”,x.,substr,(2,100);/”,cdef,”,x.,substr,(2);/“,cdef,”,x.,substr,();/“,abcdef,”,7.,Erase,x.,erase,(pos=0,n=,npos,),x.,clear,(),i.e.,x.erase(pos,n),x.erase(pos),x.erase()i.e.x.clear(),8.,Replace,x.,replace,(pos,n,s),x.,replace,(pos,n,s,pos2,n2),x.,replace,(pos,n,p),x.,replace,(pos,n,p,n2),x.,replace,(pos,n,n2,c),9.,Compare,x.,compare,(s),x.,compare,(p),x.,compare,(pos,n,s),x.,compare,(pos,n,s,pos2,n2),x.,compare,(pos,n,p,n2=,npos,),=!=,0 +-,true false,Example,x=s x=p p=x,10.,Size,x.,size,(),x.,length,(),x.,empty,(),11.,Elements,x,i,x.,at,(i),Example,xi=c;,x.at(i)=c;,std:,cout,xi;,12.,Find,find,rfind,find_first_of,find_last_of,find_first_not_of,find_last_not_of,x.,find,(s,i=0),x.,find,(p,i=0),x.,find,(p,i,n),x.,find,(c,i=0),find,Example,x.find(s);,x.find(s,i);,rfind,x.,rfind,(s,i=,npos,),x.,rfind,(p,i=,npos,),x.,rfind,(p,i,n),x.,rfind,(c,i=,npos,),Example,x.,rfind,(s);,x.,rfind,(s,i);,find_first_of,x.,find_first_of,(s,i=0),x.,find_first_of,(p,i=0),x.,find_first_of,(p,i,n),x.,find_first_of,(c,i=0),find_last_of,x.,find_last_of,(s,i=,npos,),x.,find_last_of,(p,i=,npos,),x.,find_last_of,(p,i,n),x.,find_last_of,(c,i=,npos,),find_first_not_of,x.,find_first_not_of,(s,i=0),x.,find_first_not_of,(p,i=0),x.,find_first_not_of,(p,i,n),x.,find_first_not_of,(c,i=0),find_last_not_of,x.,find_last_not_of,(s,i=,npos,),x.,find_last_not_of,(p,i=,npos,),x.,find_last_not_of,(p,i,n),x.,find_last_not_of,(c,i=,npos,),Usage of find,返回值类型,string:,size_type,返回值,找到:返回,匹配位置,找不到:返回,npos,Example:,string:size_type i=x.find(s);if(i!=string:,npos,),/found,else,/not found,An example of find,string s=“,accdcde,”;,string:size_type i1,i2,i3,i4,i5,i6;,i1=s.,find,(“,cd,”);,/,2,:s2=c&s3=d,i2=s.,rfind,(“,cd,”);,/,4,:s4=c&s5=d,i3=s.,find_first_of,(“,cd,”);,/,1,:s1=c in“,cd,”,i4=s.,find_last_of,(“,cd,”);,/,5,:s5=d in“,cd,”,i5=s.,find_first_not_of,(“,cd,”);,/,0,:s0!=c&s0!=d,i6=s.,find_last_not_of,(“,cd,”);,/,6,:s6!=c&s6!=d,13.,To C string,x.,c_,str,(),x.,copy,(p,n,pos=0),14.,Swap,std:,swap,(x,s),15.,I/O,#,include,os,x,Example:,#include,/std:,cin,and std:,cout,#include,/for and x;,std:,cout,x;,A,DVICES,尽量使用,string,,,少用,C,风格字符串函数;,将,string,作为参数或者返回值,让系统负责存储管理;,用,substr,(),去读子串,用,replace,(),去写子串;,用,find,(),在,string,里确定位置;,在,string,的末尾附加具有较高的效率;,用,string:,npos,表示“,string,的剩余部分”;,没有极端时间要求时,用,string,作为字符输入的目标;,只在必须做的时候,才用,c_,str,(),产生,string,的,C,风格表示。,参考了,Bjarne Stroustrup,C+,程序设计语言(特别版),2002,
展开阅读全文