
🎬 博主名称 :键盘敲碎了雾霭
🔥 个人专栏 : 《C语言》《数据结构》 《C++》
⛺️指尖敲代码,雾霭皆可破

文章目录
- 一、Modifiers(修正)
-
- [1.1 push_back](#1.1 push_back)
- [1.2 append](#1.2 append)
- [1.3 +=(最常用的)](#1.3 +=(最常用的))
- [1.4 insert](#1.4 insert)
- [1.5 erase](#1.5 erase)
- [1.6 replace](#1.6 replace)
- 二、字符操作
-
- [2.1 find](#2.1 find)
- [2.2 c_str](#2.2 c_str)
- [2.3 substr](#2.3 substr)
- [2.4 rfind](#2.4 rfind)
- [2.5 find_first_of](#2.5 find_first_of)
- [2.6 find_last_of](#2.6 find_last_of)
- 三、非成员函数
-
- [3.1 operator+](#3.1 operator+)
- [3.2 getline()](#3.2 getline())
- [3.3 流插入和流提取](#3.3 流插入和流提取)
- 文章结语
受篇幅限制,部分 string 相关接口放在了上一篇文章中,还没看过的朋友可以先点击下方链接快速回顾一下
https://blog.csdn.net/2401_89538720/article/details/159590175?fromshare=blogdetail&sharetype=blogdetail&sharerId=159590175&sharerefer=PC&sharesource=2401_89538720&sharefrom=from_link
一、Modifiers(修正)
1.1 push_back

cpp
string s1("hellow world");
s1.push_back('x');
cout << s1 << endl;
1.2 append

cpp
string s1("hellow world");
s1.append("xxx");
cout << s1 << endl;
1.3 +=(最常用的)
空间不够会自动扩容

cpp
string s1("hellow world");
s1 += 'x';
s1 += "jjjjj";
cout << s1 << endl;
1.4 insert

cpp
string s1("hellow world");
//插入一个字符
s1.insert(0, "p");
s1.insert(0, 1, 'x');
s1.insert(s1.begin(), 't');
cout << s1 << endl;
1.5 erase

cpp
string s1("hellow world");
//s1.erase(0, 1);
//s1.erase(s1.begin());
//s1.erase(--s1.end());
//s1.erase(s1.size()-1,1);
s1.erase(0);//默认缺省值是npos,相当于全删
cout << s1 << endl;
1.6 replace

伴随着增缩容
cpp
string s1("hellow world");
//s1.replace(6, 4,"%%");
s1.replace(6, 1,"%%");
cout << s1 << endl;
二、字符操作
2.1 find

修改空格(方法一)
cpp
string s1("hellow wo rd ha ha ");
size_t pos = s1.find(' ');
while (pos != string::npos)
{
s1.replace(pos, 1, "%%");
pos = s1.find(' ', pos + 2);
}
cout << s1 << endl;
追求效率(因为replace会反复移动数组,效率低下)
cpp
string s1("hellow wo rd ha ha ");
string s2;
for (auto ch : s1)
{
if (ch != ' ')
{
s2 += ch;
}
else
{
s2 += "%%";
}
}
s1.swap(s2);
cout << s2 << endl;
2.2 c_str

主要用来兼容C语言的,返回底层指针
cpp
cout << s1.c_str();
cpp
string s1;
cin >> s1;
FILE* pf = fopen(s1.c_str(), "r");
char ch = fgetc(pf);
while (ch != EOF)
{
cout << ch;
ch = fgetc(pf);
}
fclose(pf);
2.3 substr
会自己构造string对象

cpp
string s1("haha.cpp.zip");
size_t pos = s1.find(".");
string tmp =s1.substr(pos);
cout << tmp << endl;
2.4 rfind

cpp
string s1("haha.cpp.zip");
size_t pos = s1.rfind(".");
string tmp =s1.substr(pos);
cout << tmp << endl;
2.5 find_first_of

cpp
string s1("addefrecvrgei");
size_t pos = s1.find_first_of("aei");
while (pos != string::npos)
{
s1.replace(pos, 1, "*");
pos = s1.find_first_of("aei", pos + 1);
}
cout << s1 << endl;
2.6 find_last_of

cpp
void fuc(string s1)
{
size_t pos = s1.find_last_of("\\/");
cout <<"路径:" << s1.substr(0,pos) << endl;
cout <<"文件名:" << s1.substr(pos+1) << endl;
}
int main()
{
string s1("C:\\Users\\hlf13\\Documents\\Visual Studio 2022");
string s2("user/id/test.cpp");
fuc(s1);
fuc(s2);
}
三、非成员函数
3.1 operator+

cpp
string s1 ("haha");
cout << s1+"he";
cout << s1+'c';
cout << "abb"+s1;
3.2 getline()

cpp
int main()
{
string s1;
getline(cin,s1);
size_t pos =s1.rfind(' ');
string s2=s1.substr(pos+1);
cout<<s2.size()<<endl;
}
3.3 流插入和流提取

文章结语
感谢你读到这里~我是「键盘敲碎了雾霭」,愿这篇文字帮你敲开了技术里的小迷雾 💻
如果内容对你有一点点帮助,不妨给个暖心三连吧👇
👍 点赞 | ❤️ 收藏 | ⭐ 关注
(听说三连的小伙伴,代码一次编译过,bug绕着走~)
你的支持,就是我继续敲碎技术雾霭的最大动力 🚀
🐶 小彩蛋:
/^ ^\
/ 0 0 \
V\ Y /V
/ - \
/ |
V__) ||
摸一摸毛茸茸的小狗,赶走所有疲惫和bug~我们下篇见 ✨