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…...

华为云AI开发平台ModelArts
华为云ModelArts:重塑AI开发流程的“智能引擎”与“创新加速器”! 在人工智能浪潮席卷全球的2025年,企业拥抱AI的意愿空前高涨,但技术门槛高、流程复杂、资源投入巨大的现实,却让许多创新构想止步于实验室。数据科学家…...
ES6从入门到精通:前言
ES6简介 ES6(ECMAScript 2015)是JavaScript语言的重大更新,引入了许多新特性,包括语法糖、新数据类型、模块化支持等,显著提升了开发效率和代码可维护性。 核心知识点概览 变量声明 let 和 const 取代 var…...

ServerTrust 并非唯一
NSURLAuthenticationMethodServerTrust 只是 authenticationMethod 的冰山一角 要理解 NSURLAuthenticationMethodServerTrust, 首先要明白它只是 authenticationMethod 的选项之一, 并非唯一 1 先厘清概念 点说明authenticationMethodURLAuthenticationChallenge.protectionS…...
vue3 定时器-定义全局方法 vue+ts
1.创建ts文件 路径:src/utils/timer.ts 完整代码: import { onUnmounted } from vuetype TimerCallback (...args: any[]) > voidexport function useGlobalTimer() {const timers: Map<number, NodeJS.Timeout> new Map()// 创建定时器con…...
2023赣州旅游投资集团
单选题 1.“不登高山,不知天之高也;不临深溪,不知地之厚也。”这句话说明_____。 A、人的意识具有创造性 B、人的认识是独立于实践之外的 C、实践在认识过程中具有决定作用 D、人的一切知识都是从直接经验中获得的 参考答案: C 本题解…...

Git 3天2K星标:Datawhale 的 Happy-LLM 项目介绍(附教程)
引言 在人工智能飞速发展的今天,大语言模型(Large Language Models, LLMs)已成为技术领域的焦点。从智能写作到代码生成,LLM 的应用场景不断扩展,深刻改变了我们的工作和生活方式。然而,理解这些模型的内部…...

脑机新手指南(七):OpenBCI_GUI:从环境搭建到数据可视化(上)
一、OpenBCI_GUI 项目概述 (一)项目背景与目标 OpenBCI 是一个开源的脑电信号采集硬件平台,其配套的 OpenBCI_GUI 则是专为该硬件设计的图形化界面工具。对于研究人员、开发者和学生而言,首次接触 OpenBCI 设备时,往…...
十九、【用户管理与权限 - 篇一】后端基础:用户列表与角色模型的初步构建
【用户管理与权限 - 篇一】后端基础:用户列表与角色模型的初步构建 前言准备工作第一部分:回顾 Django 内置的 `User` 模型第二部分:设计并创建 `Role` 和 `UserProfile` 模型第三部分:创建 Serializers第四部分:创建 ViewSets第五部分:注册 API 路由第六部分:后端初步测…...
微服务通信安全:深入解析mTLS的原理与实践
🔥「炎码工坊」技术弹药已装填! 点击关注 → 解锁工业级干货【工具实测|项目避坑|源码燃烧指南】 一、引言:微服务时代的通信安全挑战 随着云原生和微服务架构的普及,服务间的通信安全成为系统设计的核心议题。传统的单体架构中&…...
第八部分:阶段项目 6:构建 React 前端应用
现在,是时候将你学到的 React 基础知识付诸实践,构建一个简单的前端应用来模拟与后端 API 的交互了。在这个阶段,你可以先使用模拟数据,或者如果你的后端 API(阶段项目 5)已经搭建好,可以直接连…...