竞赛中stl库string常用函数相关使用和记忆方法
一、前言
对于笔者来说,相关stl上的几个处理string的库函数,老是记不清,有时候就算想起了,也用不好!!???(我表示很服气😅)。然后一气之下有了这篇文章,总结一下这个sha鸟库函数。
二、分类
进行分类,这样才可以更好记忆。分类的途中惊讶的发现,常用的还是那几个增删改查 ,再加求长以及反转
三、结论篇
1、增(插入)
对于博主来说怎么简单怎么来、总共就两个一个 + ,另一个 .insert()
- + or +=
这个是个好东西,这个表示两个字符串相加。真的好理解,反正我是的😋
#include<iostream>
using namespace std;
string a = "a", b = "b";
int main(){
cout << a + b << endl;//结果:ab
a += b;
cout << a << endl;//结果:ab
}
-
.insert()
反正作者老是搞不清他的参数是什么鬼,代表什么意思,老是用不好🙃
-
插入字符串
如果你和笨笨的笔者一样得话,就只记忆最基础得吧,当然用得最多
// string& insert (size_t pos, const string& str); //两个参数 size_t 就是 整形int const string& 就是 字符串string #include<iostream> using namespace std; string a = "a12", b = "b34"; int main(){ cout << a.insert(0, b);//结果:b34a12 return 0; }
反正简而言之:在想插位置(参数pos)上插入字符串,一个整形,一个字符串参数
-
插入字符
用得较少,当然也有用的时候,能记得就记吧😭
//string& insert (size_t pos, size_t n, char c); //参数 size_t 整形 char 字符型 #include<iostream> using namespace std; string a = "a12"; char t = 'c'; int main(){ cout << a.insert(0, 2, t);//结果:cca12 return 0; }
简而言之:在想插得位置(参数pos)上插入几个(参数n)字符
-
2、删
删除没那么复杂,就只有一个库函数==.erase==
-
.erase
//string& erase (size_t pos = 0, size_t len = npos); //参数 size_t 整形int #include<iostream> using namespace std; string a = "a12"; int main(){ cout << a.erase(0, 1);//结果:12 return 0; }
简而言之:在想删除得位置(参数pos)上删除几个(参数len)字符
-
清空字符串(选一种好理解得即可,三选一)
一般直接赋值为空串,不用函数
#include<iostream> using namespace std; string a = "a12"; int main(){ cout << a.erase(0, a.size()) << endl;//结果:null(这里笔者得意思是空字符串)(1、标准方式) a = "a12"; cout << a.erase();//结果:null(这里笔者得意思是空字符串)(2、无参方式) a = "a12"; a = "" cout << a << endl; ;//结果:null(这里笔者得意思是空字符串)(3、赋值得方式,笔者觉得容易理解) return 0; }
3、改(截取子串、替换、反转)
这应该是最最常用,和库函数得精华所在了吧。会用了,就可以通关啦!😆
1. 截取子串==.substr()==
好消息,只有一种使用方法。
//string substr (size_t pos = 0, size_t len = npos) const;
//参数: size_t 整形int
#include<iostream>
using namespace std;
string a = "a12";
int main(){
cout << a.substr(0, 2) << endl;//结果:a1
return 0;
}
简而言之:在某位置(参数pos)上,截取一定长度(参数len)
2.替换==.replace==
-
用字符串替换
用得最多,最基础得用法
//string& replace (size_t pos, size_t len, const string& str); //参数 size_t 就是 整形int const string& 就是 字符串string #include<iostream> using namespace std; string a = "a12", b = "b34"; int main(){ cout << a.replace(0, 2, b) << endl; //结果b342 return 0; }
反正简而言之:用字符串==(参数str)==替换主串位置 [pos, pos + len)(pos和len是参数)的字符
-
用字符替换
用得较少,当然也有用的时候,能记得就记吧😭(作者是懒狗直接黏贴了😘)
//string& replace (size_t pos, size_t len, size_t n, char c); //参数 size_t 整形 char 字符型 #include<iostream> using namespace std; string a = "a12"; char ch = 'c'; int main(){ cout << a.replace(0, 2, 3, ch) << endl;//结果:ccc2 return 0; }
简而言之:用一定数量==(参数n)的字符(参数c)==替换主串位置 [pos, pos + len)(pos和len是参数)的字符
3.反转reverse()
不是stl的库函数,为什么拿出来呢,因为经常用。为了不混淆
#include<iostream>
#include<algorithm>//reverse()的头文件
using namespace std;
string a = "a12";
int main(){
reverse(a.begin(), a.end());//用的地址
cout << a << endl;//结果:21a
return 0;
}
简而言之:不要忘记头文件!!!!
4.查
最后一个了,大家加油!
-
查找字串在目标字符串的位置
用得最多,最基础得用法
//size_t find (const string& str, size_t pos = 0) const; //参数 size_t 就是 整形int const string& 就是 字符串string #include<iostream> #include<algorithm> using namespace std; string a = "a12", b = "a"; int main(){ cout << a.find(b, 0) << endl;//结果:0 return 0; }
反正简而言之:从pos(参数)位置开始,查找字符串中是否有该字串(参数str)
-
查找字符在目标字符串的位置
用得较少,当然也有用的时候,能记得就记吧😭(作者是懒狗直接黏贴了😘)
#include<iostream> #include<algorithm> using namespace std; string a = "a12"; char ch = 'a'; int main(){ cout << a.find(ch) << endl;//结果:0 return 0; }
简而言之:从**pos(参数)**位置开始,查找字符串中是否有该字串(参数str)
四、总结:
//参数
/*
size_t 就是 整形int
string& 就是 字符串string
*/
//插入
string& insert (size_t pos, const string& str);//在pos位置插入str
string& insert (size_t pos, size_t n, char c);//在pos位置插入n个字符c
//删除
tring& erase (size_t pos = 0, size_t len = npos);//从pos位置开始删除len个字符
//字串截取
string substr (size_t pos = 0, size_t len = npos) const;//从pos位置开始截取len个字符
//子串替换
string& replace (size_t pos, size_t len, const string& str);//从pos位置开始后的len个字符,替换为str
string& replace (size_t pos, size_t len, size_t n, char c);//从pos位置开始后的len个字符,替换为n个字符c
//字符串反转
#include<algorithm>//头文件
reverse (iterator.begin(), iterator.end());//容器从开始到结尾进行反转
//查找
size_t find (const string& str, size_t pos = 0) const;//从pos位置开始后,开始查找字符串str,返回下标
size_t find (char c, size_t pos = 0) const;//从pos位置开始后,开始查找字符c,返回下标
如果对你有帮助,点点赞吧!🤗🤗🤗