NO.28十六届蓝桥杯备战|string|insert|find|substr|关系运算|stoi|stol|stod|stof|to_string(C++)

insert

如果我们需要在字符串中间的某个位置插⼊⼀个字符串,得掌握⼀个函数就是insert

string& insert (size_t pos, const string& str); 
//pos位置前⾯插⼊⼀个string字符串  
string& insert (size_t pos, const char* s); 
//pos位置前⾯插⼊⼀个C⻛格的字符串  
string& insert (size_t pos, size_t n, char c);
//pos位置前⾯插⼊n个字符c

![[Pasted image 20250306211142.png]]

#include <iostream>  
#include <string>  
using namespace std;  
int main()  
{  
	string s = "abcdefghi";  
	string str = "xxx";  
	cout << s << endl;  
	s.insert(3, str);  
	cout << s << endl;  
	return 0;  
} 

#include <iostream>  
#include <string>  
using namespace std;  
int main()  
{  
	string s = "abcdefghi";  
	cout << s << endl;  
	s.insert(3, "xxx");  
	cout << s << endl;  
	return 0;  
} 

#include <iostream>
#include <string>  
using namespace std;  
int main()  
{  
	string s = "abcdefghi";  
	cout << s << endl;  
	s.insert(3, 3, 'x');  
	cout << s << endl;  
	return 0;  
}
find()

find() 函数⽤于查找字符串中指定⼦串/字符,并返回⼦串/字符在字符串中第⼀次出现的位置
![[Pasted image 20250306211421.png]]

size_t find (const string& str, size_t pos = 0) const;  
//查找string类型的字符串str,默认是从头开始查找,pos可以指定位置开始  
size_t find (const char* s, size_t pos = 0) const;  
//查找C⻛格的字符串s,默认是从头开始查找,pos可以指定位置开始  
size_t find (const char* s, size_t pos, size_t n) const;  
//在字符串的pos这个位置开始查找C⻛格的字符串s中的前n个字符,  
size_t find (char c, size_t pos = 0) const;  
//查找字符c,默认是从头开始,pos可以指定位置开始
  • 若找到。返回⼦串/字符在字符串中第⼀次出现的起始下标位置。
  • 若未找到。返回⼀个整数值 npos (针对 npos 的介绍会在下⾯给出)。通常判断 find() 函数的返回值是否等于 npos 就能知道是否查找到⼦串或者字符。
//代码1  
#include <iostream>  
#include <string> //添加string头⽂件  
using namespace std;  
int main()  
{  
	string s = "hello world hello everyone";  
	string str = "llo";  
	//查找string类型的字符串  
	size_t n = s.find(str);  
	cout << n << endl;  
	n = s.find(str, n + 1); //从n+1这个指定位置开始查找  
	cout << n << endl;  
	//查找C⻛格的字符串  
	n = s.find("llo");  
	cout << n << endl;  
	n = s.find("llo", n + 1); //从n+1这个指定位置开始查找  
	cout << n << endl;  
	
	return 0;  
}  

//代码2  
#include <iostream>  
#include <string> //添加string头⽂件  
using namespace std;  
int main()  
{  
	string s = "hello world hello everyone";  
	//在s中,0这个指定位置开始查找"word"中的前3个字符  
	size_t n = s.find("word", 0, 3);  
	cout << n << endl;  
	n = s.find("everyday", n+1, 5);  
	cout << n << endl;
	
	return 0;  
}  

//代码3  
#include <iostream>  
#include <string> //添加string头⽂件  
using namespace std;  
int main()  
{  
	string s = "hello world hello everyone";  
	size_t n = s.find('o');  
	cout << n << endl;  
	n = s.find('o', n + 1);  
	cout << n << endl;  
	
	return 0;  
}  

//查找不到的情况  
#include <iostream>  
#include <string> //添加string头⽂件  
using namespace std;  
int main()  
{  
	string s = "hello world hello everyone";  
	string str = "bit";  
	size_t n = s.find(str);  
	cout << n << endl;  
	if(n != string::npos)  
	cout << "找到了,位置是:" << n << endl;  
	else  
	cout << "没有找到" << endl;  
	
	return 0;  
}

在字符串中查找字符或者字符串时,有可能查找不到,这时候 find 函数会返回 npos 这个值,该数字并不是⼀个随机的数字,⽽是 string 中定义的⼀个静态常量 npos 。我们通常会判断 find 函数的返回值是否等于 npos 来判断,查找是否成功

static const size_t npos = -1;
#include <iostream>  
#include <string> //添加string头⽂件  
using namespace std;  
int main()  
{  
	//注意:npos是string中定义的,使⽤npos需要带上string::指明是string类中的  
	cout << "npos:" << string::npos << endl;  
	return 0;  
}

![[Pasted image 20250306212530.png]]

substr()

substr() 函数⽤于截取字符串中指定位置指定⻓度的⼦串。函数原型如下

string substr (size_t pos = 0, size_t len = npos) const;  
//pos 的默认值是0,也就是从下标为0的位置开始截取  
//len 的默认值是npos,意思是⼀直截取到字符串的末尾
  • substr() :如果函数不传参数,就是从下标为0的位置开始截取,直到结尾,得到的是整个字符串;
  • substr(pos) :从指定下标 pos 位置开始截取⼦串,直到结尾;
  • substr(pos, len) :从指定下标 pos 位置开始截取⻓度为 len 的⼦串
    ![[Pasted image 20250306213426.png]]

返回值类型: string ,返回的是截取到的字符串,可以使⽤ string 类型的字符串接收

#include <iostream>  
#include<string> //添加string头⽂件  
using namespace std;  
int main()  
{  
	string s = "hello world hello everyone";  
	string s1 = s.substr(7);  
	cout << s1 << endl;  
	string s2 = s.substr(7, 6);  
	cout << s2 << endl;
	
	return 0;  
}

![[Pasted image 20250306214201.png]]

substr() 和 find() 经常是配合使⽤的, find 负责找到位置, substr 从这个位置向后获得字符串

#include <iostream>  
#include<string> //添加string头⽂件  
using namespace std;  
int main()  
{  
	string s = "hello world hello everyone";  
	size_t n = s.find("world");  
	string s2 = s.substr(n, 10);  
	cout << s2 << endl;  
	
	return 0;  
}

![[Pasted image 20250306214235.png]]

string的关系运算

在实际写代码的过程中经常会涉及到两个字符串⽐较⼤⼩,⽐如:判断你输⼊的密码是否正确,就得将输⼊的密码和数据库中正确的密码⽐较。
那么两个 string 类型字符串是否可以⽐较⼤⼩呢?其实是可以的,C++中为string提供了⼀系列的关系运算。

⽀持的关系运算
string s1 = "abc";  
string s2 = "abcd";  
char s3[] = "abcdef"; //C⻛格的字符串  
(1) s1 == s2  
bool operator== (const string& lhs, const string& rhs);//使⽤⽅式:s1 == s2  
bool operator== (const char* lhs, const string& rhs);//使⽤⽅式:s3 == s1  
bool operator== (const string& lhs, const char* rhs);//使⽤⽅式:s1 == s3  
(2) s1 != s2  
bool operator!= (const string& lhs, const string& rhs);//使⽤⽅式:s1 != s2  
bool operator!= (const char* lhs, const string& rhs);//使⽤⽅式:s3 != s1  
bool operator!= (const string& lhs, const char* rhs);//使⽤⽅式:s1 != s3  
(3) s1 < s2  
bool operator< (const string& lhs, const string& rhs);//使⽤⽅式:s1 < s2  
bool operator< (const char* lhs, const string& rhs);//使⽤⽅式:s3 < s1  
bool operator< (const string& lhs, const char* rhs);//使⽤⽅式:s1 < s3  
(4) s1 <= s2  
bool operator<= (const string& lhs, const string& rhs);//使⽤⽅式:s1 <= s2  
bool operator<= (const char* lhs, const string& rhs);//使⽤⽅式:s3 <= s1  
bool operator<= (const string& lhs, const char* rhs);//使⽤⽅式:s1 <= s3  
(5) s1 > s2  
bool operator> (const string& lhs, const string& rhs);//使⽤⽅式:s1 > s2  
bool operator> (const char* lhs, const string& rhs);//使⽤⽅式:s3 > s1  
bool operator> (const string& lhs, const char* rhs);//使⽤⽅式:s1 > s3  
(6) s1 >= s2  
bool operator>= (const string& lhs, const string& rhs);//使⽤⽅式:s1 >= s2  
bool operator>= (const char* lhs, const string& rhs);//使⽤⽅式:s3 >= s1  
bool operator>= (const string& lhs, const char* rhs);//使⽤⽅式:s1 >= s3

字符串的⽐较是基于字典序进⾏的,⽐较是对应位置上字符的ASCII值的⼤⼩;⽐较的不是字符串的⻓度。

"abc" < "aq" //'b'的ascii码值是⼩于'q'的  
"abcdef" < "ff" //'a'的ASCII码值是⼩于'f'的  
"100" < "9" //'1'的ASCII码值是⼩于'9'的
#include <iostream>  
#include<string>  
using namespace std;  
int main()  
{  
	string s1 = "hello world";  
	string s2 = "hello";  
	if (s1 == (s2 + " world"))  
	{  
		cout << "s1 == s2" << endl;  
	}  
	else  
	{  
		cout << "s1 != s2" << endl;  
	}  
	
	return 0;  
}
#include <iostream>  
#include <string>  
using namespace std;  
int main()  
{  
	string s1 = "abcd";  
	string s2 = "abbcdef";  
	char s3[] = "bbc";
	if (s1 > s2)  
	cout << "s1 > s2" << endl;  
	else  
	cout << "s1 <= s2" << endl;  
	if (s1 == s2)  
	cout << "s1 == s2" << endl;  
	else  
	cout << "s1 != s2" << endl;  
	if (s1 <= s3)  
	cout << "s1 <= s3" << endl;  
	else  
	cout << "s1 > s3" << endl;  
	
	return 0;  
}

和string相关的函数

stoi/stol
  • stoi 是将字符串转换成 int 类型的值
  • stol 是将字符串转换成 long int 类型的值

stoi 函数其实可以将⼀个 string 类型的字符串,转化为整型,函数原型如下

int stoi (const string& str, size_t* idx = 0, int base = 10);  
long stol (const string& str, size_t* idx = 0, int base = 10);
  • str 表⽰被转换的 string 类型的字符串
  • idx 是⼀个输出型参数,也就是这个通过这个参数会带回⼀个值。 idx 是⼀个指针,需要在外边创建⼀个 size_t 类型的值,传递它的地址给 idx ,这个参数将会带回 str 中⽆法正确匹配数字的第⼀个字符的位置。
  • base 表⽰被解析的字符串中数字的进制值,可能是 2 , 8 , 10 , 16 或者 0
    • 默认情况下这个值是 10 ,表⽰ 10 进制数字
    • 如果传递的是 2 ,表⽰被解析的字符串中是 2 进制的数字,最终会转换成 10 进制
    • 如果传递的是 8 ,表⽰被解析的字符串中是 8 进制的数字,最终会转换成 10 进制
    • 如果传递的是 16 ,表⽰被解析的字符串中是 16 进制的数字,最终会转换成 10 进制
    • 如果传递的是 0 ,会根据字符串的内容的信息⾃动推导进制,⽐如:字符串中有 0x ,就认为是 16 进制, 0 开头会被认为是 8 进制,最终会转换成 10 进制。
#include <iostream>  
#include<string>  
using namespace std;  
int main()  
{  
	size_t pos = 0;  
	
	string s1 = "11x34";  
	int ret1 = stoi(s1, &pos, 16);  
	cout << ret1 << endl;  
	cout << "pos:" << pos << endl;  
	
	string s2 = "11x34";  
	int ret2 = stoi(s2, &pos, 2);  
	cout << ret2 << endl;  
	cout << "pos:" << pos << endl;  
	
	string s3 = "0x11x34";  
	int ret3 = stoi(s3, &pos, 0);  
	cout << ret3 << endl;  
	cout << "pos:" << pos << endl;  
	
	return 0;  
}

![[Pasted image 20250306215758.png]]

stod/stof

stod 是将字符串转换成 double 类型的值,函数原型如下,和 stoi 函数的⽐较的话,少了描述字符串中数字进制的参数,其他参数⼀致。 stof 是将字符串转换成 flaot 类型的值。

double stod (const string& str, size_t* idx = 0);  
float stof (const string& str, size_t* idx = 0);
#include <iostream>  
#include<string>  
using namespace std;  
int main()  
{  
	string s = "3.14x456";  
	double ret = stod(s, NULL);  
	cout << ret << endl;  
	
	return 0;  
}

![[Pasted image 20250306220100.png]]

to_string
string to_string (int val);  
string to_string (long val);  
string to_string (long long val);  
string to_string (unsigned val);  
string to_string (unsigned long val);
string to_string (unsigned long long val);  
string to_string (float val);  
string to_string (double val);  
string to_string (long double val);

tostring 函数可以将数字转换成字符串,从上述函数原型的⻆度看的话,可以将整型、浮点型的数字转换成字符串的,使⽤起来也⾮常简单。

#include <iostream>  
#include <string>  
using namespace std;  
int main()  
{  
	string pi = "pi is " + to_string(3.14159);  
	cout << pi << endl;  
	return 0;  
}

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

贪心算法一

> 作者&#xff1a;დ旧言~ > 座右铭&#xff1a;松树千年终是朽&#xff0c;槿花一日自为荣。 > 目标&#xff1a;了解什么是贪心算法&#xff0c;并且掌握贪心算法。 > 毒鸡汤&#xff1a;有些事情&#xff0c;总是不明白&#xff0c;所以我不会坚持。早安! >…

逐梦DBA:MySQL的编码设置

一、MySQL的编码设置 1.1 默认插入中文数据存在的问题 1.1.1 在 MySQL5.7 版本&#xff0c;默认在安装成功后存在中文乱码的问题 1. 通过 show create table xxx查看可以发现默认的字符集&#xff1a; 2. show variables like character_%;查看编码命令发现默认为拉丁 如果我…

Windows 图形显示驱动开发-WDDM 3.2-GPU-P 设备上的实时迁移(一)

本文介绍了通过 SR-IOV(单根 I/O 虚拟化)分区虚拟化的异构计算设备(GPU、NPU 等)实时迁移的功能设计。 通过 WDDM 和 MCDM 驱动程序模型支持分区的设备现已成为我们虚拟化产品不可或缺的一部分。 因此&#xff0c;必须支持实时迁移并帮助我们的虚拟化抽象实现最大程度的可靠性&…

张驰咨询:用六西格玛重构动力电池行业的BOM成本逻辑

在动力电池行业&#xff0c;BOM&#xff08;物料清单&#xff09;成本每降低1%&#xff0c;都可能改写企业的利润曲线。某头部企业的三元锂电池BOM成本曾较行业标杆高出11%&#xff0c;单电芯利润率被压缩至3%的生死线。然而&#xff0c;通过张驰咨询的六西格玛方法论&#xff…

Java 大视界 -- Java 大数据在智能政务公共服务资源优化配置中的应用(118)

&#x1f496;亲爱的朋友们&#xff0c;热烈欢迎来到 青云交的博客&#xff01;能与诸位在此相逢&#xff0c;我倍感荣幸。在这飞速更迭的时代&#xff0c;我们都渴望一方心灵净土&#xff0c;而 我的博客 正是这样温暖的所在。这里为你呈上趣味与实用兼具的知识&#xff0c;也…

单例模式的五种实现方式

1、饿汉式 ①实现&#xff1a;在类加载的时候就初始化实例 ②优点&#xff1a;线程安全 ③缺点&#xff1a;实例在类加载的时候创建&#xff0c;可能会浪费资源 //饿汉式 public class EagerSingleton{private EagerSingleton(){} //私有构造方法private static EagerSingle…

WPS工具栏添加Mathtype加载项

问题描述&#xff1a; 分别安装好WPS和MathType之后&#xff0c;WPS工具栏没直接显示MathType工具&#xff0c;或者是前期使用正常&#xff0c;由于WPS更新之后MathType工具消失&#xff0c;如下图 解决办法 将文件“MathType Commands 2016.dotm”和“MathPage.wll”从Matht…

从开源大模型工具Ollama存在安全隐患思考企业级大模型应用如何严守安全红线

近日&#xff0c;国家网络安全通报中心通报大模型工具Ollama默认配置存在未授权访问与模型窃取等安全隐患&#xff0c;引发了广泛关注。Ollama作为一款开源的大模型管理工具&#xff0c;在为用户提供便捷的同时&#xff0c;却因缺乏有效的安全管控机制&#xff0c;存在数据泄露…

战略合作升级 | 大势智慧携手广西地测院,共绘智慧测绘新蓝图

2月26日&#xff0c;武汉大势智慧科技有限公司&#xff08;以下简称“大势智慧”&#xff09;与广西壮族自治区地理信息测绘院&#xff08;以下简称“广西地测院”&#xff09;在南宁举行战略合作升级签约仪式暨技术交流座谈会。 大势智慧董事长黄先锋与广西地测院党委书记、院…

MCU-SDRAM-W9825G6KH的存储单元

ARM-M7的Memory架构&#xff1a; 在Cortex-M7中&#xff0c;存储器一共有4GB的地址空间&#xff0c;4GB的地址空间又被划分为8个区域块&#xff0c;每个块有512M的内存。 Note&#xff1a;4GB的地址空间为 0x0000 0000 - 0xFFFF FFFF&#xff0c;可寻址的512M的地址空间为 0x00…

DeepSeek-R1国产化系统gpu驱动+cuda+ollama+webui可视化离线私有化部署

1.概述 网上大部分教程都是在线部署&#xff0c;完全离线私有化部署的文章不多&#xff0c;本文介绍从GPU驱动、cuda、ollama、deepseek模型和open webui等完全离线安装几个方面&#xff0c;让小白0基础也可以私有化部署大模型deepseek-R1。 我使用的设备是银河麒麟V10操作系统…

【蓝桥杯】每天一题,理解逻辑(3/90)【Leetcode 快乐数】

闲话系列&#xff1a;每日一题&#xff0c;秃头有我&#xff0c;Hello&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;,我是IF‘Maxue&#xff0c;欢迎大佬们来参观我写的蓝桥杯系列&#xff0c;我好久没有更新博客了&#xff0c;因为up猪我寒假用自己的劳动换了…

飞机大战lua迷你世界脚本

-- 迷你世界飞机大战 v1.2 -- 星空露珠工作室制作 -- 最后更新&#xff1a;2024年1月 ----------------------------- -- 迷你世界API适配配置 ----------------------------- local UI { BASE_ID 7477478487091949474-22856, -- UI界面ID ELEMENTS { BG 1, -- 背景 BTN_LE…

AI绘画软件Stable Diffusion详解教程(6):文生图、提示词细说与绘图案例

文生图即以文字描述来生成图像&#xff0c;这是目前所有AI绘画软件的基本功能之一。要想画一副好的图片&#xff0c;除了选择好的模型&#xff0c;在文生图中&#xff0c;提示词特别关键。 一、什么是提示词&#xff08;Prompt&#xff09; 提示词又称创意、关键词、咒语、ca…

C++编程:进阶阶段—4.1封装

C面向对象的三大特性&#xff1a;封装、继承、多态 具有相同性质的对象&#xff0c;抽象为类 4.1 封装 封装的意义&#xff1a;将属性和行为作为一个整体&#xff0c;表现生活中的事物&#xff0c;并将属性和行为加以权限控制。 4.1.1 类的定义及实例化对象 语法&#xff…

信奥赛CSP-J复赛集训(模拟算法专题)(1):P8813 [CSP-J 2022] 乘方

信奥赛CSP-J复赛集训&#xff08;模拟算法专题&#xff09;&#xff08;1&#xff09;&#xff1a;P8813 [CSP-J 2022] 乘方 题目描述 小文同学刚刚接触了信息学竞赛&#xff0c;有一天她遇到了这样一个题&#xff1a;给定正整数 a a a 和 b b b&#xff0c;求 a b a^b ab …

Flink深入浅出之02

深入浅出Flink-第二天 目标 掌握常见的DataStream常见的source掌握常见的DataStream的transformation操作掌握常见的DataStream的sink操作了解入门的DataSet API算子 &#x1f4d6; 1. DataStream 的编程模型 DataStream 的编程模型包括四个部分&#xff1a;Environment、D…

[C语言日寄] 字符串操作函数的使用及其拓展

【作者主页】siy2333 【专栏介绍】⌈c语言日寄⌋&#xff1a;这是一个专注于C语言刷题的专栏&#xff0c;精选题目&#xff0c;搭配详细题解、拓展算法。从基础语法到复杂算法&#xff0c;题目涉及的知识点全面覆盖&#xff0c;助力你系统提升。无论你是初学者&#xff0c;还是…

基于websocket的多用户网页五子棋 --- 测试报告

目录 功能测试自动化测试性能测试 功能测试 1.登录注册页面 2.游戏大厅页面 3.游戏房间页面 自动化测试 1.使用脑图编写web自动化测试用例 2.创建自动化项目&#xff0c;根据用例通过selenium来实现脚本 根据脑图进行测试用例的编写&#xff1a; 每个页面一个测试类&am…

【Linux】信号处理以及补充知识

目录 一、信号被处理的时机&#xff1a; 1、理解&#xff1a; 2、内核态与用户态&#xff1a; 1、概念&#xff1a; 2、重谈地址空间&#xff1a; 3、处理时机&#xff1a; 补充知识&#xff1a; 1、sigaction&#xff1a; 2、函数重入&#xff1a; 3、volatile&…