跳到正文

个人记录

[笔记][STL]string

monoid_me约 8 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 8 分钟

版本

  1. v2当前
  2. v1

string

string 这个stl,相信大家对它的简介不陌生

string:字符串,可以完美代替char []

string 类中有很多字符串操作函数

string 是动态的,不用像 char [] 一样指定大小

string 和 cstring :cstring 指用双引号指定的东西,是个常量,不能相加,所以 string a="1"+"2"; 是错误的

string 初级

定义

C++
string a;
a="qwq";

string b="qwq";

基础输入,输出

C++
cin>>a;
cout<<a;

访问

C++
cout<<a[2];

下标注意事项

C++
string s="123";
cout<<s[2];     //输出3
s[2]='4';       //可以更改
cout<<s;        //输出124
s[3]='5';       //错误,更改了比 size()-1 大的位置
cout<<s;        //更改失败,输出124

string 中级

操作符

=,==,+,+==,==,+,+=是string追加字符串的方法

· str=str+a;str=str+a; 会先将等号右边的两个string对象内容相加,得到一个新的string对象,再把这个新的对象赋给等号左边的string对象。

· str+=a;str+=a; 直接将等号右边的string对象内容追加到左边的string对象后面。

所以,+=比+快,小

长度

C++
string a,b;
int lena=a.length(); ;
int lenb=b.size();

比较

≤\leq用于一个字符一个字符比较,如234>12345(T),234<2345(T)

高精度数比较模板:

C++
bool cmp(string s1,string s2){//若s1<s2,返回1
	if(s1.size()!=s2.size()) return s1.size()<s2.size();
	return s1<s2;
}

string 高级

输入整行

C++
getline(cin,name)//以\n结束
getline(cin,name,'#')//以'#'结束

判断子串

C++
if(a.find(b) == string::npos)
	cout << "not found\n";//b是a的子串
else
	cout <<"found\n";//b不是a的子串

string 逆天

查找

C++
#include<iostream>
using namespace std;
int main()
{
    string str="abcdefab";
    cout<<str.find_first_of('a')<<endl;//第二个参数为0,默认从下标为0开始查找。
    cout<<str.find_first_of("hce")<<endl;//待查串hce第一个出现在原串str中的字符是c,返回str中c的下标2,故结果为2。
    cout<<str.find_first_of("ab",1)<<endl;//从下标为1开始查,待查串ab第一个出现在原串str中的字符是b,返回b的下标,结果为1。
    cout<<str.find_first_of('h')<<endl;//原串没有待查字符h,故查不到,返回npos。
    cout<<str.find_first_of("hw")<<endl;//待查子串任一字符在原串中都找不到,故查不到,返回npos。
    cout<<str.find_last_of("wab")<<endl;//原串最后一个字符首先与待查子串的每一个字符一一比较,一旦有相同的就输出原串该字符的下标.。结果为b的下标7。
    cout<<str.find_last_of("wab",5)<<endl;
    //从原串中下标为5开始逆向查找,首先f与待查子串每一字符比较,若有相同的就输出该字符在原串的下标。
    //若一个都没有,就依次逆向比较,即e再与待查子串一一比较,直到原串的b与待查子串中的b相同,然后输出该b在原串的下标1。
    cout<<str.find_last_of("fab",5)<<endl;//输出f在原串的下标5。
    cout<<str.find_last_of("fab",7)<<endl;//输出b在原串的下标7。
    cout<<str.find_last_of("hwk")<<endl;//原串没有待查子串的任何字符,故返回npos。
    return 0;
}
//有效的下标应该在0~len-1范围内。len=str.size();
//如果找不到,则返回string::nops
//比如:
#if 0
string s="123";
cout<<(s.find_first_of('4')==string::npos);
//输出1
#endif

输出结果

还有find_first_not_of

C++
#include<iostream>
using namespace std;
int main()
{
    string str="abcdefab";
    cout<<str.find_first_not_of('h')<<endl;//第二个参数为0,默认从原串下标为0开始查找。第一个a就和带查字符不同,故输出a的下标0。
    cout<<str.find_first_not_of("twj",1)<<endl;//从下标为1开始查,第一个b就和待查子串任一字符不同,故输出b的下标1。
    cout<<str.find_first_not_of("wbj",1)<<endl;//从下标为1开始,第一个b与待查子串中的b一样,故b不符合,继续c再和子串任一字符比较,结果都不同,故输出c的下标2。
    cout<<str.find_first_not_of("abcdefg")<<endl;//原串任一字符都出现在子串中,故找不到,返回npos。
    cout<<str.find_last_not_of('h')<<endl;//第二个参数为0,默认从npos逆向查。原串倒数第一个字符b和h就不同,故输出b的小标7。
    cout<<str.find_last_not_of("ab",6)<<endl;//从原串下标为6开始逆向查询,第一个a和待查串中的字符a相同,不符合,故继续逆向比较。f和待查子串任一字符不同,故输出f的下标5。
    cout<<str.find_last_not_of("abcdefhu")<<endl;//原串任一字符都出现在子串中,故找不到,返回npos。
    return 0;
}

//有效的下标应该在0~len-1范围内。len=str.size();

1

字符串操作

插入 insert

C++
string str="frontbehind";
string s="插入部分";
str.insert(5,s);//第五个字符后插入
cout<<str<<endl;

结果:front插入部分behind

删除 erase

C++
string str1="frontbehind";
str1.erase(5);//第五个字符后全部删除
cout<<str1<<endl;

string str2="frontmiddlebehind";
str2.erase(5,6);//第五个字符后删除6个字符
cout<<str2<<endl;

结果:

代码
front
frontbehind

截取 substr

C++
string str1="frontbehind";
cout<<str1.substr(5)<<endl;//第五个字符后截取所有字符

string str2="frontmiddlebehind";
cout<<str2.substr(5,6)<<endl;//第五个字符后截取6个字符

结果:

代码
behind
middle
原文