<C++>详解string容器,揭开string容器的神秘面纱_图解string容器

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!


**tips:stirng赋值方法很多,但是重载的`operator=`的方式最为常用**


### string拼接操作


* 在字符串末尾拼接字符串


**函数原型:**


* string& operator+=(const char\* str)重载+=操作符
* string& operator+=(const char c)重载+=操作符
* string& operator+=(const string& str)重载+=操作符
* string& append(const char\* s)把字符串s连接到当前字符串结尾
* string& append(const char\* s,int n)把字符串s的前n个字符连接到当前字符串的结尾
* string& append(const string &s)同operator+=(const string& str)
* string& append(const string &s,int pos,int n)把字符串s中从pos开始的n个字符连接到字符串结尾


使用示例:



void test3()
{
string str1 = “红豆”;
str1 += “忆相思”;
cout << “str1=” << str1<< endl;

str1 += '?';
cout << "str1=" << str1 << endl;

string str2 = "yyds";
str1 += str2;
cout << "str1=" << str1 << endl;

string str3 = "You";
str3.append("low");
cout << "str3=" << str3 << endl;

str3.append("wuwuwu qaq", 4);
cout << "str3=" << str3 << endl;

str3.append(str2);
cout << "str3=" << str3 << endl;

str3.append(str2, 0, 1);
cout << "str3=" << str3 << endl;

}


**tips:初学者只需要稍微记几个拼接函数即可**


### string查找替换


* 指定位置查找字符串
* 指定位置删除字符串


**函数原型:**


* 查找s第一次出现位置,从pos开始查找  
 int find(const string& str, int pos = 0) const;  
 int find(const char\* s , int pos ==0) const;
* 从pos位置查找s的前n个字符第一次位置  
 int find( const char\* s, int pos, int n) const;
* 查找字符c第一次出现位置  
 int find(const char c, int pos = e) const;
* 查找str最后一次位置,从pos开始查找  
 int rfind(const string& str, int pos = npos) const;
* 查找str最后一次位置,从pos开始查找,计数永远是从前往后  
 int rfind(const char\* s, int pos = npos) const;
* 从pos查找s的前n个字符最后一次位置  
 int rfind(const char\* s, int pos, int n) const;
* 查找字符c最后一次出现位置  
 int rfind(const char c, int pos - e) const;
* 替换从pos开始n个字符为字符串str  
 string& replace(int pos, int n, const string& str);
* 替换从pos开始的n个字符为字符串s  
 string& replace(int pos, int n,const char\* s );


使用示例:



//字符串的查找和替换
//查找
void test4()
{
string str1 = “abcdefgh”;
//找到返回下标,找不到返回-1
int pos1 = str1.find(“def”);
cout << “pos1=” << pos1 << endl;
int pos2 = str1.find(“s”);
cout << “pos2=” << pos2<< endl;

pos1 = str1.rfind("ab");//从右往左找到第一个出现,从左往右计数
cout << "pos1=" << pos1 << endl;;

}
//替换
void test5()
{
string str2 = “abcdef”;
str2.replace(1, 2, “1111”);//从1号位置起,2个字符替换为1111
cout << “str2=” << str2 << endl;
}


**tips:  
 find找到字符串后返回查找的第一个字符位置,找不到返回1  
 函数虽然很多,但几乎都是两个版本的,一个是c++风格一个c语言风格**


### string字符串比较


* 字符串比较是按字符的`ASCII码`进行对比


**函数原型:**


* int compare(const string &s) const;
* int compare(const char\* s) const;  
 使用示例:



> 
> string str1 = “zello”;  
>  string str2 = “hello”;  
>  if (str1.compare(str2) == 0)  
>  {  
>  cout << “相等” << endl;  
>  }  
>  else if (str1.compare(str2) > 0)  
>  {  
>  cout << “str1大” << endl;  
>  }  
>  else  
>  {  
>  cout << “str2大” << endl;  
>  }
> 
> 
> 


**tips:字符串对比的目的是比较两个字符串是否`相等`,判断谁大谁小的意义并不是很大。**


### string字符读取


* 单个字符存取有两种方式:


**函数原型:**


* char& operator[] (int n); //通过[]获取字符
* char& at (int n); //通过at方法获取字符


使用示例:



> 
> string str1 = “hello”;  
>  //通过[]访问单个字符  
>  for (int i = 0; i < str1.size(); i++)  
>  {  
>  cout << str1[i] << " ";  
>  }  
>  cout << endl;  
>  //通过at方式访问的单个字符  
>  for (int i = 0; i < str1.size(); i++)  
>  {  
>  cout << str1.at(i) << " ";  
>  }  
>  cout << endl;  
>  //修改单个字符  
>  str1[0] = ‘z’;  
>  cout << str1 << endl;  
>  str1.at(0) = ‘x’;  
>  cout << str1 << endl;
> 
> 
> 


### string插入和删除


**函数原型:**


* string& insert(int pos,const cahr\* s);//在n位置插入字符串
* string& insert(int pos,const string& s);//在n位置插入字符串
* string& insert(int pos,int n,char c);//在指定位置插入n个字符c
* string& erase(int pos,int n = npos);//删除从pos位置开始的n个字符


使用示例:



> 
> string str = “hello”;  
>  //插入  
>  str.insert(1, “111”);  
>  cout << "str = " << str << endl;  
>  //删除  
>  str.erase(1,3);  
>  cout << "str = " << str << endl;
> 
> 
> 


**tips:插入和删除的起始下标都是从`0`开始。**


### string求子串


* 从字符串中得到想要的子串


**函数原型:**


* string substr(int pos=0,int n=npos) const ;//返回由pos位置开始的由n个字符组成的字符串



//string求子串
void test01()
{
string str = “abcdef”;
string subStr = str.substr(1, 3);
cout << “subStr=” << subStr << endl;
}
//使用操作
void test02()
{
string email = “ylqb@qq.com”;
//从邮箱地址中获取用户名信息
int pos = email.find(“@”);
string usrName = email.substr(0, pos);
cout << usrName << endl;
}


**tips:灵活的运用求子串功能,可以在实际开发中获取有效的信息,在上述代码中就可以有效获取到不同邮箱中的用户名。**


## 📃结语



> 
> 


![img](https://img-blog.csdnimg.cn/img_convert/6b97b37604f58178c96269fa0c8954d0.png)
![img](https://img-blog.csdnimg.cn/img_convert/fbb765c2eadcdbac1756d280d16c7003.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以添加戳这里获取](https://bbs.csdn.net/topics/618668825)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

1715893175979)]
[外链图片转存中...(img-4e4j6D79-1715893175979)]

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以添加戳这里获取](https://bbs.csdn.net/topics/618668825)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值