Cstring使用方法
string是C标准模板库中专门用于字符串处理的数据结构类型。它并不是C的基本数据类型,它是C标准模板库中的一个类。若要使用string对象,则必须包含头文件include。
1。初始化
常用的初始化有以下几种,带等号的是拷贝初始化,stringstr1(helloworld);helloworldstringstr2helloworld;helloworldstringstr3(str2);helloworldstringstr4str3;helloworldstringstr5(5,d);dddddstringstr6(str2,6);world,从str2的第6个字符开始到结束,拷贝到str6中stringstr7(str2,0,5);hello,从str2的第0个字符开始拷贝5个字符到str7中charbuff〔〕hellosorld;stringstr8(buff,5);hello,拷贝buff的前5个字符到str8中
特殊数据结构成员
staticconstsizetnpos1;
2。string的基本操作
长度sizetlength()constnoexcept;得到字符串的长度sizetsize()constnoexcept;得到字符串的长度sizetmaxsize()constnoexcept;得到字符串可以达到的最大长度
插入
在指定的位置后面插入一个字符串在pos后面插入字符串strstringinsert(sizetpos,conststringstr);在pos后面插入str的subpos处往后的sublen长度的字符串stringinsert(sizetpos,conststringstr,sizetsubpos,sizetsublen);在pos后面插入字符数组sstringinsert(sizetpos,constchars);在pos后面插入字符数组s的前n个字符stringinsert(sizetpos,constchars,sizetn);在pos后面插入n个字符cstringinsert(sizetpos,sizetn,charc);在p后面插入n个字符citeratorinsert(constiteratorp,sizetn,charc);在p后面插入一个字符citeratorinsert(constiteratorp,charc);在p后面插入迭代器first到last之间的字符串templateclassInputIteratoriteratorinsert(iteratorp,InputIteratorfirst,InputIteratorlast);在p后面插入il内的所有字符stringinsert(constiteratorp,initializerlistcharil);
替换
把指定的位置后指定长度的字符串替换成另一个字符串把pos后面len长度的字符串替换成strstringreplace(sizetpos,sizetlen,conststringstr);把i1和i2之间的内容替换成strstringreplace(constiteratori1,constiteratori2,conststringstr);把pos后面len长度的字符串替换成字符串str的subpos后面的sublen个长度的字符串stringreplace(sizetpos,sizetlen,conststringstr,sizetsubpos,sizetsublen);把pos后面len长度的字符串替换成字符数组s里的所有内容stringreplace(sizetpos,sizetlen,constchars);把i1和i2之间的字符串替换成数组s里的所有内容stringreplace(constiteratori1,constiteratori2,constchars);把pos后面len长度的字符串替换成字符数组s里面前n个字符stringreplace(sizetpos,sizetlen,constchars,sizetn);把i1和i2之间的字符串替换成字符数组s里面的前n个字符stringreplace(constiteratori1,constiteratori2,constchars,sizetn);把pos后面len长度的字符串替换成n个字符cstringreplace(sizetpos,sizetlen,sizetn,charc);把i1和i2之间的字符串替换成n个字符cstringreplace(constiteratori1,constiteratori2,sizetn,charc);把i1和i2之间的字符串替换成迭代器first与last之间的内容templateclassInputIteratorstringreplace(constiteratori1,constiteratori2,InputIteratorfirst,InputIteratorlast);把字符串i1和i2之间的内容替换成il里的所有字符stringreplace(constiteratori1,constiteratori2,initializerlistcharil);
添加
在字符串的末尾添加另一个字符串的内容在字符串的末尾添加另一个字符串strstringappend(conststringstr);stringoperator(conststringstr);在字符串的末尾添加两一个字符串str的subpos后面sublen长度的字符串stringappend(conststringstr,sizetsubpos,sizetsublen);在字符串的末尾添加字符数组s里的所有内容stringappend(constchars);stringoperator(constchars);在字符串的末尾添加字符数组s的前n个字符stringappend(constchars,sizetn);在字符串的末尾添加n个字符cstringappend(sizetn,charc);在字符串的末尾添加一个字符cstringoperator(charc);在字符串的末尾添加迭代器first与last之间的字符templateclassInputIteratorstringappend(InputIteratorfirst,InputIteratorlast);在字符串的末尾添加il里的所有内容stringappend(initializerlistcharil);stringoperator(initializerlistcharil);
赋值
用新的字符串替换掉本字符串的内容用str替换掉本字符串的内容stringassign(conststringstr);stringoperator(conststringstr);用str里的subpos后面sublen个长度的字符串替换掉本字符串的内容stringassign(conststringstr,sizetsubpos,sizetsublen);用字符数组s里的的所有字符替换掉本字符串的内容stringassign(constchars);stringoperator(constchars);用字符数组s的前n个字符替换掉本字符串里的内容stringassign(constchars,sizetn);把本字符串替换成n个字符cstringassign(sizetn,charc);用迭代器first和last之间的字符替换掉本字符串的内容templateclassInputIteratorstringassign(InputIteratorfirst,InputIteratorlast);用il里的所有字符串替换掉本字符串的内容stringassign(initializerlistcharil);stringoperator(initializerlistcharil);用str替换掉本字符串的内容stringassign(stringstr)noexcept;stringoperator(stringstr)noexcept;将字符串的长度置为1,并把字符C填充到字符串里stringoperator(charc);
删除
删除指定位置后面指定长度的字符删除pos后面len个长度的字符stringerase(sizetpos0,sizetlennpos);删除迭代器p到末尾的所有字符iteratorerase(iteratorp);删除迭代器first与last之间的字符iteratorerase(iteratorfirst,iteratorlast);
清空
清空字符串,得到一个空的字符串清空字符串voidclear()noexcept;
为空
判断字符串的内容是否为空判断字符串是否为空boolempty()constnoexcept;
剪切
得到指定位置后面指定长度的字符串返回pos后面len个长度的字符串stringsubstr(sizetpos0,sizetlennpos)const;
比较
指定位置后面指定长度的字符串与另一个字符串进行比较
返回值:0,两个字符串相等;
0,参与比较的字符串不匹配的第一个字符的值较低,或者所有比较的字符都匹配但参与比较的字符串较短;
0,参与比较的字符串不匹配的第一个字符的值更大,或者所有比较的字符都匹配但参与比较的字符串更长。本字符串与str进行比较intcompare(conststringstr)constnoexcept;本字符串pos后面len长度的字符串与str进行比较intcompare(sizetpos,sizetlen,conststringstr)const;本字符串pos后面len长度的字符串与str的subpos位置后面sublen长度的字符串进行比较intcompare(sizetpos,sizetlen,conststringstr,sizetsubpos,sizetsublen)const;本字符串与字符数组的所有字符进行比较intcompare(constchars)const;本字符串pos后面len长度的字符串与字符数组s里的所有字符进行比较intcompare(sizetpos,sizetlen,constchars)const;本字符串pos后面len长度的字符串与字符数组s里前n个字符进行比较intcompare(sizetpos,sizetlen,constchars,sizetn)const;
交换
与另一个字符串交换它们的内容与str交换内容voidswap(stringstr);
修改长度
请求修改字符串容量的大小,长度最多为n个字符。
如果n大于当前的字符串容量,则该函数会使容器将其容量增加到n个字符(或更大)。
在所有其他情况下,它都被视为缩小字符串容量的非绑定请求:容器实现可以自由地进行优化,并使字符串的容量大于n。
此函数对字符串长度没有影响,也不能改变其内容。将字符串的长度重置为nvoidreserve(sizetn0);
重设
把字符串重新设置成指定的长度的字符
如果重设后的长度小于原长度,删除多余的字符
如果重设后的长度大于原长度,则在结尾处插入空字符或指定的字符到达指定的长度将字符串重设成n个长度的空字符串voidresize(sizetn);将字符串重设成n个长度的c字符voidresize(sizetn,charc);
删除末尾字符
删除字符串末尾的一个字符弹出字符串末尾的一个字符voidpopback();
在末尾添加字符
在字符串的末尾添加一个字符在字符串的末尾添加一个字符cvoidpushback(charc);
得到C类型的字符串
转换成等价的C字符串转换成等价的C字符串constcharcstr()constnoexcept;得到该字符串的数组指针constchardata()constnoexcept;
取字符
得到指定位置处的字符得到指定位置处的字符charat(sizetpos);constcharat(sizetpos)const;charoperator〔〕(sizetpos);constcharoperator〔〕(sizetpos)const;得到字符串的第一个字符charfront();constcharfront()const;得到字符串的最后一个字符charback();constcharback()const;
拷贝
将字符串的指定内容拷贝到字符数组里将字符串pos处开始的len长度的字符串拷贝到字符数组s里面sizetcopy(chars,sizetlen,sizetpos0)const;
交换
交换两个字符串里的内容交换x换y的值voidswap(stringx,stringy);
查找
查找字符串中指定字符或字符串出现的第一处位置
如果没有匹配的,返回string::npos从字符串的pos处开始查找与字符串str相同的字符串sizetfind(conststringstr,sizetpos0)const;从字符串的pos处开始查找与字符数组s相同的字符串sizetfind(constchars,sizetpos0)const;从字符串的pos处开始长度为n的范围内查找与字符数组s相同的字符串sizetfind(constchars,sizetpos,sizetn)const;从字符串串的pos处开始查找与字符c相同的字符sizetfind(charc,sizetpos0)const;
查找字符串中指定字符或字符串出现的最后一处位置
如果没有匹配的,返回string::npos查找在字符串pos之前的最后一个与字符串str相匹配的字符串的位置sizetrfind(conststringstr,sizetposnpos)constnoexcept;查找在字符串pos之前的最后一个与字符数组s相匹配的字符串的位置sizetrfind(constchars,sizetposnpos)const;查找在字符串pos之前的n个字符内最后一个与字符数组s相匹配的字符串的位置sizetrfind(constchars,sizetpos,sizetn)const;查找在字符串pos之前的最后一个与字符c匹配的字符的位置sizetrfind(charc,sizetposnpos)constnoexcept;
查找字符串中与其参数中指定的任何字符匹配的第一个字符
如果没有匹配的,返回string::npos查找字符串中的pos处开始与字符串str里的任一字符相同的第一个位置sizetfindfirstof(conststringstr,sizetpos0)constnoexcept;查找字符串中的pos处开始与字符数组s里的任一字符相同的第一个位置sizetfindfirstof(constchars,sizetpos0)const;查找字符串中的pos处开始长度为n的字符串内与字符数组s里的任一字符相同的第一个位置sizetfindfirstof(constchars,sizetpos,sizetn)const;查找字符串中的pos处开始第一个与字符c相同的位置sizetfindfirstof(charc,sizetpos0)constnoexcept;
查找字符串中与其参数中指定的任何字符匹配的最后一个字符
如果没有匹配的,返回string::npos查找字符串中的pos之前的所有字符与字符串str里的任一字符相同的最后一个位置sizetfindlastof(conststringstr,sizetposnpos)constnoexcept;查找字符串中的pos之前的所有字符与字符数组s里的任一字符相同的最后一个位置sizetfindlastof(constchars,sizetposnpos)const;查找字符串中的pos之前长度为n的字符串内与字符数组s里的任一字符相同的最后一个位置sizetfindlastof(constchars,sizetpos,sizetn)const;查找字符串中的pos之前的所有字符里最后一个与字符c相同的位置sizetfindlastof(charc,sizetposnpos)constnoexcept;
查找字符串中与指定的字符数组或字符串里的任一字符都不匹配的第一个位置
如果没有找到(即参与比较的部分完全相同),返回string::npos比较字符串pos后面的全部字符与字符串str里的任一字符都不同的第一个字符所在的位置sizetfindfirstnotof(conststringstr,sizetpos0)constnoexcept;比较字符串pos后面的全部字符与字符数组s里的任一字符都不同的第一个字符所在的位置sizetfindfirstnotof(constchars,sizetpos0)const;比较字符串pos后面的n个字符与字符数组s里的任一字符都不同的第一个字符所在的位置sizetfindfirstnotof(constchars,sizetpos,sizetn)const;比较字符串pos后面的全部字符与字符c不同的第一个字符所在的位置sizetfindfirstnotof(charc,sizetpos0)constnoexcept;
查找字符串中与指定的字符数组或字符串里的任一字符都不匹配的最后一个位置
如果没有找到(即参与比较的部分完全相同),返回string::npos比较字符串pos前面的全部字符与字符串str里的任一字符都不同的最后一个字符所在的位置sizetfindlastnotof(conststringstr,sizetposnpos)constnoexcept;比较字符串pos前面的全部字符与字符数组s里的任一字符都不同的最后一个字符所在的位置sizetfindlastnotof(constchars,sizetposnpos)const;比较字符串pos前面的n个字符与字符数组s里的任一字符都不同的最后一个字符所在的位置sizetfindlastnotof(constchars,sizetpos,sizetn)const;比较字符串pos前面的全部字符与字符c不同的最后一个字符所在的位置sizetfindlastnotof(charc,sizetposnpos)constnoexcept;
operator
重载运算符
返回一个新构造的字符串对象,其值为lhs中的字符和rhs中的字符的连接stringoperator(conststringlhs,conststringrhs);stringoperator(stringlhs,stringrhs);stringoperator(stringlhs,conststringrhs);stringoperator(conststringlhs,stringrhs);stringoperator(conststringlhs,constcharrhs);stringoperator(stringlhs,constcharrhs);stringoperator(constcharlhs,conststringrhs);stringoperator(constcharlhs,stringrhs);stringoperator(conststringlhs,charrhs);stringoperator(stringlhs,charrhs);stringoperator(charlhs,conststringrhs);stringoperator(charlhs,stringrhs);
重载运算符
比较lhs与rhs是否相等booloperator(conststringlhs,conststringrhs)noexcept;booloperator(constcharlhs,conststringrhs);booloperator(conststringlhs,constcharrhs);
重载!运算符
比较lhs与rhs是否不等booloperator!(conststringlhs,conststringrhs)noexcept;booloperator!(constcharlhs,conststringrhs);booloperator!(conststringlhs,constcharrhs);
重载运算符
比较lhs是否小于rhsbooloperator(conststringlhs,conststringrhs)noexcept;booloperator(constcharlhs,conststringrhs);booloperator(conststringlhs,constcharrhs);
重载运算符
比较lhs是否小于等于rhsbooloperator(conststringlhs,conststringrhs)noexcept;booloperator(constcharlhs,conststringrhs);booloperator(conststringlhs,constcharrhs);
重载运算符
比较lhs是否大于rhsbooloperator(conststringlhs,conststringrhs)noexcept;booloperator(constcharlhs,conststringrhs);booloperator(conststringlhs,constcharrhs);
重载运算符
比较lhs是否大于等于rhsbooloperator(conststringlhs,conststringrhs)noexcept;booloperator(constcharlhs,conststringrhs);booloperator(conststringlhs,constcharrhs);
输入输出流
从流中插入获取字符串从流中提取字符串istreamoperator(istreamis,stringstr);将字符串插入流ostreamoperator(ostreamos,conststringstr);从流is中提取一行字符串到str中,直到划分字符delim为止istreamgetline(istreamis,stringstr,chardelim);istreamgetline(istreamis,stringstr,chardelim);从流is中提取一行字符串到str中,直到换行为止istreamgetline(istreamis,stringstr);istreamgetline(istreamis,stringstr);
原文链接:链接