C++ string类详解

news2024/9/24 18:27:09

⭐️ 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)使用 nc 字符来填充字符串
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;	// 11
	cout << s.length() << endl;	// 11

	return 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 d
	cout << endl;

	// 自动迭代 自动判断结束
	// 范围for 本质上调用的也是迭代器
	for (auto val : s) {
		cout << val << " ";
	}
	// output: h e l l o   w o r l d
	cout << endl;

	for (string::reverse_iterator it = s.rbegin(); it != s.rend(); it++) {
		cout << *it << " ";
	}
	// output: d l r o w   o l l e h
	cout << endl;

	// const 
	const string s2 = "nihao";
	string::const_iterator it = s2.begin();
	while (it != s2.end()) {
		cout << *it << " ";
		it++;
	}
	// output: n i h a o

	cout << endl;
	string::const_reverse_iterator rit = s2.rbegin();
	while (rit != s2.rend()) {
		cout << *rit << " ";
		rit++;
	}
	// output: o a h i n

	return 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 world

	return 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() + 初始化,resizen 传的比 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; // 5
	
	string s2 = "hello world";
	s2.resize(5);
	cout << s2.size() << endl;	// 100
	cout << 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)返回一个 valstring 对象
4️⃣int stoi (const string& str, size_t* idx = 0, int base = 10)字符串转整数。 idx 通常都为 nullptrbase 代表进制

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;	 // 10
	

	val = stoi(s_val);
	cout << val << endl;	// 10

	return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/900360.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

USB隔离器电路分析,SA8338矽塔sytatek电机驱动,源特科技VPS8701,开关电源,电源 大师

一、 USB隔离器电路分析 进行usb隔离可以使用USB隔离模块 ADUM3160 ADUM4160 注意&#xff1a;B0505S 最大带载0.16A&#xff0c;副边需要带载能力需要改变方案 比如移动硬盘至少需要0.5A 用充电宝、18650、设计5V1A输出电源 二、 1A隔离电压方案

【蓝桥杯】[递归]母牛的故事

原题链接&#xff1a;https://www.dotcpp.com/oj/problem1004.html 目录 1. 题目描述 2. 思路分析 3. 代码实现 1. 题目描述 2. 思路分析 我们列一个年份和母牛数量的表格&#xff1a; 通过观察&#xff0c;找规律&#xff0c;我们发现&#xff1a; 当年份小于等于4时&…

Linux环境下python连接Oracle教程

下载Oracle client需要的 安装包 rpm包下载地址&#xff1a;Oracle官方下载地址 选择系统版本 选择Oracle版本 下载3个rpm安装包 oracle-instantclient12.2-basic-12.2.0.1.0-1.i386.rpm oracle-instantclient12.2-devel-12.2.0.1.0-1.i386.rpm oracle-instantclient12.2-sq…

算法通关村第八关——轻松搞定二叉树的深度和高度问题

1.基础知识 二叉树节点的高度&#xff1a;指从当前节点到叶子节点的最长简单路径边的条数 二叉树节点的深度&#xff1a;指从根节点到当前节点的最长简单路径边的条数 二叉树的深度和高度问题&#xff0c;递归思想的运用很是普遍&#xff0c;有的问题层序遍历也可以解决。 2.最…

PyTorch Lightning:通过分布式训练扩展深度学习工作流

一、介绍 欢迎来到我们关于 PyTorch Lightning 系列的第二篇文章&#xff01;在上一篇文章中&#xff0c;我们向您介绍了 PyTorch Lightning&#xff0c;并探讨了它在简化深度学习模型开发方面的主要功能和优势。我们了解了 PyTorch Lightning 如何为组织和构建 PyTorch 代码提…

3种获取OpenStreetMap数据的方法【OSM】

OpenStreetMap 是每个人都可以编辑的世界地图。 这意味着你可以纠正错误、添加新地点&#xff0c;甚至自己为地图做出贡献&#xff01; 这是一个社区驱动的项目&#xff0c;拥有数百万注册用户。 这是一个社区驱动的项目&#xff0c;旨在在开放许可下向每个人提供所有地理数据。…

基于YOLOv8模型的奶牛目标检测系统(PyTorch+Pyside6+YOLOv8模型)

摘要&#xff1a;基于YOLOv8模型的奶牛目标检测系统可用于日常生活中检测与定位奶牛目标&#xff0c;利用深度学习算法可实现图片、视频、摄像头等方式的目标检测&#xff0c;另外本系统还支持图片、视频等格式的结果可视化与结果导出。本系统采用YOLOv8目标检测算法训练数据集…

【小梦C嘎嘎——启航篇】vector 以及日常使用的接口介绍

【小梦C嘎嘎——启航篇】vector 日常使用的接口介绍&#x1f60e; 前言&#x1f64c;vector 是什么&#xff1f;vector 比较常使用的接口 总结撒花&#x1f49e; &#x1f60e;博客昵称&#xff1a;博客小梦 &#x1f60a;最喜欢的座右铭&#xff1a;全神贯注的上吧&#xff01…

Parking Steps

上面是老师傅说的停车步骤&#xff0c;说这样不会伤变速箱。 平时就是&#xff0c;脚踩刹车&#xff0c;直接从D档撸到P档&#xff0c;拉手刹&#xff0c;哈哈。 你的停车步骤是啥。。

redis 存储结构原理 2

咱们接着上一部分来进行分享&#xff0c;我们可以在如下地址下载 redis 的源码&#xff1a; https://redis.io/download 此处我下载的是 redis-6.2.5 版本的&#xff0c;xdm 可以直接下载上图中的 **redis-6.2.6 **版本&#xff0c; redis 中 hash 表的数据结构 redis hash …

php_mb_strlen指定扩展

1 中文在utf-字符集下占3个字节,所以计算出来长度为9。 2 可以引入php多字节字符的扩展&#xff0c;默认是没有的&#xff0c;需要自己配置这个函数 3 找到php.ini文件&#xff0c;去掉;extension mbstring的注释&#xff0c;接着重启apache服务 可以看到准确输出的中文的长度…

javascript期末作业【三维房屋设计】

1、引入three.js库 官网下载three.js 库 放置目录并引用 引入js文件: 设置场景&#xff08;scene&#xff09; &#xff08;1&#xff09;创建场景对象 &#xff08;2&#xff09;设置透明相机 1,透明相机的优点 透明相机机制更符合于人的视角,在场景预览和游戏场景多有使用…

视频怎么转gif高清动图?分享一款视频转gif工具

许多小伙伴都不知道如何将拍摄的短视频转gif图片&#xff0c;本文将分享一款专业的视频转gif工具&#xff0c;打来浏览器即可将视频在线转gif&#xff08;https://www.gif.cn&#xff09;&#xff0c;操作简单&#xff0c;使用方便&#xff0c;下面是详细的步骤。 打开网站&am…

SpringBoot案例-员工管理-新增员工

查看页面原型&#xff0c;明确需求 页面原型 需求 阅读接口文档 接口文档链接如下&#xff1a; 【腾讯文档】SpringBoot案例所需文档 https://docs.qq.com/doc/DUkRiTWVaUmFVck9N 思路分析 阅读需求文档后可知&#xff0c;前端发送请求的同时&#xff0c;将前端请求参数以…

centos8 使用phpstudy安装tomcat部署web项目

系统配置 1、安装Tomcat 2、问题 正常安装完Tomcat应该有个配置选项&#xff0c;用来配置server.xml web.xml 还有映射webapps路径选项&#xff0c;但是我用的这个版本并没有。所以只能曲线救国。 3、解决 既然没有配置项&#xff0c;那就只能按最基本的方法配置&#xff0c…

算法之排序总结

排序算法 最近&#xff0c;一直在学习业务上的知识&#xff0c;对基础没有怎么重视&#xff0c;因此&#xff0c;这篇文章想对于排序算法进行一个大致的总结&#x1f913;&#x1f913;&#x1f913;。 首先来说一下&#xff0c;关于排序一些相关的基础知识。 排序概述 原地…

代码随想录第25天|216.组合总和III ​​​​​​​,17. 电话号码的字母组合

216.组合总和III 回溯三部曲 确定递归函数参数 targetSum&#xff08;int&#xff09;目标和&#xff0c;也就是题目中的n。k&#xff08;int&#xff09;就是题目中要求k个数的集合。sum&#xff08;int&#xff09;为已经收集的元素的总和&#xff0c;也就是path里元素的…

(学习笔记-进程管理)什么是悲观锁、乐观锁?

互斥锁与自旋锁 最底层的两种就是 [互斥锁和自旋锁]&#xff0c;有很多高级的锁都是基于它们实现的。可以认为它们是各种锁的地基&#xff0c;所以我们必须清楚它们之间的区别和应用。 加锁的目的就是保证共享资源在任意时间内&#xff0c;只有一个线程访问&#xff0c;这样就…

LabVIEW模拟化学反应器的工作

LabVIEW模拟化学反应器的工作 近年来&#xff0c;化学反应器在化学和工业过程领域有许多应用。高价值产品是通过混合产品&#xff0c;化学反应&#xff0c;蒸馏和结晶等多种工业过程转换原材料制成的。化学反应器通常用于大型加工行业&#xff0c;例如酿酒厂公司饮料产品的发酵…

C 基础拾遗

C基础拾遗 预处理器 预处理器 14.1 预定义符号 14.2 #define