资源描述:
《c++语言知识点CPPstring》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、std::stringStringinC++StandardLibrarystd::stringConstructor构造函数Assign赋值Append附加Insert插入Concatenate拼接Substring子串Erase删除Replace替换Compare比较Size大小Elements元素Find查找ToCstringC风格串Swap交换I/O输入输出1.Constructorstring()string(s)string(s,pos,n)string(p)string(p,n)string(n,c)2.Assignx.assign(s)x.assign(s,pos,n
2、)x.assign(p)x.assign(p,n)x.assign(n,c)x=sx=px=c3.Appendx.append(s)x.append(s,pos,n)x.append(p)x.append(p,n)x.append(n,c)x+=sx+=px+=c4.Insertx.insert(pos,s)x.insert(pos,s,pos2,n)x.insert(pos,p)x.insert(pos,p,n)x.insert(pos,n,c)5.Concatenatex+sx+pp+xx+cc+x6.Substringx.substr(pos=0,n=npos)NOTE:std
3、::string::npos表示串中字符的最大位置i.e.x.substr(pos,n)x.substr(pos)x.substr()stringx=“abcdef”;x.substr(2,3);//”cde”x.substr(2,100);//”cdef”x.substr(2);//“cdef”x.substr();//“abcdef”7.Erasex.erase(pos=0,n=npos)x.clear()i.e.x.erase(pos,n)x.erase(pos)x.erase()i.e.x.clear()8.Replacex.replace(pos,n,s)x.replace
4、(pos,n,s,pos2,n2)x.replace(pos,n,p)x.replace(pos,n,p,n2)x.replace(pos,n,n2,c)9.Comparex.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+-truefalseExamplex==sx==pp==x10.Sizex.size()x.length()x.empty()11.Elementsx[i]x.at(i)Examplex[i]=c;x.at
5、(i)=c;std::cout<>x[i];12.Findfindrfindfind_first_offind_last_offind_first_not_offind_last_not_ofx.find(s,i=0)x.find(p,i=0)x.find(p,i,n)x.find(c,i=0)findExamplex.find(s);x.find(s,i);rfindx.rfind(s,i=npos)x.rfind(p,i=npos)x.rfind(p,i,n)x.rfind(c,i=npos)Examplex.rfind(s);x.rfind(s,i
6、);find_first_ofx.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_ofx.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_ofx.find_first_not_of(s,i=0)x.find_first_not_of(p,i=0)x.find_first_not_o
7、f(p,i,n)x.find_first_not_of(c,i=0)find_last_not_ofx.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)Usageoffind返回值类型string::size_type返回值找到:返回匹配位置找不到:返回nposExample:string::