C++ string类详解
⭐️ string
string 是表示字符串的字符串类,该类的接口与常规容器的接口基本一致,还有一些额外的操作 string 的常规操作,在使用 string 类时,需要使用 #include <string> 以及 using namespace std;。
✨ 帮助文档:https://cplusplus.com/reference/string/string/string/
🌟 std::string::string 构造函数(constructor)
| 序号 | 构造函数 | 功能 |
|---|---|---|
| 1️⃣ | string() | 默认拷贝:构造空的string类对象,即空字符串,默认第一个字符位置是'\0',为了兼容c |
| 2️⃣ | string(const string& str) | 拷贝构造 |
| 3️⃣ | string(const string& str , size_t pos , size_t len = npos) | 拷贝构造的重载,从字符串 pos 位置开始拷贝,拷贝 len 个字符 |
| 4️⃣ | string(const char * s) | 使用 c_string 来初始化 string 类对象 |
| 5️⃣ | string(const char * s , size_t n) | 从 s 指向的字符串中复制前 n 个字符 |
| 6️⃣ | string(size_t n , char c) | 使用 n 个 c 字符来填充字符串 |
| 7️⃣ | template <class InputIterator> string (InputIterator first , InputIterator last) | 复制一个迭代器序列的字符串 [first , last) |
#include <iostream>
#include <string>
using namespace std;int main() {string s1; // 默认构造 第一个字符是 `\0`cout << s1 << endl;/*调用构造本身的写法应该是:string s2("hello world");但是为什么可以这样写呢? string s2 = "hello world";因为当构造函数是单参的时候,是支持隐式类型转换的。这里本质上先构造再拷贝构造,但是编译器通常会优化为直接构造。若不希望隐式类型的转换可以在构造函数前添加 explicit 关键字*/string s2 = "hello world"; // 使用c字符串构造一个string类对象cout << s2 << endl;string s3(s2); // 拷贝构造cout << s3 << endl;string s4(s3, 6, 5); // 拷贝构造的重载 从下标6位置开始拷贝 拷贝5个字符cout << s4 << endl;string s5("hello" , 3); // 拷贝 `hello` 前3个字符cout << s5 << endl;string s6(10 , 'a'); // 使用 10个 `a` 填充对象cout << s6 << endl;string s7(s6.begin() , s6.end()); // 迭代器序列初始化cout << s7 << endl;return 0;
}
output:

ps: npos 原型:static const size_t npos = -1;。npos 的访问:string::npos。无符号的 -1 相当于是整型的最大值,若当不传这个参数时, 或者 len 大于后面剩余字符的长度,那么使用默认参数 npos都是相当于 直到字符串的结束。[ 返回 ]

🌟 std::string::operator= 赋值重载
| 序号 | 函数名称 | 功能 |
|---|---|---|
| 1️⃣ | string& operator= (const string& str) | 用一个新的 string 对象替换当前 string 对象的内容 |
| 2️⃣ | string& operator= (const char * s) | 用一个c的字符串替换当前 string 对象内容 |
| 3️⃣ | string& operator= (char c) | 使用一个字符替换当前 string 对象的内容 |
#include <iostream>
#include <string>
using namespace std;int main() {string s1 = "hello world";
// 1.string temp = "ccc";s1 = temp;cout << s1 << endl;
// 2.s1 = "hhhh";cout << s1 << endl;
// 3.s1 = 'a';cout << s1 << endl;return 0;
}
output:

🌟 元素的访问
| 序号 | 函数名称 | 功能 |
|---|---|---|
| 1️⃣ | char& operator[] (size_t pos) | 返回当前 string 对象中 pos 位置字符的引用 |
| 2️⃣ | const char& operator[](size_t pos) const | 返回当前 const string 对象中 pos 位置字符的引用 |
| 3️⃣ | char& at (size_t pos) | 返回当前 string 对象中 pos 位置字符的引用 |
| 4️⃣ | const char& at (size_t pos) const | 返回当前 const string 对象中 pos 位置字符的引用 |
| 5️⃣ | char& back() | 返回当前 string 对象最后一个字符的引用 |
| 6️⃣ | const char& back() const | 返回当前 const string 对象最后一个字符的引用 |
| 7️⃣ | char& front() | 返回当前 string 对象第一个字符的引用 |
| 8️⃣ | const char& front() const | 返回当前 const string 对象第一个字符的引用 |
ps: at 和 [] 的行为是一样的,函数都会检查 pos 是否是合法位置,若不是,[] 是断言错误,而 at 是抛异常。
ps: back == [xx.size() - 1]、front == [0]。
#include <iostream>
#include <string>
using namespace std;int main() {const string s1 = "hello";for (size_t i = 0; i < s1.size(); i++) {cout << s1[i] << " ";}cout << endl;string s2 = "world";for (size_t i = 0; i < s2.size(); i++) {s2[i]++;cout << s2[i] << " ";}cout << endl;string s3 = "hello world";cout << s3.back() << s3[s3.size() - 1] << endl;cout << s3.front() << s3[0] << endl;cout << s3.at(4) << endl;return 0;
}
output:

🌟 元素的长度
| 序号 | 函数名称 | 功能 |
|---|---|---|
| 1️⃣ | size_t size() const | 返回 string 对象实际字符的长度 |
| 2️⃣ | size_t length() const | 返回 string 对象实际字符的长度 |
#include <iostream>
#include <string>
using namespace std;int main() {string s = "hello world";cout << s.size() << endl; // 11cout << s.length() << endl; // 11return 0;
}
🌟 string 迭代器
| 序号 | 函数名称 | 功能 |
|---|---|---|
| 1️⃣ | iterator begin() | 返回一个迭代器,该迭代器指向 string 对象的第一个字符 |
| 2️⃣ | const_iterator begin() const | 返回一个迭代器,该迭代器指向 const string 对象的第一个字符 |
| 3️⃣ | iterator end() | 返回一个迭代器,该迭代器指向 string 对象最后一个实际字符的下一个位置 |
| 4️⃣ | const_iterator end() const | 返回一个迭代器,该迭代器指向 const string 对象最后一个实际字符的下一个位置 |
| 5️⃣ | reverse_iterator rbegin() | 返回一个反向迭代器,该迭代器指向 string 对象最后一个实际字符的位置 |
| 6️⃣ | const_reverse_iterator rbegin() const | 返回一个反向迭代器,该迭代器指向 const string 对象最后一个实际字符的位置 |
| 7️⃣ | reverse_iterator() rend() | 返回一个反向迭代器,该迭代器指向 string 对象第一个字符的前一个位置 |
| 8️⃣ | const_reverse_iterator() rend() const | 返回一个反向迭代器,该迭代器指向 const string 对象第一个字符的前一个位置 |
ps: [ begin() , end() )、( rend() , rbegin() ]
#include <iostream>
#include <string>
using namespace std;int main() {string s = "hello world";for (string::iterator it = s.begin(); it != s.end(); it++) {cout << *it << " ";}// output: h e l l o w o r l dcout << endl;// 自动迭代 自动判断结束// 范围for 本质上调用的也是迭代器for (auto val : s) {cout << val << " ";}// output: h e l l o w o r l dcout << endl;for (string::reverse_iterator it = s.rbegin(); it != s.rend(); it++) {cout << *it << " ";}// output: d l r o w o l l e hcout << endl;// const const string s2 = "nihao";string::const_iterator it = s2.begin();while (it != s2.end()) {cout << *it << " ";it++;}// output: n i h a ocout << endl;string::const_reverse_iterator rit = s2.rbegin();while (rit != s2.rend()) {cout << *rit << " ";rit++;}// output: o a h i nreturn 0;
}
output:

🌟 string 对象修改
| 序号 | 函数名称 | 功能 |
|---|---|---|
| 1️⃣ | void push_back (char c) | 在当前 string 对象的末尾追加一个 c 字符 |
| 2️⃣ | string& append (const string& str) | 在当前 string 对象的末尾追加一个 const string str 对象 |
| 3️⃣ | string& append (const string& str , size_t subpos , size_t sublen) | 在当前 string 对象的末尾追加一个 const string str 对象的子串,从 subpos 位置开始,拷贝 sublen 个字符过去 类似上面构造函数的 npos 用法 |
| 4️⃣ | string& append (const char* s); | 在当前 string 对象的末尾追加一个 c_string 字符串 |
| 5️⃣ | template <class InputIterator> string& append (InputIterator first , InputIterator last) | 追加一个迭代器序列的字符串 [first , last) |
#include <iostream>
#include <string>
using namespace std;int main() {string s = "hello";s.push_back('-');cout << s << endl;s = "hello";string temp = " world";s.append(temp);cout << s << endl;string s2 = "hello";string temp2 = " world";s2.append(temp2 , 0 , 3);cout << s2 << endl;string s3 = "hello";s3.append(" world");cout << s3 << endl;string s4 = "hello";s4.append(s4.begin(), s4.end());cout << s4 << endl;string s5 = "hello";s5.append(s5.rbegin() , s5.rend());cout << s5 << endl;return 0;
}
output:

std::string::operator+= 运算符重载
| 序号 | 函数名称 | 功能 |
|---|---|---|
| 6️⃣ | string& operator+= (const string& str); | 在当前 string 对象的末尾追加一个 const string str 对象 |
| 7️⃣ | string& operator+= (const char* s); | 在当前 string 对象的末尾追加一个 c_string 字符串 |
| 8️⃣ | string& operator+= (char c); | 在当前 string 对象的末尾追加一个 c 字符 |
#include <iostream>
#include <string>
using namespace std;int main() {string s = "he";s += 'l';s += 'l';s += "o ";string temp = "world";s += temp;cout << s << endl;// output: hello worldreturn 0;
}
🌟 元素的容量
| 序号 | 函数名称 | 功能 |
|---|---|---|
| 1️⃣ | size_t capacity() const | 返回当前 string 对象的容量大小 |
| 2️⃣ | void reserve (size_t n = 0) | 改变当前 string 对象的容量为 n |
| 3️⃣ | void resize (size_t n) | 将当前 string 对象的 size() 调整为 n 并初始化为 '\0' |
| 4️⃣ | void resize (size_t n , char c) | 将当前 string 对象的 size() 调整为 n 并初始化为 c |
| 5️⃣ | void clear(); | 删除当前 string 对象的所有内容,size() = 0 |
| 6️⃣ | bool empty() const | 若当前 string 对象为空返回 true,否则返回 false |
ps: reserve 是改变容量,而 resize 是改变 size() + 初始化,当 resize 的 n 传的比 string 的大小还小,则就是删除。
#include <iostream>
#include <string>
using namespace std;int main() {string s = "hello";cout << s.capacity() << endl;s.reserve(100);cout << s.capacity() << endl;cout << s.size() << endl; // 5string s2 = "hello world";s2.resize(5);cout << s2.size() << endl; // 100cout << s2 << endl;s2.clear();cout << s2.empty() << endl;return 0;
}
output:

🌟 std::string::insert 插入

ps: 需要的查文档即可,效率不高很少用。
🌟 std::string::erase 删除

🌟 std::string::c_str 返回c的字符串
| 序号 | 函数名称 | 功能 |
|---|---|---|
| 1️⃣ | const char* c_str() const | 返回c的字符串使用 '\0' 结尾 |
🌟 查找
| 序号 | 函数名称 | 功能 |
|---|---|---|
| 1️⃣ | size_t find (char c , size_t pos = 0) const | 从当前 string 对象的 pos 位置开始查找 c 字符,返回这个字符第一次出现的位置,否则返回 string::npos |
| 2️⃣ | string substr (size_t pos = 0 , size_t len = npos) const | 返回当前对象 pos 位置开始的 len 个字符的子串 |
#include <iostream>
#include <string>
using namespace std;int main() {string s = "hello world";size_t res = s.find('w' , 0);if (res != string::npos) {cout << s.substr(res) << endl; // world}return 0;
}
| 序号 | 函数名称 | 功能 |
|---|---|---|
| 3️⃣ | size_t rfind (char c , size_t pos = npos) const | 从当前 string 对象的 pos 位置从后向前开始查找 c 字符,返回这个字符最后一次出现的位置,否则返回 string::npos |
🌟 其他
| 序号 | 函数名称 | 功能 |
|---|---|---|
| 1️⃣ | istream& getline (istream& is , string& str , char delim) | 输入一行字符遇到 delim 终止 |
| 2️⃣ | istream& getline (istream& is , string& str) | 输入一行字符遇到 \n 终止 |
| 3️⃣ | string to_string (int val) | 返回一个 val 的 string 对象 |
| 4️⃣ | int stoi (const string& str, size_t* idx = 0, int base = 10) | 字符串转整数。 idx 通常都为 nullptr,base 代表进制 |
ps: to_string 支持的转换类型

ps: string 可以转换为的类型

#include <iostream>
#include <string>
using namespace std;int main() {int val = 10;string s_val = to_string(val);cout << s_val << endl; // 10val = stoi(s_val);cout << val << endl; // 10return 0;
}
相关文章:
C++ string类详解
⭐️ string string 是表示字符串的字符串类,该类的接口与常规容器的接口基本一致,还有一些额外的操作 string 的常规操作,在使用 string 类时,需要使用 #include <string> 以及 using namespace std;。 ✨ 帮助文档&…...
深入浅出Pytorch函数——torch.nn.init.ones_
分类目录:《深入浅出Pytorch函数》总目录 相关文章: 深入浅出Pytorch函数——torch.nn.init.calculate_gain 深入浅出Pytorch函数——torch.nn.init.uniform_ 深入浅出Pytorch函数——torch.nn.init.normal_ 深入浅出Pytorch函数——torch.nn.init.c…...
一、docker及mysql基本语法
文章目录 一、docker相关命令二、mysql相关命令 一、docker相关命令 (1)拉取镜像:docker pull <镜像ID/image> (2)查看当前docker中的镜像:docker images (3)删除镜像&#x…...
【计算机网络】13、ARP 包:广播自己的 mac 地址和 ip
机器启动时,会向外广播自己的 mac 地址和 ip 地址,这个即称为 arp 协议。范围是未经过路由器的部分,如下图的蓝色部分,范围内的设备都会在本地记录 mac 和 ip 的绑定信息,若有重复则覆盖更新(例如先收到 ma…...
通过微软Azure调用GPT的接口API-兼容平替OpenAI官方的注意事项
众所周知,我们是访问不通OpenAI官方服务的,但是我们可以自己通过代理或者使用第三方代理访问接口 现在新出台的规定禁止使用境外的AI大模型接口对境内客户使用,所以我们需要使用国内的大模型接口 国内的效果真的很差,现在如果想使…...
回归预测 | MATLAB实现BO-SVM贝叶斯优化支持向量机多输入单输出回归预测(多指标,多图)
回归预测 | MATLAB实现BO-SVM贝叶斯优化支持向量机多输入单输出回归预测(多指标,多图) 目录 回归预测 | MATLAB实现BO-SVM贝叶斯优化支持向量机多输入单输出回归预测(多指标,多图)效果一览基本介绍程序设计…...
GAN!生成对抗网络GAN全维度介绍与实战
目录 一、引言1.1 生成对抗网络简介1.2 应用领域概览1.3 GAN的重要性 二、理论基础2.1 生成对抗网络的工作原理2.1.1 生成器生成过程 2.1.2 判别器判别过程 2.1.3 训练过程训练代码示例 2.1.4 平衡与收敛 2.2 数学背景2.2.1 损失函数生成器损失判别器损失 2.2.2 优化方法优化代…...
自动驾驶仿真:基于Carsim开发的加速度请求模型
文章目录 前言一、加速度输出变量问题澄清二、配置Carsim动力学模型三、配置Carsim驾驶员模型四、添加VS Command代码五、Run Control联合仿真六、加速度模型效果验证 前言 1、自动驾驶行业中,算法端对于纵向控制的功能预留接口基本都是加速度,我们需要…...
.netcore grpc客户端工厂及依赖注入使用
一、客户端工厂概述 gRPC 与 HttpClientFactory 的集成提供了一种创建 gRPC 客户端的集中方式。可以通过依赖包Grpc.Net.ClientFactory中的AddGrpcClient进行gRPC客户端依赖注入AddGrpcClient函数提供了许多配置项用于处理一些其他事项;例如AOP、重试策略等 二、案…...
C语言入门_Day7 逻辑运算
目录: 前言 1.逻辑运算 2.优先级 3.易错点 4.思维导图 前言 算术运算用来进行数据的计算和处理;比较运算是用来比较不同的数据,进而来决定下一步怎么做;除此以外还有一种运算叫做逻辑运算,它的应用场景也是用来影…...
什么是Eureka?以及Eureka注册服务的搭建
导包 <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation"http://maven.apache.org/POM/4.0.0 htt…...
Docker安装并配置镜像加速器,镜像、容器的基本操作
目录 1.安装docker服务,配置镜像加速器 (1)安装依赖的软件包 (2)设置yum源,我配置的阿里仓库 (3)选择一个版本安装 (4)启动docker服务,并设置…...
前端 -- 基础 网页、HTML、 WEB标准 扫盲详解
什么是网页 : 网页是构成网站的基本元素,它通常由 图片、链接、文字、声音、视频等元素组成。 通常我们看到的网页 ,常见以 .html 或 .htm 后缀结尾的文件, 因此俗称 HTML 文件 什么是 HTML : HTML 指的是 超文本标记语言,…...
分布式锁实现方式
分布式锁 1 分布式锁介绍 1.1 什么是分布式 一个大型的系统往往被分为几个子系统来做,一个子系统可以部署在一台机器的多个 JVM(java虚拟机) 上,也可以部署在多台机器上。但是每一个系统不是独立的,不是完全独立的。需要相互通信ÿ…...
C语言小练习(一)
🌞 “人生是用来体验的,不是用来绎示完美的,接受迟钝和平庸,允许出错,允许自己偶尔断电,带着遗憾,拼命绽放,这是与自己达成和解的唯一办法。放下焦虑,和不完美的自己和解…...
Flask-flask系统运行后台轮询线程
对于有些flask系统,后台需要启动轮询线程,执行特定的任务,以下是一个简单的例子。 globals/daemon.py import threading from app.executor.ops_service import find_and_run_ops_task_todo_in_redisdef context_run_func(app, func):with …...
jsp本质-servlet
jsp本质-servlet 一、jsp文件 <% page language"java" contentType"text/html; charsetUTF-8" pageEncoding"UTF-8"%> <!DOCTYPE html> <html> <head><meta charset"UTF-8"><title>JSP Example…...
回归预测 | MATLAB实现GWO-SVM灰狼优化算法优化支持向量机多输入单输出回归预测(多指标,多图)
回归预测 | MATLAB实现GWO-SVM灰狼优化算法优化支持向量机多输入单输出回归预测(多指标,多图) 目录 回归预测 | MATLAB实现GWO-SVM灰狼优化算法优化支持向量机多输入单输出回归预测(多指标,多图)效果一览基…...
科技资讯|苹果Vision Pro新专利曝光:可调节液态透镜
苹果公司近日申请了名为“带液态镜头的电子设备”,概述了未来可能的头显设计。头显设备中的透镜采用可调节的液态透镜,每个透镜可以具有填充有液体的透镜腔,透镜室可以具有形成光学透镜表面的刚性和 / 或柔性壁。 包括苹果自家的 Vision Pr…...
神经网络基础-神经网络补充概念-38-归一化输入
概念 归一化输入是一种常见的数据预处理技术,旨在将不同特征的取值范围映射到相似的尺度,从而帮助优化机器学习模型的训练过程。归一化可以提高模型的收敛速度、稳定性和泛化能力,减少模型受到不同特征尺度影响的情况。 常见的归一化方法 …...
简易版抽奖活动的设计技术方案
1.前言 本技术方案旨在设计一套完整且可靠的抽奖活动逻辑,确保抽奖活动能够公平、公正、公开地进行,同时满足高并发访问、数据安全存储与高效处理等需求,为用户提供流畅的抽奖体验,助力业务顺利开展。本方案将涵盖抽奖活动的整体架构设计、核心流程逻辑、关键功能实现以及…...
JavaScript 中的 ES|QL:利用 Apache Arrow 工具
作者:来自 Elastic Jeffrey Rengifo 学习如何将 ES|QL 与 JavaScript 的 Apache Arrow 客户端工具一起使用。 想获得 Elastic 认证吗?了解下一期 Elasticsearch Engineer 培训的时间吧! Elasticsearch 拥有众多新功能,助你为自己…...
(二)TensorRT-LLM | 模型导出(v0.20.0rc3)
0. 概述 上一节 对安装和使用有个基本介绍。根据这个 issue 的描述,后续 TensorRT-LLM 团队可能更专注于更新和维护 pytorch backend。但 tensorrt backend 作为先前一直开发的工作,其中包含了大量可以学习的地方。本文主要看看它导出模型的部分&#x…...
五年级数学知识边界总结思考-下册
目录 一、背景二、过程1.观察物体小学五年级下册“观察物体”知识点详解:由来、作用与意义**一、知识点核心内容****二、知识点的由来:从生活实践到数学抽象****三、知识的作用:解决实际问题的工具****四、学习的意义:培养核心素养…...
基础测试工具使用经验
背景 vtune,perf, nsight system等基础测试工具,都是用过的,但是没有记录,都逐渐忘了。所以写这篇博客总结记录一下,只要以后发现新的用法,就记得来编辑补充一下 perf 比较基础的用法: 先改这…...
(二)原型模式
原型的功能是将一个已经存在的对象作为源目标,其余对象都是通过这个源目标创建。发挥复制的作用就是原型模式的核心思想。 一、源型模式的定义 原型模式是指第二次创建对象可以通过复制已经存在的原型对象来实现,忽略对象创建过程中的其它细节。 📌 核心特点: 避免重复初…...
爬虫基础学习day2
# 爬虫设计领域 工商:企查查、天眼查短视频:抖音、快手、西瓜 ---> 飞瓜电商:京东、淘宝、聚美优品、亚马逊 ---> 分析店铺经营决策标题、排名航空:抓取所有航空公司价格 ---> 去哪儿自媒体:采集自媒体数据进…...
Spring AI Chat Memory 实战指南:Local 与 JDBC 存储集成
一个面向 Java 开发者的 Sring-Ai 示例工程项目,该项目是一个 Spring AI 快速入门的样例工程项目,旨在通过一些小的案例展示 Spring AI 框架的核心功能和使用方法。 项目采用模块化设计,每个模块都专注于特定的功能领域,便于学习和…...
如何应对敏捷转型中的团队阻力
应对敏捷转型中的团队阻力需要明确沟通敏捷转型目的、提升团队参与感、提供充分的培训与支持、逐步推进敏捷实践、建立清晰的奖励和反馈机制。其中,明确沟通敏捷转型目的尤为关键,团队成员只有清晰理解转型背后的原因和利益,才能降低对变化的…...
【堆垛策略】设计方法
堆垛策略的设计是积木堆叠系统的核心,直接影响堆叠的稳定性、效率和容错能力。以下是分层次的堆垛策略设计方法,涵盖基础规则、优化算法和容错机制: 1. 基础堆垛规则 (1) 物理稳定性优先 重心原则: 大尺寸/重量积木在下…...
