当前位置: 首页 > news >正文

第九层(2):STL之string类

文章目录

  • 前情回顾
  • 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<string>
#include<iostream>
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<string>
#include<iostream>
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<string>
#include<iostream>
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<string>
#include<iostream>
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<string>
#include<iostream>
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<string>
#include<iostream>
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<string>
#include<iostream>
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<string>
#include<iostream>
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<string>
#include<iostream>
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++爬塔日记
🙉都看到这里了,留下你们的👍点赞+⭐收藏+📋评论吧🙉

相关文章:

  • Allegro如何自动做差分对内等长操作指导
  • 搜索引擎位置跟踪应用SerpBear
  • 浅析一条SQL在mysql中是如何执行的
  • 前端艺术之毛玻璃-倾斜-日历
  • Python-Flask-2023.1.24-Review
  • SpringBoot 统一功能处理
  • 3. Python列表简介
  • sidebar(侧边栏原理vue admin)
  • BERT模型结构可视化与模块维度转换剖析
  • 谈谈线程安全问题及其解决方法
  • 好客租房-12.ES接入java
  • java入门笔记
  • 进阶C语言 第二章-------《进阶指针》 (指针数组、数组指针、函数指针、回调指针)知识点+基本练习题+深入细节+通俗易懂+完整思维导图+建议收藏
  • 【图卷积神经网络】02-谱域图卷积介绍
  • 【JavaWeb】JavaScript基础语法(下)
  • django开发-定时任务的使用
  • iOS | NSProxy
  • iOS筛选菜单、分段选择器、导航栏、悬浮窗、转场动画、启动视频等源码
  • IP路由与转发
  • leetcode讲解--894. All Possible Full Binary Trees
  • miaov-React 最佳入门
  • PHP 7 修改了什么呢 -- 2
  • underscore源码剖析之整体架构
  • Vue--数据传输
  • 关于Flux,Vuex,Redux的思考
  • 关于springcloud Gateway中的限流
  • 技术攻略】php设计模式(一):简介及创建型模式
  • 聊聊redis的数据结构的应用
  • 每个JavaScript开发人员应阅读的书【1】 - JavaScript: The Good Parts
  • 深度学习在携程攻略社区的应用
  • 微信支付JSAPI,实测!终极方案
  • 消息队列系列二(IOT中消息队列的应用)
  • 用jquery写贪吃蛇
  • AI又要和人类“对打”,Deepmind宣布《星战Ⅱ》即将开始 ...
  • LevelDB 入门 —— 全面了解 LevelDB 的功能特性
  • PostgreSQL之连接数修改
  • UI设计初学者应该如何入门?
  • ​ 全球云科技基础设施:亚马逊云科技的海外服务器网络如何演进
  • ​iOS安全加固方法及实现
  • ​批处理文件中的errorlevel用法
  • ​直流电和交流电有什么区别为什么这个时候又要变成直流电呢?交流转换到直流(整流器)直流变交流(逆变器)​
  • #pragma pack(1)
  • #Z0458. 树的中心2
  • (06)金属布线——为半导体注入生命的连接
  • (附源码)springboot车辆管理系统 毕业设计 031034
  • (完整代码)R语言中利用SVM-RFE机器学习算法筛选关键因子
  • (转)树状数组
  • .htaccess配置常用技巧
  • .h头文件 .lib动态链接库文件 .dll 动态链接库
  • .NET 4.0中的泛型协变和反变
  • .NET CF命令行调试器MDbg入门(三) 进程控制
  • .net 后台导出excel ,word
  • .net实现客户区延伸至至非客户区
  • .NET运行机制
  • /etc/sudoers (root权限管理)