一、string类补充
1.函数示例:
(1)find和rfind函数,返回的是下标或者string::npos
index=ss.find(s1,pos,num)
find从pos(包括)开始往右查找(num的作用待补充)
index=ss.rfind(s1,poss,num)
rfind从pos(包括)开始往左查找(num的作用待补充)
代码示例:
//>>>>Qiansui #include<iostream> #include<algorithm> #include<cmath> #define ll long long using namespace std; const int maxm=1e4+5; string ss[3]{"123asd123","a123gh123","ea123s123"},s1="123"; int main(){ int index; cout<<"find\n"; for(int i=0;i<3;++i){ index=ss[i].find(s1); if(index!=string::npos) cout<<index<<endl; else cout<<"Error\n"; } cout<<"rfind\n"; for(int i=0;i<3;++i){ index=ss[i].rfind(s1,6); if(index!=string::npos) cout<<index<<endl; else cout<<"Error\n"; } cout<<string::npos; return 0; }
运行结果:
(2)遍历
代码示例:
#include<iostream> #include <string> using namespace std; int main(){ string s1{"world"}; for(int i=0;i<s1.size(); ++i) cout<<s1[i]<<endl; for(auto c : s1) cout<<c<<endl; return 0; }
运行结果:
标签:string,示例,STL,s1,C++,int,num,摘记,include From: https://www.cnblogs.com/Qiansui/p/17043125.html