string的各种函数(系统学习)
1、按照⾯向对象的要求,可以把字符串看作⼀个对象,设计⼀个串类加以描述。但是,在⼀般的情况下不必建⽴⾃⼰的串类,c++标准
在库<string>中给出了类string,提供了丰富的串操作,程序员使⽤指令: #include<string>即存取这个类。可将类string视为⼀个容器类,其部分操作如下:
构造函数:
string s:构造⼀个空串s
string s(a):构造串s,并⽤字符数组a初始化
string s(a,n):构造串s,⽤字符数组a的前n个字符初始化
string s(str):⽤串str构造串s(拷贝初始化)
看下⾯代码验证
#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
const int maxn=1e5+10;
const int maxk=3e5+10;
const int maxx=1e4+10;
const ll maxe=1000+10;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int main()
{
char a[maxn];
cin>>a;//输⼊abcdefgh
string s(a);//结果为abcdefgh
string s1(a,5);//结果为abcde
string s2(s1);//结果为abcde
cout<<s<<endl;
cout<<s1<<endl;
cout<<s2<<endl;
return0;
}
输⼊输出:
getline(cin,s,delim):从cin中提取字符存⼊串s中,当遇到delim或者提取的字符个数等于s的长度时结束,如果⾃⼰不设定,默认为'\n' getline(cin,s):遇到'\n'结束
cin>>s:遇到空⽩字符终⽌提取,空⽩字符不被读取
cout<<s:输出s
#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
const int maxn=1e5+10;
const int maxk=3e5+10;
const int maxx=1e4+10;
字符串函数注册登录
const ll maxe=1000+10;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int main()
{
string s;
while(getline(cin,s,'#'))
{
getchar();//⾃⼰指定了字符要加getchar,不然结果会多出⼀个换⾏,但是默认情况下不⽤加getchar
cout<<s<<endl;
}
return0;
}
操作符:
s=val:赋值操作,val可以是⼀个串,⼀个字符或⼀个字符数组
s+=val:将val添加到串s之后
s[pos]:下标操作
s+t,t+s:s和t的连接,返回连接的结果。 t可以是⼀个串,⼀个字符或者⼀个字符数组
t<s,s<t,s<=t:由关系操作符确定结果是true还是false
t==s,s==t,s!+t······
#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
const int maxn=1e5+10;
const int maxk=3e5+10;
const int maxx=1e4+10;
const ll maxe=1000+10;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int main()
{
string s1,s2;
char a;
for(int i=0;i<5;i++)
{
cin>>a;
s1+=a;
}
cout<<s1<<endl;//输⼊为abcde,结果为abcde
cin>>s2;//输⼊fg
cout<<s2<<endl;//输出fg
s1+=s2;
cout<<s1<<endl;//输出abcdefg
if(s1<s2) cout<<"yes"<<endl;
else cout<<"no"<<endl;
return0;
}
添加:
s.append(str):将str添加在s之后,返回s
s.append(str,pos,n):将str中从位置pos开始的n个字符添加在s之后,如果n太⼤,则取str中从位置pos到串结尾的字符,返回s s.append(a):将字符数组添加在s之后,返回s
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
const int maxn=1e5+10;
const int maxk=3e5+10;
const int maxx=1e4+10;
const ll maxe=1000+10;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int main()
{
string s1,s2;
cin>>s1>>s2;//输⼊abcde fg
cout<<s1<<""<<s2<<endl;//输出abcde fg
s1.append(s2);
cout<<s1<<endl;//输出abcdefg
s2.append(s1,1,3);//输出结果为fgbcd
cout<<s2<<endl;
return0;
}
赋值:
s.assign(str):⽤str给s赋值,返回s
s.assign(str,pos,n):将str中从位置pos开始的n个字符组成的字串赋给s s.assign(a):将字符数组赋给s
#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
const int maxn=1e5+10;
const int maxk=3e5+10;
const int maxx=1e4+10;
const ll maxe=1000+10;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int main()
{
string s1,s2;
cin>>s1;//输⼊abcde
s2.assign(s1);
cout<<s2<<endl;//输出结果为abcde
s2.assign(s1,0,3);
cout<<s2<<endl;//输出结果为abc
return0;
}
长度:
s.size():返回s的长度,不能⽤于统计C风格的以'\0'结尾的字符串长度s.length():返回s的长度,不能⽤于统计C风格的以'\0'结尾的字符串长度
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
const int maxn=1e5+10;
const int maxk=3e5+10;
const int maxx=1e4+10;
const ll maxe=1000+10;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int main()
{
string s1,s2;
cin>>s1;//输⼊abcde
cout<<s1.length()<<endl;
cout<<s1.size()<<endl;
return0;
}
⽐较:
spare(str):根据s⼩于,等于,⼤于,返回⼀个负值,0,正值
spare(a):像上⾯的⼀样,只是这⾥是⼀个字符数组a⽽已
spare(pos,n,str):s中从位置pos开始的n个字符组成的⼦串与str⽐较
#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
const int maxn=1e5+10;
const int maxk=3e5+10;
const int maxx=1e4+10;
const ll maxe=1000+10;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int main()
{
string s1,s2;
cin>>s1>>s2;
cout<<s1pare(s2)<<endl;
cout<<s1pare(2,1,s2)<<endl;
return0;
}
插⼊:
s.insert(pos,str):在s的位置pos处插⼊str
s.insert(pos1,str,pos2,n):将str中从位置pos2开始的n个字符插⼊s中的位置pos1处s.insert(pos,a,n):将a 的前n个字符插⼊s中位置pos处
#include<iostream>
#include<string.h>
#include<map>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
const int maxn=1e5+10;
const int maxk=3e5+10;
const int maxx=1e4+10;
const ll maxe=1000+10;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int main()
{
string s1,s2;
cin>>s1>>s2;//输⼊abcde
s1.insert(0,s2);
cout<<s1<<endl;
s1.insert(0,s2,1,1);//位置都是从0开始的
cout<<s1<<endl;
return0;
}
替换:
#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
const int maxn=1e5+10;
const int maxk=3e5+10;
const int maxx=1e4+10;
const ll maxe=1000+10;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int main()
{
string s1,s2;
cin>>s1>>s2;//输⼊abcde fg
cout<<s1<<endl;
cout<<s1<<endl;//结果为fgde
return0;
}
取⼦串:
s.substr(pos,n):返回从pos开始的n个字符组成的⼦串,如果n太⼤或者省略,则返回从pos开始直到s结尾的左右字符组成的⼦串,pos的默认值为0,会在末尾⾃动加'\0'
c语⾔的strncpy函数不会在末尾帮你加'\0',切记要⾃⼰加
#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。