字符串中的函数在写题中主要使用的有
string的排序函数sort(s.begin(),s.send())
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
void test()
{
string s="cdefba";
sort(s.begin(),s.end());
cout<<"s:"<<endl;
}
用于查找find函数
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
cin>>str;
//主要字符串是从0开始计数的
cout<<"ab在str中的位置:"<<str.find("ab")<<endl;
//返回第一次出现ab的位置,没有则返回一串乱码
cout<<"ab在str[2]到str[n-1]中的位置:"<<str.find("ab",2)<<endl;
//返回第一次从str[2]开始出现ab的位置,没有则返回一串乱码
cout<<"ab在str[0]到str[2]中的位置:"<<str.rfind("ab",2)<<endl;
//返回ab在str[0]到str[2]中的位置,没有则返回一串乱码
return 0;
}
子串substr()函数
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
cin>>str;
cout<<"返回str[3]及其以后的子串:"<<str.substr(3)<<endl;
//若小于限制长度则报错
cout<<"从ste[2]开始由四个字符组成的子串:"<<str.substr(2,4)<<endl;
//若小于限制长度则报错
return 0;
}
————————————————
版权声明:本文为CSDN博主「Gowi_fly」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_43309286/article/details/93191767
str.erase()函数 str.erase(first,last)
erase()函数在str中删除指定长度的字符串first为开始的位子,last为删除的字符个数。