string
str.size() 字符串的长度
auto 对string类型同样适用,不过是单个字符迭代
str.find()返回类型是int类型的坐标
str.substr(i,x)取字符串,取i坐标开始之后的x个字符,在下面的代码案例中,使用substr匹配大段字符串中的要求字符串。
string strr(str,i,x)从str的i位置开始,将其后面的x个字符作为strr的内容
如果i,x为空,那么为strr=str;
#include <bits/stdc++.h>
using namespace std;
int main(){
string str;
str="12345678";
for(auto i:str){
cout<<i<<" "; //1 2 3 4 5 6 7 8
}
cout<<endl;
int t=str.find("4");
cout<<t<<endl;
string strr(str,2,5);
for(auto i:strr){
cout<<i<<" "; //1 2 3 4 5 6 7 8
}
cout<<endl;
string str1;
str1="welcometochuanzhi cupchuanzhi";
int res=0;
for(int i=0;i<str1.size();i++)
{
if(str1.substr(i,8)=="chuanzhi")
res++;
}
cout<<res;//2
}
运行截图:
新知识:
1.substr()是取字符串一段字符串的函数,可以应用在匹配字符串的特定字符串中。原本看到这样的一个题是很懵逼的,然后满脑子暴力,但是有了这个substr,这种题目就可以迎刃而解了。每当这个时候我就感觉,复习一遍基础内容对我来说是多么的有必要。
2.字符串的拼接可以用appdend()和+
3.rbegin():逆向迭代,指向字符串的末尾 rend():指向字符串的开头位置
4.大小写转换 tolower toupper,配套transform(vec.begin(),vec.end(),vec.begin(),::tolower)