第九层(2):STL之string类
创始人
2024-05-16 15:09:43
0

文章目录

  • 前情回顾
  • string类
    • string类的本质
    • string与char*的区别
    • string类的特点
    • string类的构造函数
    • string类内的字符串追加函数
    • string类内的字符串查找函数
    • string类内的字符串替换函数
    • string类内的字符串比较函数
    • string类内的字符单个访问函数
    • string类内的插入函数
    • string类内的删除函数
    • string类内的获取字串函数
  • 还是石碑?

🎉welcome🎉
✒️博主介绍:一名大一的智能制造专业学生,在学习C/C++的路上会越走越远,后面不定期更新有关C/C++语法,数据结构,算法,Linux,ue5使用,制作游戏的心得,和大家一起共同成长。
✈️C++专栏:C++爬塔日记
😘博客制作不易,👍点赞+⭐收藏+➕关注

前情回顾

在上一章中,我上到了最后一层,了解到了什么是STL,并且学到一些基本的使用,当我以为就此结束的时候,发现第一个石碑倒下之后,后面还有一座石碑…

  • 🚄上章地址:第九层(1):初识STL

string类

我看着面前的石碑,陷入沉思:难道第九层和下面的构造是不一样的吗?第九层后面还有很多石碑吗?只要这个石碑倒下就能验证猜想…

string类的本质

  • string是C++风格的字符串,而它本质上是一个类

string与char*的区别

  • char*是一个指针,可以指向一个字符串的起始地址
  • string是一个类,但类内本质上是封装了一个char*,用于管理字符串,是char*的一个容器

string类的特点

  • string类内部封装了很多成员函数,如:查找find、拷贝copy、删除delete、替换replace、插入insert
  • string管理char*所分配的内存,所以不用担心复制越界和取值越界的问题,由类内直接复制

string类的构造函数

  • string类中的构造函数有四个,有一个创建和三个初始化:
string();//创建一个空的字符串
string(const char* s);//使用字符串s对string对象进行初始化
string(const string& s);//使用一个string对象初始化另一个string对象
string(int n,char c);//使用n个字符c初始化string对象

使用:

#include
#include
using namespace std;void test1()
{string s;const char* c = "CPP";string s1(c);cout << s1 << endl;string s2(s1);cout << s2 << endl;string s3(5, 'C');cout << s3 << endl;
}int main()
{test1();return 0;
}

在这里插入图片描述
可以根据喜好选择初始化的方法。

string类内的字符串追加函数

  • 对于字符串追加函数,在C++中同样存在,只不过封装在了在string类内部,并且有更多选择:
string& operator+=(const char* str);//操作符重载,可以将常量字符串str追加到string对象后面
string& operator+=(const char s);//操作符重载,将常量字符s追加到string对象后面
string& operator+=(const string& str);//操作符重载,将string对象str追加到另一个string对象后面
string& append(const char* str);//可以将常量字符串str追加到string对象后面
string& append(const char* str, int n);//可以将常量字符串str的前n个字符追加到string对象后面
string& append(const string& str);//将string对象str追加到另一个string对象后面
string& append(const string& str,int pos,int n)://将string对象str从pos下标开始的n个字符追加到另一个string对象后面

使用:

#include
#include
using namespace std;void test1()
{const char* str = "CPP";string s1; s1 += str;cout << s1 << endl;const char s = '=';s1 += s;cout << s1 << endl;string s2;s2 += s1;cout << s2 << endl;string s3("I love");s3.append(str);cout << s3 << endl;string s4;s4.append(str, 2);cout << s4 << endl;string s5;s5.append(s3);cout << s5 << endl;s4.append(s3, 2, 4);cout << s4 << endl;
}
int main()
{test1();return 0;
}

在这里插入图片描述

string类内的字符串查找函数

  • 在C语言中,有字符串查找的库函数,同样在C++中,封装到了string类当中:
int find(const string& str ,int pos = 0)const;//查找string对象str第一次出现的位置,从下标为pos的开始查找
int find(const char* str, int pos = 0)const;//查找常量字符串str第一次出现的位置,从下标为pos的开始查找
int find(const char* str, int pos,int n)const;//查找常量字符串str是否存在,从下标为pos的查找前n个字符
int find(const char s,int pos = 0)const;//查找常量字符s第一次出现的位置
int rfind(const string& str ,int pos = npos)const;//查找string对象str最后一次出现的位置,从下标为pos的开始查找
int find(const char* str, int pos = npos)const;//查找常量字符串str最后一次出现的位置,从下标为pos的开始查找
int find(const char* str, int pos,int n)const;//查找常量字符串str是否存在,从下标为pos的查找前n个字符
int find(const char s,int pos = npos)const;//查找常量字符s最后一次出现的位置
  • 在C++中的查找和C的中的查找是一样的,找到返回下标,找不到返回-1,那为什么在C++中要出现find和rfind呢?它们的区别是什么?
  • find是从左往右查
  • rfind是从右往左查

使用:

#include
#include
using namespace std;void test1()
{string s("asdffdsadffdsa");string s1("ff");int f1 = s.find(s1);cout << f1 << endl;int f2 = s.find("ff");cout << f2 << endl;int f3 = s.find("ff", 5,6);cout << f3 << endl;int f4 = s.find('d');cout << f4 << endl;int f5 = s.rfind(s1);cout << f5 << endl;int f6 = s.rfind("ff");cout << f6 << endl;int f7 = s.rfind("ff", 0, 5);cout << f7 << endl;int f8 = s.rfind('a');cout << f8 << endl;
}
int main()
{test1();return 0;
}

在这里插入图片描述

string类内的字符串替换函数

  • 在string类中,可以完成字符串的替换:
string& replace(int pos ,int n ,const string& str);//把从下标pos开始的n个字符串替换成string对象str
string&replace(int pos ,int n ,const char* str);//把从下标pos开始的n个字符串替换成字符串str
  • 当替换的字符串中字符个数大于被替换的字符个数时,会怎么样?

使用:

#include
#include
using namespace std;void test1()
{string s("h1 world");s.replace(1, 1, "ello");cout << s << endl;
}
int main()
{test1();return 0;
}

在这里插入图片描述
是会将要替换进行的字符串全都替换进去,但是不会改变我们不让改变的地方。

string类内的字符串比较函数

  • 在C++中,也将字符串比较封装在了string类内,与C一样,是比较下标一样的ASCII码值,等于返回0,小于返回-1,大于返回1
int compare(const string& str)const;//与string对象str相比较
int compare(const char* str)const;//与常量字符串str相比较

使用:

#include
#include
using namespace std;void test1()
{string s("asdfg");int c1 = s.compare("asdfg");cout << c1 << endl;int c2 = s.compare("asdfr");cout << c2 << endl;int c3 = s.compare("asdfa");cout << c3 << endl;
}
int main()
{test1();return 0;
}

在这里插入图片描述

string类内的字符单个访问函数

  • 在C++中,字符串中的字符也可以进行单个访问,可以修改:
char& operator[](int n);//操作符重载,可以访问到下标为n的字符
char& at(int n);//通过at访问下标为n的字符

使用:

#include
#include
using namespace std;void test1()
{string s("hello world");for (int i = 0; i < s.size()/*size可以获取字符串场长度*/; i++){cout << s[i] << " ";}cout << endl;for (int i = 0; i < s.size()/*size可以获取字符串场长度*/; i++){cout << s.at(i) << " ";}cout << endl;
}
int main()
{test1();return 0;
}

在这里插入图片描述

string类内的插入函数

  • 与string类内的替换函数是基本一样的,但是不一样的是,它不会让原本的字符消失
string& insert(int pos,const char* str);//从下标pos的位置开始插入常量字符串str
string& insert(int pos,const string& str);//从下标pos的位置开始插入string对象str
string& insert(int pos,int n,const char s);//从下标pos的位置开始插入n个字符s常量字符串str
#include
#include
using namespace std;void test1()
{string s("h world");s.insert(1, "ello");cout << s << endl;string s1("ab");s1.insert(1, 3, 'c');cout << s1 << endl;
}
int main()
{test1();return 0;
}

在这里插入图片描述

string类内的删除函数

  • 它可以指定删除一个区间内的内容
string& erase(int pos,int n=npos//表示string结束的最后位置);//从下标为pos的字符开始删除n个字符

使用:

#include
#include
using namespace std;void test1()
{string s("hello 11world");s.erase(6,2);cout << s << endl;
}
int main()
{test1();return 0;
}

在这里插入图片描述

string类内的获取字串函数

  • 在string类内可以获取区间内的字符
string& substr(int pos=0,int n=npos)const;//返回从下标pos开始的n个字符

使用:

#include
#include
using namespace std;void test1()
{string s("hello world");string s1;int n = s.find(" ");//通过查找找到下标s1 = s.substr(0, n);cout << s1 << endl;
}
int main()
{test1();return 0;
}

在这里插入图片描述

还是石碑?

  • 随着对string有了解之后,第二座石碑也倒下了,映入我眼帘的还是一座石碑…

😘预知后事如何,关注新专栏,和我一起征服C++这座巨塔
🚀专栏:C++爬塔日记
🙉都看到这里了,留下你们的👍点赞+⭐收藏+📋评论吧🙉

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...