string

server/2025/2/14 0:03:44/

string__0">string 概念

string 字符串其实是一种更加高级的封装,string字符串中包含大量的方法,
这些方法使得字符串的操作变得更加简单。

C++中将字符串直接作为一种类型,也就是string类型,使用string类型创建的
对象就是C++的字符串。

使用C++中提供的string是,必须添加头文件string

string__8">string 的常见操作

string__9">string 字符串的创建和初始化

#include string"><iostream>
#include string"><string>using namespace std;int main()
{string s1;//空字符串 string s2 = string">"hello world";string s3(string">"hello C++");cout << string">"s1:" << s1 << endl;cout << string">"s2:" << s2 << endl;cout << string">"s3:" << s3 << endl;return 0;
}

在这里插入图片描述
string 字符串的创建共有s1、s2、s3这三种方式
string s1;表示创建空字符串,就是说这个字符串是空的,并不是随机值
s2和s3是字符串创建和初始化的两种方式
注意:s2和s3中的字符串不再以\0作为结束标志了,跟C语言的不同
在这里插入图片描述
上面这个图,只是s2的示意图,实际上string类型的字符串比这个要复杂的多

C++中的string字符串和char类型的数组所表示的字符串还有一个区别
string类型的字符串对象可以直接赋值

#include string"><iostream>
#include string"><string>using namespace std;int main()
{string s1 = string">"hello world";string s2 = string">"XXX";cout << s2 << endl;s2 = s1;cout << s2 << endl;return 0;
}

在这里插入图片描述

string__59">string 字符串的输入

cin的方式

使用cin输入一个string的字符串,只能胜任不带空格的字符串

#include string"><iostream>
#include string"><string>using namespace std;int main()
{string s;//输入cin >> s;//输出cout << s << endl; return 0;
}

在这里插入图片描述
在这里插入图片描述

getline的方式

getline是C++标准库中的一个韩素华,用于从输入流中读取一行文本,并将其存储为字符串。
getline函数有两种不同的形式,分别对应着字符串的结束方式。

istream& getline(istream& is, string& str);
istream& getline(istream& is, string& str, char delim);

istream是输入流类型,cin是istream类型的标准输入流对象。
ostream是输出流类型,cout是ostream类型的标准输出流对象。

getline函数是输入流中读取一行文本信息,如果是在标准输入流(键盘)
中读取数据,就可以传cin给第一个参数

第一种getline函数以\n作为字符串的结束标志,它的一般格式是:

getline(cin, string str);
cin 表示从输入流中读取信息
str 是存放读取到的信息的字符串

这种形式的getline函数从输入流(例如cin)中读取文本,直到遇见\n为止,
然后将读取到的文本(不包括\n)存储到指定的string类型的变量str中。

#include string"><iostream>
#include string"><string>using namespace std;int main()
{string str;//输入getline(cin, str);//输出cout << str << endl; return 0;
}

在这里插入图片描述
第二种getline函数允许用户自定义结束标志,它的一般格式是:

getline(cin, string str, char delim);
cin 表示从输入流中读取信息
str 是存放读取到的信息的字符串
delim 是自定义的结束标志

这种形式的getline函数从输入流(例如cin)中读取文本,直到遇见用户指定
的结束标志(delim)为止,然后将读取到的文本(不包括结束标志字符)
存储到指定的string类型的变量str中。

#include string"><iostream>
#include string"><string>using namespace std;int main()
{string str;//输入getline(cin, str, 'e');//输出cout << str << endl; return 0;
}

在这里插入图片描述

size()

string中提供了size()函数用于获取字符串长度。
在C++中关于字符串的操作函数都是包含在syting中的,
所以需要调用这些函数时,通常用 **.**点运算符。

#include string"><iostream>
#include string"><string>using namespace std;int main()
{string s1;string s2 = string">"hello";string s3 = string">"hello world";string s4(string">"abc    ");cout << string">"s1:" << s1.size() << endl; cout << string">"s2:" << s2.size() << endl;cout << string">"s3:" << s3.size() << endl;cout << string">"s4:" << s4.size() << endl;return 0;
}

在这里插入图片描述
注意string类型的字符串是可以通过下标访问的

#include string"><iostream>
#include string"><string>using namespace std;int main()
{string s = string">"abcdef";for(int i = 0; i < s.size(); i++){cout << s[i] << string">" ";}cout << endl;return 0;
}

在这里插入图片描述
在这里插入图片描述

迭代器(iterator)

迭代器是一种对象,它可以用来遍历容器(比如string)中的元素,
迭代器的作用类似于指针,或者数组下标

begin() 和 end()

begin(): 返回指向字符串第一个字符的迭代器,需要一个迭代器的变量来接收
end(): 返回指向字符串最后一个字符的下一个位置的迭代器(该位置不属于字符串)
string中begin()和end()返回的迭代器类型是string::iterator

迭代器可以进行大小比较,也可以+或者-整数运算。
同一个容器的两个迭代器也可以相减,相减结果的绝对值是两个迭代器中间元素的个数。

#include string"><iostream>
#include string"><string>using namespace std;int main()
{string s = string">"abcdef";string::iterator it1 = s.begin();string::iterator it2 = s.end();cout << (it1 < it2) << endl;cout << it1 - it2 << endl;return 0;
}

在这里插入图片描述

迭代器通常用于遍历字符串,可以正序遍历,也可以逆序遍历。

正序遍历:

#include string"><iostream>
#include string"><string>using namespace std;int main()
{string s = string">"abcdef";for(string::iterator it = s.begin(); it < s.end(); it++){cout << *it << string">" ";}cout << endl;return 0;
}

在这里插入图片描述
在这里插入图片描述

逆序遍历

#include string"><iostream>
#include string"><string>using namespace std;int main()
{string s = string">"abcdef";for(string::iterator it = s.end() - 1; it >= s.begin(); it--){cout << *it << string">" ";}cout << endl;return 0;
}

在这里插入图片描述

通过迭代器找到元素后,改变迭代器指向的元素是可以直接改变字符串内容的。

#include string"><iostream>
#include string"><string>using namespace std;int main()
{string s = string">"abcdef";cout << s << endl;for(string::iterator it = s.begin(); it < s.end(); it++){*it = 'x';}cout << s << endl;return 0;
}

在这里插入图片描述

push_back()

push_back()用于在字符串尾部插一个字符

#include string"><iostream>
#include string"><string>using namespace std;int main()
{string s = string">"hello";cout << s << endl;s.push_back('w');cout << s << endl;return 0;
}

在这里插入图片描述

在这里插入图片描述

#include string"><iostream>
#include string"><string>using namespace std;int main()
{//向空字符串中插入字符 string s;s.push_back('w');s.push_back('o');s.push_back('r');s.push_back('l');s.push_back('d');cout << s << endl;//向非空字符串中插入字符 string s1 = string">"hello ";s1.push_back('w');s1.push_back('o');s1.push_back('r');s1.push_back('l');s1.push_back('d');cout << s1 << endl;//批量插入字符 string s2;for(char i = 'a'; i <= 'z'; i++){s2.push_back(i);}cout << s2 << endl;return 0;
}

在这里插入图片描述

string__361">string 的+=和+运算

string类型的字符串支持+和+=运算,本质是string中重载了operator+=这个操作符。

#include string"><iostream>
#include string"><string>using namespace std;int main()
{string s1 = string">"world";string s2 = string">"hello";//头部拼接cout << s2 + s1 << endl;cout << string">"h" + s1 << endl;cout << string">"hello" + s1 << endl;s1 = string">"hello" + s1;cout << s1 << endl;//尾部拼接s1 = string">"world";//让s1重新变为"world" cout << s1 + s2 << endl;cout << s1 + string">"h" << endl;cout << s1 + string">"hello" << endl;s1 += string">"hello";cout << s1 << endl;return 0;
}

在这里插入图片描述

pop_back

pop_back()用于删除字符串中最后一个字符。
这个成员函数是在C++11标准中引入的,有的编译器可能不支持。

#include string"><iostream>
#include string"><string>using namespace std;int main()
{string s = string">"hello";cout << s << endl;s.pop_back();cout << s << endl;return 0;
}

在这里插入图片描述

在这里插入图片描述
注意:当字符串中没有字符时,调用pop_back()的话,程序会出现异常。
这种行为也是未定义行为,要避免使用。

#include string"><iostream>
#include string"><string>using namespace std;int main()
{string s;s.pop_back();cout << s << endl;return 0;
}

在DevC++上程序最后崩了。
在这里插入图片描述
为避免删除导致上述情况,可以改写代码

#include string"><iostream>
#include string"><string>using namespace std;int main()
{string s = string">"abc";while(s.size() > 0)//通过size()函数来控制字符串长度 s.pop_back();return 0;
}

insert()

inset()可以在字符串中间的某个位置插入一个字符串。

函数原型如下:

string& insert(size_t pos, const string& str);//pos位置前面插入一个string字符串
string& insert(size_t pos, const char* c);//pos位置前面插入一个C风格的字符串
string& insert(size_t pos, size_t n, char c);//pos位置前面插入n个字符c
在这里插入图片描述

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

在这里插入图片描述

find()

find()函数用于查找字符串中指定子串/字符,并返回子串/字符在字符串中第一次出现的位置。

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就能知道是否查找到子串/字符

#include string"><iostream>
#include string"><string>using namespace std;int main()
{string s = string">"hello world hello everyone";string str = string">"llo";//查找string类型的字符串size_t n = s.find(str);cout << n << endl;n = s.find(str, n + 1);//从n+1位置开始查找strcout << n << endl;//查找C风格的字符串n = s.find(string">"llo");cout << n << endl;n = s.find(string">"llo", n + 1);//从n+1位置开始查找cout << n << endl; return 0;
}

在这里插入图片描述

在这里插入图片描述

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

在这里插入图片描述

#include string"><iostream>
#include string"><string>using namespace std;int main()
{string s = string">"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 string"><iostream>
#include string"><string>using namespace std;int main()
{string s = string">"hello world hello everyone";string str = string">"xxx";size_t n = s.find(str);if(n != string::npos)cout << string">"找到了,位置是:" << n << endl;elsecout << string">"找不到" << endl;return 0;
}

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

static const size_t npos = -1;

#include string"><iostream>
#include string"><string>using namespace std;int main()
{//注意:npos是string中定义的,使用npos需要带上string:: cout << string">"npos:" << string::npos << endl;return 0;
}

在这里插入图片描述
npos不是-1的原因是:-1是有符号的,而npos是size_t类型的,其是无符号的

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的子串。

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

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

在这里插入图片描述
在这里插入图片描述
substr()和find()经常配合使用,find负责找到位置,substr从这个位置向后获得字符串。

#include string"><iostream>
#include string"><string>using namespace std;int main()
{string s = string">"hello world hello everyone";size_t n = s.find(string">"world");string s1 = s.substr(n, 11);cout << s1 << endl;return 0;
}

在这里插入图片描述

string__687">string 的关系运算

支持的关系运算

运算: >、<、>=、<=、==、!=
string类型 与 string类型 的运算
string类型 与 char类型 的运算
char
类型 与 string类型 的运算

字符串比较

字符串的比较是基于字典序进行的,比较的是对应位置上字符的ASCII值的大小。
比如
“abc” < “aq”,'b’的ASCII值是小于’q’的
“abcdef” < “ff”,'a’的ASCII值是小于’f’的
“100” < “9”,'1’的ASCII值是小于’9’的

string__699">string 和数字的转换函数

stoi/stol

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

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,16进制等同。
如果传递的是0,会根据字符串的内容的信息自动推导进制。
若字符串中有0x,就认为是10进制,0开头会被认为是8进制,最终会转换成10进制。

#include string"><iostream>
#include string"><string>using namespace std;int main()
{size_t pos = 0;string s1 = string">"11x34";int ret1 = stoi(s1, &pos, 16);cout << ret1 << endl;//ret1得到的是16进制的11,换算成10进制是17cout << string">"pos:" << pos << endl;//pos中存放的是'x'的位置,因为16进制中没有'x',//所以无法正确匹配 string s2 = string">"11x34";int ret2 = stoi(s2, &pos, 2);cout << ret2 << endl;//2^1*1+2^0*1 == 3cout << string">"pos:" << pos << endl;string s3 = string">"0x11x34";int ret3 = stoi(s3, &pos, 0);cout << ret3 << endl;//0x说明是16进制,所以是17cout << string">"pos:" << pos << endl;//第二个'x'的下标位置是4 return 0;
}

在这里插入图片描述

stod/stof

stod是将字符串转换成double类型的值
stof是将字符串转换成float类型的值

double stod(const string& str, size_t* idx = 0);
float stof(const string& str, size_t* idx = 0);

#include string"><iostream>
#include string"><string>using namespace std;int main()
{string s = string">"3.14x456";double ret = stod(s, NULL);//不需要第二个参数可以写成NULL或者0 cout << ret << endl;return 0;
}

在这里插入图片描述

string_775">to_string

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);

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

在这里插入图片描述
请添加图片描述


http://www.ppmy.cn/server/167466.html

相关文章

本地大模型编程实战(11)与外部工具交互(2)

文章目录 准备定义工具方法创建提示词生成工具方法实参以 json 格式返回实参自定义 JsonOutputParser返回 json 调用工具方法定义通用方法用 链 返回结果返回结果中包含工具输入 总结代码 在使用 LLM(大语言模型) 时&#xff0c;经常需要调用一些自定义的工具方法完成特定的任务…

【大语言模型】最新ChatGPT、DeepSeek等大语言模型助力高效办公、论文与项目撰写、数据分析、机器学习与深度学习建模等科研应用

ChatGPT、DeepSeek等大语言模型助力科研应用 随着人工智能技术的快速发展&#xff0c;大语言模型如ChatGPT和DeepSeek在科研领域的应用正在为科研人员提供强大的支持。这些模型通过深度学习和大规模语料库训练&#xff0c;能够帮助科研人员高效地筛选文献、生成论文内容、进行数…

【工业场景】用YOLOv8实现烟雾识别

烟雾识别任务的应用场景主要主要体现在以下几个方面: 火灾预警:烟雾是火灾的早期信号,通过烟雾识别技术可以及时发现火灾并及时采取应急措施,减少火灾对人员和财产的损失。 工业安全监测:在工业生产过程中,烟雾可能是一些危险化学品泄露或燃烧产生的,通过烟雾识别可以实…

大模型被偷家?CNN结合多模态!

2025深度学习发论文&模型涨点之—— CNN多模态 卷积神经网络是一种特殊类型的神经网络&#xff0c;其主要结构包括卷积层、池化层、全连接层和输出层。卷积层通过卷积操作学习图像的特征&#xff0c;池化层通过下采样操作减少参数数量&#xff0c;全连接层和输出层通过分类…

DeepSeek服务器繁忙之谜:原因与解决方案

文章来源于百家号&#xff1a;GPU服务器厂家 DeepSeek&#xff0c;作为一款备受欢迎的人工智能平台&#xff0c;凭借其强大的功能和免费开放的策略&#xff0c;吸引了大量普通用户和开发者。然而&#xff0c;许多用户在使用过程中经常遇到“服务器繁忙&#xff0c;请稍后再试”…

IPC 共享通俗讲解及其安全风险

IPC 共享&#xff0c;指的是进程间通信&#xff08;IPC&#xff0c;Inter-Process Communication&#xff09;中的数据共享方式。简单来说&#xff0c;它允许多个进程&#xff08;程序&#xff09;相互交换信息或共享数据。 为什么需要 IPC 共享&#xff1f; 在计算机中&#…

深度学习-利用预训练的 ResNet 和 DenseNet 模型进行医学影像诊断

下面将分别展示基于 PyTorch 框架&#xff0c;利用预训练的 ResNet 和 DenseNet 模型进行医学影像诊断以提高准确率的代码实现。以肺炎 X 光影像诊断为例&#xff0c;假设数据集已经按照训练集和测试集划分好&#xff0c;每个类别存放在不同文件夹中。 1. 安装必要的库 pip i…

Ai无限免费生成高质量ppt教程(deepseek+kimi)

第一步&#xff1a;打开deepseek官网&#xff08;DeepSeek) 1.如果deepseek官网网络繁忙&#xff0c;解决方案如下&#xff1a; (1)超算互联网:超算互联网 (2)秘塔AI搜索:https://metaso.cn/(开启长思考&#xff09; (3)纳米ai:https://bot.n.cn/ (4)使用easychat官网&#xff…