c++——string类基础详解
c++——string类基础详解
string是c++风格的字符串,本质上是一个类
一、构造函数
string s1;// 无参构造,创建一个空的字符串
string s2("Hello World");//有参构造,给字符串进行初始化
string s3(s2);//拷贝构造
string s4(8,'A');//使用n个(8个)字符(A)进行初始化
运行结果:

注意:

方法三:拷贝构造仅限于未被构造的string,不能作为复制作用,如上图,已经通过无参构造了 s1 ,再进行次操作就会报错。
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-D79ZPyVa-1609774179355)(C:\Users\happy\AppData\Roaming\Typora\typora-user-images\image-20210104160759375.png)]](https://img-blog.csdnimg.cn/20210104233128791.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUxMzQ0MTcy,size_16,color_FFFFFF,t_70)
方法四:其中的字符,只能为字符字面量,单个的char类型字符,如果为 “liu” 这种形式,则会报错。如上图的操作,则会只输出由字符串最后一个字符组合成的结果。
二、赋值
1、=(一般使用得较多)
string s1,s2,s3;
s1 = "Hello World";
s2 = s1;
s3 = "a";//或者 s3 = 'a';
//当为双引号时,类似于s1;若为单引号,则为char型的一个字符字面量
运行结果:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yRFr1YIo-1609774179357)(C:\Users\happy\AppData\Roaming\Typora\typora-user-images\image-20210104155716645.png)]](https://img-blog.csdnimg.cn/20210104233207305.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUxMzQ0MTcy,size_16,color_FFFFFF,t_70)
2、assign()函数
string s1,s2,s3,s4;
s1.assign("Hello C++");
s2.assign("Hello C++",5);//把字符串(Hello C++)中的前n个(5个),即Hello对s2进行赋值
s3.assign(s2);//拷贝复制进行赋值;
s4.assign(8,'w');//使用n个(8个)字符(w)进行
运行结果:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vTPWZ5Gr-1609774179360)(C:\Users\happy\AppData\Roaming\Typora\typora-user-images\image-20210104163543107.png)]](https://img-blog.csdnimg.cn/20210104233237123.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUxMzQ0MTcy,size_16,color_FFFFFF,t_70)
三、拼接
1、+=
string s1;
s1 = "我";
s1 += "爱你";//char数组
cout<<s1<<endl;
s1 += '!';//char字符字面量
cout<<s1<<endl;
s1 += "fover no give";//string
cout<<s1<<endl;
运行结果:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Z6a5Fm9E-1609774179362)(C:\Users\happy\AppData\Roaming\Typora\typora-user-images\image-20210104164911883.png)]](https://img-blog.csdnimg.cn/20210104233302172.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUxMzQ0MTcy,size_16,color_FFFFFF,t_70)
2、append()函数
string s1,s2;
s1 = "I";
s1.append("love");//char数组
cout<<s1<<endl;
s1.append("you are beautiful",3);//char数组中前n个(3个)字符连接到字符串s1的末尾
cout<<s1<<endl;
s1.append("fover no give");//string
cout<<s1<<endl;
s2.append(s1,8,5);///将字符串中从第pos位置(8,从0开始数),开始的n个(5个)字符连接到字符串s2的末尾
cout<<s2<<endl;
运行结果:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XnvooMcZ-1609774179365)(C:\Users\happy\AppData\Roaming\Typora\typora-user-images\image-20210104170511239.png)]](https://img-blog.csdnimg.cn/20210104233327375.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUxMzQ0MTcy,size_16,color_FFFFFF,t_70)
四 、查找——find()函数/rfind()函数
string s = "Hello Worlod! I love you!";
int a1 = s.find("lo");//存在该字符串,则返回该字符串首字母的位置(类似与数组,从下标0开始。)
int a2 = s.rfind("lo");
int b = s.find("wd");//不存在该字符串,则返回 -1
cout<<a1<<endl;
cout<<a2<<endl;
cout<<b<<endl;
运行结果:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KZTtgaFf-1609774179367)(C:\Users\happy\AppData\Roaming\Typora\typora-user-images\image-20210104221436600.png)]](https://img-blog.csdnimg.cn/20210104233352261.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUxMzQ0MTcy,size_16,color_FFFFFF,t_70)
注:比较find()函数和rfind()函数
find()函数,从左往右开始查找,查找第一次出现字符串的首字母位置;而rfind()函数,则从右往左开始查找,查找第一次出现字符串的尾字母的位置。
五、替换——replace()函数
string s = "Hello Worlod! I love you!";
s.replace(1,3,"6666");//从1号位置起,3个元素被替换成后面的字符串
cout<<s<<endl;
运行结果:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3J85SC3u-1609774179368)(C:\Users\happy\AppData\Roaming\Typora\typora-user-images\image-20210104224210778.png)]](https://img-blog.csdnimg.cn/20210104233423456.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUxMzQ0MTcy,size_16,color_FFFFFF,t_70)
六、比较——compare()函数
比较方式:按照字符的ASCII码依次进行比较
等于 --> 0
大于 --> 1
小于 --> -1
string s1 = "hexlo";
string s2 = "hello";
if(s1.compare(s2)==0)
cout<<"s1 等于 s2";
else if (s1.compare(s2)>0)//即:1
cout<<"s1 大于 s2";// x 的ASCII码大于 l 的ASCII码
else //即: -1
cout<<"s1 小于 s2";
运行结果:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GxOL9T6R-1609774179370)(C:\Users\happy\AppData\Roaming\Typora\typora-user-images\image-20210104225125822.png)]](https://img-blog.csdnimg.cn/20210104233501759.png)
实用性:主要是比较是否相等,判断谁大谁小意义不大
七、字符存(读)取(写)
1、通过 [] 访问单个字符(类似于char 数组)
string s1 = "hello";
for(int i=0;i<s1.size();i++)
{
cout<<s1[i]<<" ";
}
cout<<endl;
2、通过 at 方式访问单个字符
string s1 = "hello";
for(int i=0;i<s1.size();i++)
{
cout<<s1.at(i)<<" ";
}
cout<<endl;
1、2运行结果:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PGAGUq4x-1609774179372)(C:\Users\happy\AppData\Roaming\Typora\typora-user-images\image-20210104230423625.png)]](https://img-blog.csdnimg.cn/20210104233530949.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUxMzQ0MTcy,size_16,color_FFFFFF,t_70)
注:写,即修改,两种方式类似上,不赘述
八、插入——insert()函数(略)
string s1 = "hello";
s1.insert(1,"666");//在1号位置,插入字符串
cout<<s1<<endl;
运行结果:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-g4rby3p1-1609774179373)(C:\Users\happy\AppData\Roaming\Typora\typora-user-images\image-20210104231100120.png)]](https://img-blog.csdnimg.cn/20210104233557633.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUxMzQ0MTcy,size_16,color_FFFFFF,t_70)
注:写过这个函数使用的博客
我的博客(含leecode例题):https://blog.csdn.net/m0_51344172/article/details/112174996
大佬的该函数详解博客:https://blog.csdn.net/weixin_39583140/article/details/88798799
九、删除——erase()函数
string s1 = "h666ello";
s1.erase(1,3);//在1号位置起,删除3个字符
cout<<s1<<endl;
运行结果:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2MC0z5w6-1609774179375)(C:\Users\happy\AppData\Roaming\Typora\typora-user-images\image-20210104231626537.png)]](https://img-blog.csdnimg.cn/2021010423363071.png)
注:函数中的位置,均类似char数组,从下标0开始
十、子串获取——substr()函数
string s1 = "Hello World ! I Love You !";
string s2 = s1.substr(0,5);//在1号位置起,取5个字符
cout<<s2<<endl;
运行结果:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RW8KXbsw-1609774179376)(C:\Users\happy\AppData\Roaming\Typora\typora-user-images\image-20210104232147113.png)]](https://img-blog.csdnimg.cn/20210104233655568.png)
实用性:搭配 find()函数 /或者其他函数一起使用
例:
//截取用户名
string email = "baby@163.com";
int pos = email.find("@");
string useName = email.substr(0,pos);
cout<<useName<<endl;
运行结果:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PxSZoXYa-1609774179377)(C:\Users\happy\AppData\Roaming\Typora\typora-user-images\image-20210104232653451.png)]](https://img-blog.csdnimg.cn/20210104233714856.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUxMzQ0MTcy,size_16,color_FFFFFF,t_70)
十一、string转换成char数组——c_str()函数
char c[20];
string s="1234";
strcpy(c,s.c_str());
注:转换后不能继续使用string类的函数:

十二、字符串翻转——reverse()函数
string s="123456";
reverse(s.begin(),s.end());
cout<<s<<endl;
运行结果:

注:reverse()函数,只要是能用swap交换的连续存储结构都可以用。例:
int a[] = {1,2,3,4,5};
reverse(a,a+5);
for(int i=0;i<5;i++)
cout<<a[i]<<' ';
运行结果:

十三、memset()函数——对一连串内存按字节填充
封装在 < cstring >库中


浙公网安备 33010602011771号