深入学习C++String21版经过个人的修改感谢原著作者.docx
《深入学习C++String21版经过个人的修改感谢原著作者.docx》由会员分享,可在线阅读,更多相关《深入学习C++String21版经过个人的修改感谢原著作者.docx(25页珍藏版)》请在冰点文库上搜索。
深入学习C++String21版经过个人的修改感谢原著作者
目录
目录错误!
未定义书签。
一、C++的string的使用错误!
1.1C++string简介错误!
1.2string的成员错误!
1.2.1append2
1.2.2assign3
1.2.3at4
#include4
usingnamespacestd;4
intmain()4
{4
stringa,b;4
a="abcde";4
b="fghij";4
charx[]="123456";4
cout<return0;4}41.2.4begin41.2.5clear41.2.6compare41.2.7copy41.2.8empty51.2.9end51.2.10erase51.2.11find51.2.12find_first_not_of61.2.13find_first_of61.2.14find_last_not_of71.2.15find_last_of71.2.16insert71.2.17length81.2.18max_size81.2.19rbegin81.2.20rend81.2.21replace81.2.22rfind91.2.23size101.2.24substr101.2.25swap101.3string与algorithm相结合的使用101.5.1string与remove101.5.2string与unique、sort111.5.3string与search111.5.4string和find、find_if121.5.5string与copy、copy_if121.5.6string与count、count_if13 一、C++的string的使用1.1C++string简介C++兼容C对字符串的处理方式,与此同时还在标准库(STL)中提供了string容器,我们可以很容易的使用string来进行字符串处理。而且string还能很好的与标准库中的泛型算法结合起来使用,非常的方便。虽然在MFC等框架中也提供了诸如CString这样的字符串处理类,但是个人认为STL的string依然是最棒的,使用标准库提供的string可以轻松的与原来的CAPI兼容,也可以很好的与系统底层的API兼容。1.2string的成员1.2.1append在尾部添加字符或者字符串append共有8种重载:#include#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<<1<a.append(x);//第一种重载方法cout<//输出:abcde123456cout<<2<a="abcde";a.append(x,3);//第二种重载分方法cout<//输出:abcde123cout<<3<a="abcde";a.append(b,0,2);//第三种重载方法cout<//输出:abcdefgcout<<4<a="abcde";a.append(b);cout<//输出:abcdefghijcout<<5<a="abcde";a.append(5,'0');//第五种重载方法cout<//输出:abcde00000cout<<6<a="abcde";string::const_iteratori=a.begin(),i1=a.end();a.append(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:abcdeabcdecout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.append(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:abcde987654321return0;}1.2.2assign为字符串重新赋予新的内容。你可以将它看做先把字符串清空,然后再append。因此assign也有8种重载。例子:#include#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<<1<a.assign(x);//第一种重载方法cout<//输出:123456cout<<2<a="abcde";a.assign(x,3);//第二种重载分方法cout<//输出:123cout<<3<a="abcde";a.assign(b,0,2);//第三种重载方法cout<//输出:fgcout<<4<a="abcde";a.assign(b);cout<//输出:fghijcout<<5<a="abcde";a.assign(5,'0');//第五种重载方法cout<//输出:00000cout<<6<a="abcde";b="edcba";string::const_iteratori=b.begin(),i1=b.end();a.assign(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:edcbacout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.assign(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:987654321return0;}1.2.3at#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
return0;4
}4
1.2.4begin4
1.2.5clear4
1.2.6compare4
1.2.7copy4
1.2.8empty5
1.2.9end5
1.2.10erase5
1.2.11find5
1.2.12find_first_not_of6
1.2.13find_first_of6
1.2.14find_last_not_of7
1.2.15find_last_of7
1.2.16insert7
1.2.17length8
1.2.18max_size8
1.2.19rbegin8
1.2.20rend8
1.2.21replace8
1.2.22rfind9
1.2.23size10
1.2.24substr10
1.2.25swap10
1.3string与algorithm相结合的使用10
1.5.1string与remove10
1.5.2string与unique、sort11
1.5.3string与search11
1.5.4string和find、find_if12
1.5.5string与copy、copy_if12
1.5.6string与count、count_if13
一、C++的string的使用
1.1C++string简介
C++兼容C对字符串的处理方式,与此同时还在标准库(STL)中提供了string容器,我们可以很容易的使用string来进行字符串处理。
而且string还能很好的与标准库中的泛型算法结合起来使用,非常的方便。
虽然在MFC等框架中也提供了诸如CString这样的字符串处理类,但是个人认为STL的string依然是最棒的,使用标准库提供的string可以轻松的与原来的CAPI兼容,也可以很好的与系统底层的API兼容。
1.2string的成员
1.2.1append
在尾部添加字符或者字符串
append共有8种重载:
#include
usingnamespacestd;
intmain()
{
stringa,b;
a="abcde";
b="fghij";
charx[]="123456";
cout<<1<a.append(x);//第一种重载方法cout<//输出:abcde123456cout<<2<a="abcde";a.append(x,3);//第二种重载分方法cout<//输出:abcde123cout<<3<a="abcde";a.append(b,0,2);//第三种重载方法cout<//输出:abcdefgcout<<4<a="abcde";a.append(b);cout<//输出:abcdefghijcout<<5<a="abcde";a.append(5,'0');//第五种重载方法cout<//输出:abcde00000cout<<6<a="abcde";string::const_iteratori=a.begin(),i1=a.end();a.append(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:abcdeabcdecout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.append(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:abcde987654321return0;}1.2.2assign为字符串重新赋予新的内容。你可以将它看做先把字符串清空,然后再append。因此assign也有8种重载。例子:#include#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<<1<a.assign(x);//第一种重载方法cout<//输出:123456cout<<2<a="abcde";a.assign(x,3);//第二种重载分方法cout<//输出:123cout<<3<a="abcde";a.assign(b,0,2);//第三种重载方法cout<//输出:fgcout<<4<a="abcde";a.assign(b);cout<//输出:fghijcout<<5<a="abcde";a.assign(5,'0');//第五种重载方法cout<//输出:00000cout<<6<a="abcde";b="edcba";string::const_iteratori=b.begin(),i1=b.end();a.assign(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:edcbacout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.assign(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:987654321return0;}1.2.3at#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
a.append(x);//第一种重载方法
cout<//输出:abcde123456cout<<2<a="abcde";a.append(x,3);//第二种重载分方法cout<//输出:abcde123cout<<3<a="abcde";a.append(b,0,2);//第三种重载方法cout<//输出:abcdefgcout<<4<a="abcde";a.append(b);cout<//输出:abcdefghijcout<<5<a="abcde";a.append(5,'0');//第五种重载方法cout<//输出:abcde00000cout<<6<a="abcde";string::const_iteratori=a.begin(),i1=a.end();a.append(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:abcdeabcdecout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.append(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:abcde987654321return0;}1.2.2assign为字符串重新赋予新的内容。你可以将它看做先把字符串清空,然后再append。因此assign也有8种重载。例子:#include#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<<1<a.assign(x);//第一种重载方法cout<//输出:123456cout<<2<a="abcde";a.assign(x,3);//第二种重载分方法cout<//输出:123cout<<3<a="abcde";a.assign(b,0,2);//第三种重载方法cout<//输出:fgcout<<4<a="abcde";a.assign(b);cout<//输出:fghijcout<<5<a="abcde";a.assign(5,'0');//第五种重载方法cout<//输出:00000cout<<6<a="abcde";b="edcba";string::const_iteratori=b.begin(),i1=b.end();a.assign(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:edcbacout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.assign(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:987654321return0;}1.2.3at#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
//输出:
abcde123456
cout<<2<a="abcde";a.append(x,3);//第二种重载分方法cout<//输出:abcde123cout<<3<a="abcde";a.append(b,0,2);//第三种重载方法cout<//输出:abcdefgcout<<4<a="abcde";a.append(b);cout<//输出:abcdefghijcout<<5<a="abcde";a.append(5,'0');//第五种重载方法cout<//输出:abcde00000cout<<6<a="abcde";string::const_iteratori=a.begin(),i1=a.end();a.append(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:abcdeabcdecout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.append(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:abcde987654321return0;}1.2.2assign为字符串重新赋予新的内容。你可以将它看做先把字符串清空,然后再append。因此assign也有8种重载。例子:#include#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<<1<a.assign(x);//第一种重载方法cout<//输出:123456cout<<2<a="abcde";a.assign(x,3);//第二种重载分方法cout<//输出:123cout<<3<a="abcde";a.assign(b,0,2);//第三种重载方法cout<//输出:fgcout<<4<a="abcde";a.assign(b);cout<//输出:fghijcout<<5<a="abcde";a.assign(5,'0');//第五种重载方法cout<//输出:00000cout<<6<a="abcde";b="edcba";string::const_iteratori=b.begin(),i1=b.end();a.assign(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:edcbacout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.assign(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:987654321return0;}1.2.3at#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
a.append(x,3);//第二种重载分方法
cout<//输出:abcde123cout<<3<a="abcde";a.append(b,0,2);//第三种重载方法cout<//输出:abcdefgcout<<4<a="abcde";a.append(b);cout<//输出:abcdefghijcout<<5<a="abcde";a.append(5,'0');//第五种重载方法cout<//输出:abcde00000cout<<6<a="abcde";string::const_iteratori=a.begin(),i1=a.end();a.append(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:abcdeabcdecout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.append(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:abcde987654321return0;}1.2.2assign为字符串重新赋予新的内容。你可以将它看做先把字符串清空,然后再append。因此assign也有8种重载。例子:#include#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<<1<a.assign(x);//第一种重载方法cout<//输出:123456cout<<2<a="abcde";a.assign(x,3);//第二种重载分方法cout<//输出:123cout<<3<a="abcde";a.assign(b,0,2);//第三种重载方法cout<//输出:fgcout<<4<a="abcde";a.assign(b);cout<//输出:fghijcout<<5<a="abcde";a.assign(5,'0');//第五种重载方法cout<//输出:00000cout<<6<a="abcde";b="edcba";string::const_iteratori=b.begin(),i1=b.end();a.assign(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:edcbacout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.assign(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:987654321return0;}1.2.3at#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
abcde123
cout<<3<a="abcde";a.append(b,0,2);//第三种重载方法cout<//输出:abcdefgcout<<4<a="abcde";a.append(b);cout<//输出:abcdefghijcout<<5<a="abcde";a.append(5,'0');//第五种重载方法cout<//输出:abcde00000cout<<6<a="abcde";string::const_iteratori=a.begin(),i1=a.end();a.append(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:abcdeabcdecout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.append(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:abcde987654321return0;}1.2.2assign为字符串重新赋予新的内容。你可以将它看做先把字符串清空,然后再append。因此assign也有8种重载。例子:#include#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<<1<a.assign(x);//第一种重载方法cout<//输出:123456cout<<2<a="abcde";a.assign(x,3);//第二种重载分方法cout<//输出:123cout<<3<a="abcde";a.assign(b,0,2);//第三种重载方法cout<//输出:fgcout<<4<a="abcde";a.assign(b);cout<//输出:fghijcout<<5<a="abcde";a.assign(5,'0');//第五种重载方法cout<//输出:00000cout<<6<a="abcde";b="edcba";string::const_iteratori=b.begin(),i1=b.end();a.assign(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:edcbacout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.assign(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:987654321return0;}1.2.3at#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
a.append(b,0,2);//第三种重载方法
cout<//输出:abcdefgcout<<4<a="abcde";a.append(b);cout<//输出:abcdefghijcout<<5<a="abcde";a.append(5,'0');//第五种重载方法cout<//输出:abcde00000cout<<6<a="abcde";string::const_iteratori=a.begin(),i1=a.end();a.append(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:abcdeabcdecout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.append(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:abcde987654321return0;}1.2.2assign为字符串重新赋予新的内容。你可以将它看做先把字符串清空,然后再append。因此assign也有8种重载。例子:#include#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<<1<a.assign(x);//第一种重载方法cout<//输出:123456cout<<2<a="abcde";a.assign(x,3);//第二种重载分方法cout<//输出:123cout<<3<a="abcde";a.assign(b,0,2);//第三种重载方法cout<//输出:fgcout<<4<a="abcde";a.assign(b);cout<//输出:fghijcout<<5<a="abcde";a.assign(5,'0');//第五种重载方法cout<//输出:00000cout<<6<a="abcde";b="edcba";string::const_iteratori=b.begin(),i1=b.end();a.assign(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:edcbacout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.assign(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:987654321return0;}1.2.3at#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
abcdefg
cout<<4<a="abcde";a.append(b);cout<//输出:abcdefghijcout<<5<a="abcde";a.append(5,'0');//第五种重载方法cout<//输出:abcde00000cout<<6<a="abcde";string::const_iteratori=a.begin(),i1=a.end();a.append(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:abcdeabcdecout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.append(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:abcde987654321return0;}1.2.2assign为字符串重新赋予新的内容。你可以将它看做先把字符串清空,然后再append。因此assign也有8种重载。例子:#include#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<<1<a.assign(x);//第一种重载方法cout<//输出:123456cout<<2<a="abcde";a.assign(x,3);//第二种重载分方法cout<//输出:123cout<<3<a="abcde";a.assign(b,0,2);//第三种重载方法cout<//输出:fgcout<<4<a="abcde";a.assign(b);cout<//输出:fghijcout<<5<a="abcde";a.assign(5,'0');//第五种重载方法cout<//输出:00000cout<<6<a="abcde";b="edcba";string::const_iteratori=b.begin(),i1=b.end();a.assign(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:edcbacout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.assign(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:987654321return0;}1.2.3at#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
a.append(b);
cout<//输出:abcdefghijcout<<5<a="abcde";a.append(5,'0');//第五种重载方法cout<//输出:abcde00000cout<<6<a="abcde";string::const_iteratori=a.begin(),i1=a.end();a.append(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:abcdeabcdecout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.append(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:abcde987654321return0;}1.2.2assign为字符串重新赋予新的内容。你可以将它看做先把字符串清空,然后再append。因此assign也有8种重载。例子:#include#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<<1<a.assign(x);//第一种重载方法cout<//输出:123456cout<<2<a="abcde";a.assign(x,3);//第二种重载分方法cout<//输出:123cout<<3<a="abcde";a.assign(b,0,2);//第三种重载方法cout<//输出:fgcout<<4<a="abcde";a.assign(b);cout<//输出:fghijcout<<5<a="abcde";a.assign(5,'0');//第五种重载方法cout<//输出:00000cout<<6<a="abcde";b="edcba";string::const_iteratori=b.begin(),i1=b.end();a.assign(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:edcbacout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.assign(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:987654321return0;}1.2.3at#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
abcdefghij
cout<<5<a="abcde";a.append(5,'0');//第五种重载方法cout<//输出:abcde00000cout<<6<a="abcde";string::const_iteratori=a.begin(),i1=a.end();a.append(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:abcdeabcdecout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.append(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:abcde987654321return0;}1.2.2assign为字符串重新赋予新的内容。你可以将它看做先把字符串清空,然后再append。因此assign也有8种重载。例子:#include#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<<1<a.assign(x);//第一种重载方法cout<//输出:123456cout<<2<a="abcde";a.assign(x,3);//第二种重载分方法cout<//输出:123cout<<3<a="abcde";a.assign(b,0,2);//第三种重载方法cout<//输出:fgcout<<4<a="abcde";a.assign(b);cout<//输出:fghijcout<<5<a="abcde";a.assign(5,'0');//第五种重载方法cout<//输出:00000cout<<6<a="abcde";b="edcba";string::const_iteratori=b.begin(),i1=b.end();a.assign(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:edcbacout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.assign(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:987654321return0;}1.2.3at#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
a.append(5,'0');//第五种重载方法
cout<//输出:abcde00000cout<<6<a="abcde";string::const_iteratori=a.begin(),i1=a.end();a.append(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:abcdeabcdecout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.append(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:abcde987654321return0;}1.2.2assign为字符串重新赋予新的内容。你可以将它看做先把字符串清空,然后再append。因此assign也有8种重载。例子:#include#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<<1<a.assign(x);//第一种重载方法cout<//输出:123456cout<<2<a="abcde";a.assign(x,3);//第二种重载分方法cout<//输出:123cout<<3<a="abcde";a.assign(b,0,2);//第三种重载方法cout<//输出:fgcout<<4<a="abcde";a.assign(b);cout<//输出:fghijcout<<5<a="abcde";a.assign(5,'0');//第五种重载方法cout<//输出:00000cout<<6<a="abcde";b="edcba";string::const_iteratori=b.begin(),i1=b.end();a.assign(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:edcbacout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.assign(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:987654321return0;}1.2.3at#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
abcde00000
cout<<6<a="abcde";string::const_iteratori=a.begin(),i1=a.end();a.append(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:abcdeabcdecout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.append(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:abcde987654321return0;}1.2.2assign为字符串重新赋予新的内容。你可以将它看做先把字符串清空,然后再append。因此assign也有8种重载。例子:#include#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<<1<a.assign(x);//第一种重载方法cout<//输出:123456cout<<2<a="abcde";a.assign(x,3);//第二种重载分方法cout<//输出:123cout<<3<a="abcde";a.assign(b,0,2);//第三种重载方法cout<//输出:fgcout<<4<a="abcde";a.assign(b);cout<//输出:fghijcout<<5<a="abcde";a.assign(5,'0');//第五种重载方法cout<//输出:00000cout<<6<a="abcde";b="edcba";string::const_iteratori=b.begin(),i1=b.end();a.assign(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:edcbacout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.assign(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:987654321return0;}1.2.3at#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
string:
:
const_iteratori=a.begin(),i1=a.end();
a.append(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符
cout<//输出:abcdeabcdecout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.append(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:abcde987654321return0;}1.2.2assign为字符串重新赋予新的内容。你可以将它看做先把字符串清空,然后再append。因此assign也有8种重载。例子:#include#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<<1<a.assign(x);//第一种重载方法cout<//输出:123456cout<<2<a="abcde";a.assign(x,3);//第二种重载分方法cout<//输出:123cout<<3<a="abcde";a.assign(b,0,2);//第三种重载方法cout<//输出:fgcout<<4<a="abcde";a.assign(b);cout<//输出:fghijcout<<5<a="abcde";a.assign(5,'0');//第五种重载方法cout<//输出:00000cout<<6<a="abcde";b="edcba";string::const_iteratori=b.begin(),i1=b.end();a.assign(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:edcbacout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.assign(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:987654321return0;}1.2.3at#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
abcdeabcde
cout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.append(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:abcde987654321return0;}1.2.2assign为字符串重新赋予新的内容。你可以将它看做先把字符串清空,然后再append。因此assign也有8种重载。例子:#include#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<<1<a.assign(x);//第一种重载方法cout<//输出:123456cout<<2<a="abcde";a.assign(x,3);//第二种重载分方法cout<//输出:123cout<<3<a="abcde";a.assign(b,0,2);//第三种重载方法cout<//输出:fgcout<<4<a="abcde";a.assign(b);cout<//输出:fghijcout<<5<a="abcde";a.assign(5,'0');//第五种重载方法cout<//输出:00000cout<<6<a="abcde";b="edcba";string::const_iteratori=b.begin(),i1=b.end();a.assign(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:edcbacout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.assign(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:987654321return0;}1.2.3at#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
chartest0[]="987654321";
vectortest(test0,test0+9);
vector:
iteratori2=test.begin(),i3=test.end();
a.append(i2,i3);//第七种重载方法和第六种差不多!
//不过这个可以加除了string以外类型的迭代器的字符
cout<//输出:abcde987654321return0;}1.2.2assign为字符串重新赋予新的内容。你可以将它看做先把字符串清空,然后再append。因此assign也有8种重载。例子:#include#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<<1<a.assign(x);//第一种重载方法cout<//输出:123456cout<<2<a="abcde";a.assign(x,3);//第二种重载分方法cout<//输出:123cout<<3<a="abcde";a.assign(b,0,2);//第三种重载方法cout<//输出:fgcout<<4<a="abcde";a.assign(b);cout<//输出:fghijcout<<5<a="abcde";a.assign(5,'0');//第五种重载方法cout<//输出:00000cout<<6<a="abcde";b="edcba";string::const_iteratori=b.begin(),i1=b.end();a.assign(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:edcbacout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.assign(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:987654321return0;}1.2.3at#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
abcde987654321
return0;
}
1.2.2assign
为字符串重新赋予新的内容。
你可以将它看做先把字符串清空,然后再append。
因此assign也有8种重载。
例子:
cout<<1<a.assign(x);//第一种重载方法cout<//输出:123456cout<<2<a="abcde";a.assign(x,3);//第二种重载分方法cout<//输出:123cout<<3<a="abcde";a.assign(b,0,2);//第三种重载方法cout<//输出:fgcout<<4<a="abcde";a.assign(b);cout<//输出:fghijcout<<5<a="abcde";a.assign(5,'0');//第五种重载方法cout<//输出:00000cout<<6<a="abcde";b="edcba";string::const_iteratori=b.begin(),i1=b.end();a.assign(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:edcbacout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.assign(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:987654321return0;}1.2.3at#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
a.assign(x);//第一种重载方法
cout<//输出:123456cout<<2<a="abcde";a.assign(x,3);//第二种重载分方法cout<//输出:123cout<<3<a="abcde";a.assign(b,0,2);//第三种重载方法cout<//输出:fgcout<<4<a="abcde";a.assign(b);cout<//输出:fghijcout<<5<a="abcde";a.assign(5,'0');//第五种重载方法cout<//输出:00000cout<<6<a="abcde";b="edcba";string::const_iteratori=b.begin(),i1=b.end();a.assign(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:edcbacout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.assign(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:987654321return0;}1.2.3at#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
123456
cout<<2<a="abcde";a.assign(x,3);//第二种重载分方法cout<//输出:123cout<<3<a="abcde";a.assign(b,0,2);//第三种重载方法cout<//输出:fgcout<<4<a="abcde";a.assign(b);cout<//输出:fghijcout<<5<a="abcde";a.assign(5,'0');//第五种重载方法cout<//输出:00000cout<<6<a="abcde";b="edcba";string::const_iteratori=b.begin(),i1=b.end();a.assign(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:edcbacout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.assign(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:987654321return0;}1.2.3at#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
a.assign(x,3);//第二种重载分方法
cout<//输出:123cout<<3<a="abcde";a.assign(b,0,2);//第三种重载方法cout<//输出:fgcout<<4<a="abcde";a.assign(b);cout<//输出:fghijcout<<5<a="abcde";a.assign(5,'0');//第五种重载方法cout<//输出:00000cout<<6<a="abcde";b="edcba";string::const_iteratori=b.begin(),i1=b.end();a.assign(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:edcbacout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.assign(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:987654321return0;}1.2.3at#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
123
cout<<3<a="abcde";a.assign(b,0,2);//第三种重载方法cout<//输出:fgcout<<4<a="abcde";a.assign(b);cout<//输出:fghijcout<<5<a="abcde";a.assign(5,'0');//第五种重载方法cout<//输出:00000cout<<6<a="abcde";b="edcba";string::const_iteratori=b.begin(),i1=b.end();a.assign(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:edcbacout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.assign(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:987654321return0;}1.2.3at#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
a.assign(b,0,2);//第三种重载方法
cout<//输出:fgcout<<4<a="abcde";a.assign(b);cout<//输出:fghijcout<<5<a="abcde";a.assign(5,'0');//第五种重载方法cout<//输出:00000cout<<6<a="abcde";b="edcba";string::const_iteratori=b.begin(),i1=b.end();a.assign(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:edcbacout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.assign(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:987654321return0;}1.2.3at#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
fg
cout<<4<a="abcde";a.assign(b);cout<//输出:fghijcout<<5<a="abcde";a.assign(5,'0');//第五种重载方法cout<//输出:00000cout<<6<a="abcde";b="edcba";string::const_iteratori=b.begin(),i1=b.end();a.assign(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:edcbacout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.assign(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:987654321return0;}1.2.3at#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
a.assign(b);
cout<//输出:fghijcout<<5<a="abcde";a.assign(5,'0');//第五种重载方法cout<//输出:00000cout<<6<a="abcde";b="edcba";string::const_iteratori=b.begin(),i1=b.end();a.assign(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:edcbacout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.assign(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:987654321return0;}1.2.3at#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
fghij
cout<<5<a="abcde";a.assign(5,'0');//第五种重载方法cout<//输出:00000cout<<6<a="abcde";b="edcba";string::const_iteratori=b.begin(),i1=b.end();a.assign(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:edcbacout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.assign(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:987654321return0;}1.2.3at#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
a.assign(5,'0');//第五种重载方法
cout<//输出:00000cout<<6<a="abcde";b="edcba";string::const_iteratori=b.begin(),i1=b.end();a.assign(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:edcbacout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.assign(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:987654321return0;}1.2.3at#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
00000
cout<<6<a="abcde";b="edcba";string::const_iteratori=b.begin(),i1=b.end();a.assign(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符cout<//输出:edcbacout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.assign(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:987654321return0;}1.2.3at#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
b="edcba";
const_iteratori=b.begin(),i1=b.end();
a.assign(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符
cout<//输出:edcbacout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.assign(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:987654321return0;}1.2.3at#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
edcba
cout<<7<chartest0[]="987654321";vectortest(test0,test0+9);vector::iteratori2=test.begin(),i3=test.end();a="abcde";a.assign(i2,i3);//第七种重载方法和第六种差不多!//不过这个可以加除了string以外类型的迭代器的字符cout<//输出:987654321return0;}1.2.3at#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
a.assign(i2,i3);//第七种重载方法和第六种差不多!
cout<//输出:987654321return0;}1.2.3at#include#includeusingnamespacestd;intmain(){stringa,b;a="abcde";b="fghij";charx[]="123456";cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
987654321
1.2.3at
cout<return0;}1.2.4begin用迭代器时使用!1.2.5clear删除所有字符。1.2.6compare以字典顺序进行比较。>为正,=为零,<为负。#include#includeusingnamespacestd;intmain(){stringa,b;a="123";b="023";cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
1.2.4begin
用迭代器时使用!
1.2.5clear
删除所有字符。
1.2.6compare
以字典顺序进行比较。
>为正,=为零,<为负。
a="123";
b="023";
cout<//输出:1return0;}1.2.7copy拷贝,为内部调用。#include#includeusingnamespacestd;intmain(){intlength;charbuffer[20];stringstr("Teststring...");length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给bufferbuffer[length]='\0';cout<<"buffercontains:"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
1
1.2.7copy
拷贝,为内部调用。
intlength;
charbuffer[20];
stringstr("Teststring...");
length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给buffer
buffer[length]='\0';
cout<<"buffercontains:
"<//输出:buffercontains:stringreturn0;}1.2.8empty返回字符串是否为空。1.2.9end返回指向末尾的iterator或者const_iterator。不能对其返回值提领!1.2.10erase删除字符串。#include#includeusingnamespacestd;intmain(){stringstr("Thisisanexamplephrase.");string::iteratorit;//eraseusedinthesameorderasdescribedabove:str.erase(10,8);//删除str[10]后的8个字符cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
buffercontains:
string
1.2.8empty
返回字符串是否为空。
1.2.9end
返回指向末尾的iterator或者const_iterator。
不能对其返回值提领!
1.2.10erase
删除字符串。
stringstr("Thisisanexamplephrase.");
iteratorit;
//eraseusedinthesameorderasdescribedabove:
str.erase(10,8);//删除str[10]后的8个字符
cout<it=str.begin()+9;str.erase(it);//深处迭代器位置的字符cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
it=str.begin()+9;
str.erase(it);//深处迭代器位置的字符
cout<str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符
cout<return0;} 1.2.11find1.2.12find_first_not_of查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
1.2.11find
1.2.12find_first_not_of
查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。
stringstr("lookfornon-alphabeticcharacters...");
size_tfound;
found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");
//查找串中没有的元素!
返回第一个找到的元素的下标!
if(found!
=string:
npos)
cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris-atposition12;return0;}1.2.13find_first_of查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回第一个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
Firstnon-alphabeticcharacteris-atposition12;
1.2.13find_first_of
查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!
found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");
//查找串中有的元素!
cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterislatposition0;return0;}1.2.14find_last_not_of查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");//查找串中没有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
Firstnon-alphabeticcharacterislatposition0;
1.2.14find_last_not_of
查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!
found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");
返回最后一个个找到的元素的下标!
cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
cout<<"atposition"<}//输出:Firstnon-alphabeticcharacteris.atposition36;return0;}1.2.15find_last_of查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!例子:#include#includeusingnamespacestd;intmain(){stringstr("lookfornon-alphabeticcharacters...");size_tfound;found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");//查找串中有的元素!返回最后一个个找到的元素的下标!if(found!=string::npos){cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
Firstnon-alphabeticcharacteris.atposition36;
1.2.15find_last_of
查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!
found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");
cout<<"Firstnon-alphabeticcharacteris"<cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
cout<<"atposition"<}//输出:Firstnon-alphabeticcharacterissatposition33;return0;}1.2.16insert插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。详细的例子可以参考MSDN,其它没有什么值得说的。1.2.17length返回字符串长度。1.2.18max_size返回一个string所能容纳的最大字符个数。通常我们都不需要用到这个函数。1.2.19rbegin返回反向迭代器,指向反向的第一个元素。同vector,不解释1.2.20rend返回反向迭代器,指向最后一个元素。绝对不能对它进行提领操作。同vector,不解释1.2.21replace字符串替换。#include#includeusingnamespacestd;intmain(){stringbase="thisisateststring.";stringstr2="nexample";stringstr3="samplephrase";stringstr4="useful.";//functionversionsusedinthesameorderasdescribedabove://Usingpositions:0123456789*123456789*12345stringstr=base;//"thisisateststring."str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
Firstnon-alphabeticcharacterissatposition33;
1.2.16insert
插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。
这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。
详细的例子可以参考MSDN,其它没有什么值得说的。
1.2.17length
返回字符串长度。
1.2.18max_size
返回一个string所能容纳的最大字符个数。
通常我们都不需要用到这个函数。
1.2.19rbegin
返回反向迭代器,指向反向的第一个元素。
同vector,不解释
1.2.20rend
返回反向迭代器,指向最后一个元素。
绝对不能对它进行提领操作。
1.2.21replace
字符串替换。
stringbase="thisisateststring.";
stringstr2="nexample";
stringstr3="samplephrase";
stringstr4="useful.";
//functionversionsusedinthesameorderasdescribedabove:
//Usingpositions:
0123456789*123456789*12345
stringstr=base;//"thisisateststring."
str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替
cout<str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替
cout<str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替
cout<str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替
cout<str.replace(22,1,3,'!');//str[22]后面的1个字符用3个"!"代替cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
str.replace(22,1,3,'!
');//str[22]后面的1个字符用3个"!
"代替
cout<//Usingiterators:0123456789*123456789*string::iteratorit=str.begin();//^str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
//Usingiterators:
0123456789*123456789*
iteratorit=str.begin();//^
str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替
cout<str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替
cout<it+=8;//^str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
it+=8;//^
str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替
cout<str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替
cout<it+=3;//^str.replace(it,str.end(),str4.begin(),str4.end());//str里的it到str.end()位置的字符用str4里的//str4.begin()到str4.end()的字符替换cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
it+=3;//^
str.replace(it,str.end(),str4.begin(),str4.end());
//str里的it到str.end()位置的字符用str4里的
//str4.begin()到str4.end()的字符替换
cout<cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
cout<return0;}输出:.1.2.22rfind跟find类似,不过是反过来搜索。特别注意,返回值是index,没有rindex的概念。输出最后一个找到的字符串的第一个字符的下标例子:1.2.23size返回长度。跟length()一样。1.2.24substr例子:1.2.25swap同vector,不解释1.3string与algorithm相结合的使用string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。1.5.1string与remove#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
输出:
.
1.2.22rfind
跟find类似,不过是反过来搜索。
特别注意,返回值是index,没有rindex的概念。
输出最后一个找到的字符串的第一个字符的下标
1.2.23size
返回长度。
跟length()一样。
1.2.24substr
1.2.25swap
1.3string与algorithm相结合的使用
string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。
1.5.1string与remove
stringss("HelloWorld!
_HelloWorld!
");
cout<ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());
cout<ss="HelloWorld!_HelloWorld!";ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
ss="HelloWorld!
";
ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());
cout<return0;}1.5.2string与unique、sort#include#include#includeusingnamespacestd;intmain(){stringss("HelloWorld!_HelloWorld!");cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
1.5.2string与unique、sort
cout<sort(ss.begin(),ss.end());cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
sort(ss.begin(),ss.end());
cout<ss.erase(unique(ss.begin(),ss.end()),ss.end());cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
ss.erase(unique(ss.begin(),ss.end()),ss.end());
cout<return0;}1.5.3string与search#include#include#include#includeusingnamespacestd;structiCmp:publicbinary_function{booloperator()(charleft,charright){returntoupper(left)==toupper(right);}};intmain(){stringstr(
1.5.3string与search
structiCmp:
publicbinary_function
booloperator()(charleft,charright)
returntoupper(left)==toupper(right);
};
stringstr(
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2