C++中string容器的修改操作
目录
1.push_back() 尾插字符
2.append() 尾插字符串
3.operator+=
4.assign 覆盖
5.insert() 指定位置插入
6.erase() 删除
7.replace() 替换
8.swap() 交换
9.pop_back() 尾删
1.push_back() 尾插字符
void push_back (char c)
string s("i miss gjj");
s.push_back('!');
cout << s << endl;//i miss gjj!
2.append() 尾插字符串
1.string& append (const string& str) 尾插字符串
string s("i miss gjj");
s.append(",i love gjj");
cout << s << endl;//i miss gjj,i love gjj
2.string& appned (const string& str, size_t subpos, size_t sublen) 尾插字符串的第subpos位置开始的sublen字符(sublenda≥剩余字符串长度,则尾插剩余所有字符)
string s("i miss gjj");
s.append("040525", 2, 4);//i miss gjj0525
3.string& append (char* s) 尾插指针指向的字符串
string s("i miss gjj");
char p[] = "!!!";
s.append(p);
cout << s << endl;//i miss gjj!!!
4.string& append (char* s, size_t n) 尾插指针指向字符串的前n个字符
string s("i miss gjj");
char p[] = "5257";
s.append(p, 2);
cout << s << endl;//i miss gjj52
5.string& append (char c, size_t n) 尾插n个字符c
string s("i miss gjj");
s.append(10, '!');
cout << s << endl;//i miss gjj!!!!!!!!!!
6.template <class InputIterator>
string& append (InputIterator first, InputIterator last) 尾插迭代器指向范围的字符串
string s1("i miss gjj");
string s2("i miss gjj");
string s(" 5257 ");
s1.append(s.begin(), s.end());
s2.append(++s.begin(), s.end() - 2);
cout << s1 << endl;//i miss gjj 5257
cout << s2 << endl;//i miss gjj525
3.operator+=
1.string& operator+= (const string& str)
2.string& operator+= (const char* s)
3.string& operator+= (char c)
string s1(" i miss gjj !");
string s2(" gjj miss i !");
s1 += s2;
cout << s1 << endl;// i miss gjj ! gjj miss i !
s1 += "######";
cout << s1 << endl;// i miss gjj ! gjj miss i !######
s1 += '$';
cout << s1 << endl;// i miss gjj ! gjj miss i !######$
4.assign 覆盖
1.string& assign (const string& str) 用对象str数据覆盖现有对象数据
string s1("hello world");
string s2("gjj");
s1.assign(s2);
cout << s1 << endl;//gjj
2.string& assign (const string& str, size_t subpos, size_t sublen) 用对象str的第subpos位置开始的sublen个数据覆盖现有对象数据
string s1("hello world");
string s2("gjj");
s1.assign(s2, 1, 2);
cout << s1 << endl;//jj
3.string& assign (const char* s) 用字符串s覆盖现有对象数据
string s("!!!");
char p[] = "gjj and i";
s.assign(p);
cout << s << endl;//gjj and i
4.string& assign (const char* s, size_t n) 用字符串s的前n个字符覆盖现有对象数据
string s("!!!");
char p[] = "gjj and i 5257";
s.assign(p, 9);
cout << s << endl;//gjj and i
5.string& assign (size_t n, char c) 用n个字符c覆盖现有对象数据
string s("!!!");
s.assign(2, 'j');
cout << s << endl;//jj
6.template <class InputIterator>
string& assign (InputIterator first, InputIterator last) 用迭代器指定范围覆盖现有对象数据
string s1("!!!");
string s2("gjj");
s1.assign(s2.begin(), s2.end());
cout << s1 << endl;//gjj
5.insert() 指定位置插入
1.string& insert (size_t pos, const string& str) 在pos位置前插入对象str数据
string s1("gjj");
string s2("love ");
s1.insert(0, s2);
cout << s1 << endl;//love gjj
2.string& insert (size_t pos, const string& str, size_t subpos, size_t sublen) 在pos位置前插入对象str从subpos位置开始的sublen个数据
string s1("hello");
string s2(" world");
s1.insert(5, s2, 0, 6);
cout << s1 << endl;//hello world
3.string& insert (size_t pos, const char* s) 在pos位置前插入字符串s
string s1("hello");
char p[] = " world";
s1.insert(5, p);
cout << s1 << endl;//hello world
4.string& insert (size_t pos, const char* s, size_t n) 在pos位置前插入字符串s的前n个字符
string s1("hello ");
char p[] = "12345";
s1.insert(6, p, 3);
cout << s1 << endl;//hello 123
5.string& insert (size_t, pos, size_t n, char c) 在pos位置前插入n个字符c
void insert (iterator p, size_t n, char c) 在迭代器p指向位置前插入n个字符c
string s1("hello ");
string s2("hello ");
s1.insert(s1.begin(), 3, '#');
s2.insert(s2.end(), 3, '#');
cout << s1 << endl;//###hello
cout << s2 << endl;//hello ###
6.iterator insert (iterator p, char c) 在迭代器p指向位置插入字符c
string s("hello");
s.insert(s.end(), '!');
cout << s << endl;//hello!
7.template <class InputIterator>
void insert (iterator p, InputItrator first, InputItrator last) 在迭代器p指向的位置插入迭代器first和last指向的范围数据
string s1("hello");
string s2(" world");
s1.insert(s1.end(), s2.begin(), s2.end());
cout << s1 << endl;//hello world
6.erase() 删除
1.string& erase (size_t pos = 0, size_t len = npos) 从pos位置开始删除len个字符(如果不提供参数相当于clear,删除所有数据)
2.iterator erase (iterator p) 删除迭代器p指示的位置
3.iterator erase (iterator first, iterator last) 删除迭代器first与last指示范围之间的数据
string s("hello");
s.erase(2, 10);
cout << s << endl;//hestring s("hello");
s.erase(s.begin());
cout << s << endl;//ellostring s("hello");
s.erase(s.begin(), --s.end());
cout << s << endl;//o
7.replace() 替换
1.string& replace (size_t pos, size_t len, const string& str) 用str数据替换pos位置起的len个字符
string& replace (iterator i1, iterator i2, const string& str) 用str数据替换迭代器i1和i2指示范围数据
string s1("hello world");
string s2("#####");
s1.replace(2, 3, s2);
cout << s1 << endl;//he##### world
s1.replace(2, 3, "$$$$$");
cout << s1 << endl;//he$$$$$## world
s1.replace(s1.begin(), s1.end(), s2);
cout << s1 << endl;//#####
2.string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen)
用str的subpos位置开始的sublen个数据替换pos位置开始的len个数据
string s("hello world");
s.replace(6, 5, "bit", 0, 3);
cout << s << endl;//hello bit
3.string& replace (size_t pos, size_t len, const char* s) 用字符串s替代pos位置开始的len个数据
string& replace (iterator i1, iterator i2, const char* s) 用字符串s替代迭代器i1和i2指向的范围数据
string s("hello world");
char p1[] = "gjj";
char p2[] = "world";
s.replace(6, 5, p1);
cout << s << endl;//hello gjj
s.replace(s.begin() + 6, s.end(), p2);
cout << s << endl;//hello world
4.string& replace (size_t pos, size_t len, const char*s, size_t n) 用字符串s的前n个字符替代pos位置开始的len个数据
string& replace (iterator i1, iterator i2, const char*s, size_t n) 用字符串s的前n个数据替代迭代器i1和i2指向的范围数据
string s("hello world");
char p1[] = "gjj111";
char p2[] = "world222";
s.replace(6, 5, p1, 3);
cout << s << endl;//hello gjj
s.replace(s.begin() + 6, s.end(), p2, 5);
cout << s << endl;//hello world
5.string& replace (size_t pos, size_t len, size_t n, char c) 用n个字符c替换pos位置开始的len个数据
string& replace (iterator i1, iterator i2, size_t n, char c) 用n个字符c替换迭代器i1和i2指示的范围数据
string s("hello world");
s.replace(6, 5, 2, 'j');
cout << s << endl;//hello jj
s.replace(s.begin() + 6, s.end(), 2, 'z');
cout << s << endl;//hello zz
6.template <class InputIterator>
string& replace (iterator i1, iterator i2, InputIterator first, InputIterator last)
用迭代器first和last指示的数据范围替换迭代器i1和i2指示的数据范围
string s1("hello world");
string s2("gjj");
s1.replace(s1.begin() + 6, s1.end(), s2.begin(), s2.end());
cout << s1 << endl;//hello gjj
8.swap() 交换
void swap(string& str) 交换两个对象的数据
string s1("hello world");
string s2("hello gjj");
s1.swap(s2);
cout << s1 << endl;//hello gjj
cout << s2 << endl;//hello world
9.pop_back() 尾删
void pop_back() 删除最后一个字符
string s("hello gjj#");
s.pop_back();
cout << s << endl;//hello gjj
相关文章:
C++中string容器的修改操作
目录 1.push_back() 尾插字符 2.append() 尾插字符串 3.operator 4.assign 覆盖 5.insert() 指定位置插入 6.erase() 删除 7.replace() 替换 8.swap() 交换 9.pop_back() 尾删 1.push_back() 尾插字符 void push_back (char c) string s("i miss gjj"); s…...

Elasticsearch:虚拟形象辅助和对话驱动的语音到 RAG 搜索
作者:来自 Elastic Sunile Manjee 搜索的演变 搜索已经从产生简单结果的简单文本查询发展成为容纳文本、图像、视频和问题等各种格式的复杂系统。 如今的搜索结果通过生成式人工智能、机器学习和交互式聊天功能得到增强,提供更丰富、更动态且与上下文相…...

测试开发工程师(QA)职业到底需要干些什么?part7:硬件测试工程师QA
概述 硬件测试工程师QA主要负责确保硬件产品在设计、制造和交付过程中的质量和性能。主要任务是进行测试、验证和分析硬件系统、组件和设备,以确保其符合规格和质量标准。下面是硬件测试工程师QA在其工作中常涉及的一些方面: 测试计划和策略:…...

Python基础:标准库 -- pprint (数据美化输出)
1. pprint 库 官方文档 pprint --- 数据美化输出 — Python 3.12.2 文档 pprint — Data pretty printer — Python 3.12.2 documentation 2. 背景 处理JSON文件或复杂的嵌套数据时,使用普通的 print() 函数可能不足以有效地探索数据或调试应用程序。下面通过一…...

Visual Studio 小更新:改善变量的可见性
在 Visual Studio 2022 17.10 预览版 2 中,我们改善了一些小功能,例如:在调试版本中,变量窗口现已可以显示调用堆栈中任意帧的局部变量。 如需体验此功能,请直接安装最新预览版本,就可以知道是怎么一回事儿…...

C++自主点餐系统
一、 题目 设计一个自助点餐系统,方便顾客自己点餐,并提供对餐厅销售情况的统计和管理功能。 二、 业务流程图 三、 系统功能结构图 四、 类的设计 五、 程序代码与说明 头文件1. SystemMap.h #pragma once #ifndef SYSTEMMAP #define SYSTEMMAP #in…...

jconsole jvisualvm
jconsole 打开方式 命令行输入 jconsole双击想要连接的应用 界面展示 jvisualvm 打开方式 命令行输入 jvisualvm双击想要连接的应用 可以安装插件,比如 Visual GC 直观看到 GC 过程...
python vtkUnstructuredGrid 转 vtkAlgorithmOutput_
在VTK (Vtk.py)中,vtkUnstructuredGrid对象可以通过多种方式转换为vtkAlgorithmOutput_对象。这种转换通常在管道中使用,以将一个算法的输出传递给另一个算法作为其输入。 以下是一个简单的例子,展示如何将vtkUnstructuredGrid对象转换为 v…...

IS-IS路由
概览: Intermediate System-to-Intermediate System,中间系统到中间系统协议 IS-IS--IGP--链路状态协议--AD值:115 IS--中间系统(路由器) ES--终端系统(PC) 在早期IS-IS的开发并不是为了IP…...

打造新质生产力,亚信科技2024年如何行稳致远?
引言:不冒进、不激进,稳扎稳打, 一个行业一个行业地深度拓展。 【全球云观察 | 科技热点关注】 基于以往“一巩固、三发展”的多年业务战略,亚信科技正在落实向非通信行业、标准产品、软硬一体产品和国际市场的“四…...

开源博客项目Blog .NET Core源码学习(12:App.Application项目结构分析)
开源博客项目Blog的App.Application项目主要定义网站页面使用的数据类,同时定义各类数据的增删改查操作接口和实现类。App.Application项目未安装Nuget包,主要引用App.Core项目的类型。 App.Application项目的顶层文件夹如下图所示,下面逐…...

AES加密解密算法
一,AES算法概述 AES属于分组加密,算法明文长度固定为128位(单位是比特bit,1bit就是1位,128位等于16字节) 而密钥长度可以是128、192、256位 当密钥为128位时,需要循环10轮完成加密࿰…...
计算机网络(05)
计算机网络(04) 网络负载均衡 由多台服务器以对称的方式组成一个服务器集合每台服务器都具有等价的地位 , 可以单独对外提供服务而无须其他服务器的辅助均衡负载能够平均分配客户请求到服务器列阵,借此提供快速获取重要数据,解决…...

6、ChatGLM3-6B 部署实践
一、ChatGLM3-6B介绍与快速入门 ChatGLM3 是智谱AI和清华大学 KEG 实验室在2023年10月27日联合发布的新一代对话预训练模型。ChatGLM3-6B 是 ChatGLM3 系列中的开源模型,免费下载,免费的商业化使用。 该模型在保留了前两代模型对话流畅、部署门槛低等众多…...
python面试题(1~10)
1、列表(list)和元组(tuple)有什么区别? ①列表是不可变的,创建后可以对其进行修改。元组是不可变的,元组一旦创建,就不能对其进行修改。 ②列表表示的顺序,它们是有序…...

分类预测 | Matlab实现CNN-LSTM-Mutilhead-Attention卷积神经网络-长短期记忆网络融合多头注意力机制多特征分类预测
分类预测 | Matlab实现CNN-LSTM-Mutilhead-Attention卷积神经网络-长短期记忆网络融合多头注意力机制多特征分类预测 目录 分类预测 | Matlab实现CNN-LSTM-Mutilhead-Attention卷积神经网络-长短期记忆网络融合多头注意力机制多特征分类预测分类效果基本介绍模型描述程序设计参…...

SQLServer CONCAT 函数的用法
CONCAT函数用于将多个字符串值连接在一起。以下是一个简单的示例,演示了如何使用CONCAT函数: -- 创建一个示例表 CREATE TABLE ExampleTable (FirstName NVARCHAR(50),LastName NVARCHAR(50) );-- 插入一些示例数据 INSERT INTO ExampleTable (FirstNam…...
python快速入门一
变量 定义一个变量并打印到控制台 message "Hello World!" print(message)控制台输出 Hello World!修改变量 message "Hello World!" print(message) message "Hello Python World!" print(message)控制台输出 Hello World! Hello Pytho…...
Elasticsearch 面试题及参考答案:深入解析与实战应用
在大数据时代,Elasticsearch 以其强大的搜索能力和高效的数据处理性能,成为了数据架构师和开发者必备的技能之一。本文将为您提供一系列精选的 Elasticsearch 面试题及参考答案,帮助您在面试中脱颖而出,同时也为您的大数据架构设计提供实战参考。 1. 为什么要使用 Elastic…...
【ARM 嵌入式 C 入门及渐进 18 -- 字符数字转整形函数 atoi 介绍】
请阅读【嵌入式开发学习必备专栏 】 文章目录 字符数字转整形函数 atoiatoi 简单实现 字符数字转整形函数 atoi 在 C 语言中,main 函数能够接收命令行参数。这些参数通过两个参数传递给 main 函数:int argc 和 char *argv[]。argc 是命令行参数的数量&a…...
Vue记事本应用实现教程
文章目录 1. 项目介绍2. 开发环境准备3. 设计应用界面4. 创建Vue实例和数据模型5. 实现记事本功能5.1 添加新记事项5.2 删除记事项5.3 清空所有记事 6. 添加样式7. 功能扩展:显示创建时间8. 功能扩展:记事项搜索9. 完整代码10. Vue知识点解析10.1 数据绑…...

安宝特方案丨XRSOP人员作业标准化管理平台:AR智慧点检验收套件
在选煤厂、化工厂、钢铁厂等过程生产型企业,其生产设备的运行效率和非计划停机对工业制造效益有较大影响。 随着企业自动化和智能化建设的推进,需提前预防假检、错检、漏检,推动智慧生产运维系统数据的流动和现场赋能应用。同时,…...
三体问题详解
从物理学角度,三体问题之所以不稳定,是因为三个天体在万有引力作用下相互作用,形成一个非线性耦合系统。我们可以从牛顿经典力学出发,列出具体的运动方程,并说明为何这个系统本质上是混沌的,无法得到一般解…...
Python如何给视频添加音频和字幕
在Python中,给视频添加音频和字幕可以使用电影文件处理库MoviePy和字幕处理库Subtitles。下面将详细介绍如何使用这些库来实现视频的音频和字幕添加,包括必要的代码示例和详细解释。 环境准备 在开始之前,需要安装以下Python库:…...

成都鼎讯硬核科技!雷达目标与干扰模拟器,以卓越性能制胜电磁频谱战
在现代战争中,电磁频谱已成为继陆、海、空、天之后的 “第五维战场”,雷达作为电磁频谱领域的关键装备,其干扰与抗干扰能力的较量,直接影响着战争的胜负走向。由成都鼎讯科技匠心打造的雷达目标与干扰模拟器,凭借数字射…...
JVM暂停(Stop-The-World,STW)的原因分类及对应排查方案
JVM暂停(Stop-The-World,STW)的完整原因分类及对应排查方案,结合JVM运行机制和常见故障场景整理而成: 一、GC相关暂停 1. 安全点(Safepoint)阻塞 现象:JVM暂停但无GC日志,日志显示No GCs detected。原因:JVM等待所有线程进入安全点(如…...
Spring AI与Spring Modulith核心技术解析
Spring AI核心架构解析 Spring AI(https://spring.io/projects/spring-ai)作为Spring生态中的AI集成框架,其核心设计理念是通过模块化架构降低AI应用的开发复杂度。与Python生态中的LangChain/LlamaIndex等工具类似,但特别为多语…...

人工智能(大型语言模型 LLMs)对不同学科的影响以及由此产生的新学习方式
今天是关于AI如何在教学中增强学生的学习体验,我把重要信息标红了。人文学科的价值被低估了 ⬇️ 转型与必要性 人工智能正在深刻地改变教育,这并非炒作,而是已经发生的巨大变革。教育机构和教育者不能忽视它,试图简单地禁止学生使…...
LOOI机器人的技术实现解析:从手势识别到边缘检测
LOOI机器人作为一款创新的AI硬件产品,通过将智能手机转变为具有情感交互能力的桌面机器人,展示了前沿AI技术与传统硬件设计的完美结合。作为AI与玩具领域的专家,我将全面解析LOOI的技术实现架构,特别是其手势识别、物体识别和环境…...
uniapp 集成腾讯云 IM 富媒体消息(地理位置/文件)
UniApp 集成腾讯云 IM 富媒体消息全攻略(地理位置/文件) 一、功能实现原理 腾讯云 IM 通过 消息扩展机制 支持富媒体类型,核心实现方式: 标准消息类型:直接使用 SDK 内置类型(文件、图片等)自…...