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+;
const int maxn=1e5+;
const int maxk=3e5+;
const int maxx=1e4+;
const ll maxe=+;
#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,);//结果为abcde
string s2(s1);//结果为abcde
cout<<s<<endl;
cout<<s1<<endl;
cout<<s2<<endl;
return ;
}

输入输出:

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+;
const int maxn=1e5+;
const int maxk=3e5+;
const int maxx=1e4+;
const ll maxe=+;
#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;
}
return ;
}

操作符:

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+;
const int maxn=1e5+;
const int maxk=3e5+;
const int maxx=1e4+;
const ll maxe=+;
#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=;i<;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;
return ;
}

添加:

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<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+;
const int maxn=1e5+;
const int maxk=3e5+;
const int maxx=1e4+;
const ll maxe=+;
#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,,);//输出结果为fgbcd
cout<<s2<<endl;
return ;
}

赋值:

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+;
const int maxn=1e5+;
const int maxk=3e5+;
const int maxx=1e4+;
const ll maxe=+;
#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,,);
cout<<s2<<endl;//输出结果为abc
return ;
}

长度:

s.size():返回s的长度,不能用于统计C风格的以'\0'结尾的字符串长度

s.length():返回s的长度,不能用于统计C风格的以'\0'结尾的字符串长度

#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+;
const int maxn=1e5+;
const int maxk=3e5+;
const int maxx=1e4+;
const ll maxe=+;
#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;
return ;
}

比较:
s.compare(str):根据s小于,等于,大于,返回一个负值,0,正值

s.compare(a):像上面的一样,只是这里是一个字符数组a而已

s.compare(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+;
const int maxn=1e5+;
const int maxk=3e5+;
const int maxx=1e4+;
const ll maxe=+;
#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<<s1.compare(s2)<<endl;
cout<<s1.compare(,,s2)<<endl;
return ;
}

插入:

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<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+;
const int maxn=1e5+;
const int maxk=3e5+;
const int maxx=1e4+;
const ll maxe=+;
#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(,s2);
cout<<s1<<endl;
s1.insert(,s2,,);//位置都是从0开始的
cout<<s1<<endl;
return ;
}

替换:

s.replace(pos1,n1,str):用str替换s中从位置pos1开始的n1个字符组成的子串,返回s

s.replace(pos1,n1,str,pos2,n2):用str中从位置pos2开始的n2个字符替换s中从位置pos1开始的n1个字符,返回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+;
const int maxn=1e5+;
const int maxk=3e5+;
const int maxx=1e4+;
const ll maxe=+;
#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
s1.replace(,,s2);//结果为afgde
cout<<s1<<endl;
s1.replace(,,s2,,);
cout<<s1<<endl;//结果为fgde
return ;
}

取子串:

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>
#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+;
const int maxn=1e5+;
const int maxk=3e5+;
const int maxx=1e4+;
const ll maxe=+;
#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.substr(,)<<endl;//结果为bcd
return ;
}

是否为空串:

s.empty:为空,返回true,不为空,返回false

查找:

s.find(str,pos):在串s中从位置pos开始查找串str,若查找成功,返回str首次出现的位置,若查找不成功,返回s.npos,pos的默认值为0

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
string s="abc";
if(s.find('d')!=s.npos) cout<<""<<endl;
else cout<<""<<endl;
return ;
}
//输出为0

s.find(a,pos):同上

s.rfind(str,pos):在s的前(pos+str.size())个字符中查找str,若查找成功,返回str最后一次出现的位置,若不成功,返回-1,若pos省略,则在s中查找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+;
const int maxn=1e5+;
const int maxk=3e5+;
const int maxx=1e4+;
const ll maxe=+;
#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 bc cout<<s1.find(s2,);
return ;
}
#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+;
const int maxn=1e5+;
const int maxk=3e5+;
const int maxx=1e4+;
const ll maxe=+;
#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;//输入abcdefgabcdefg fg
cout<<s1.rfind(s2)<<endl;//
cout<<s1.rfind(s2,)<<endl;//
return ;
}

reverce()函数:将字符串翻转,s.reverce(s.begin(),s.end())

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std; #include <iostream>
#include <string>
#include <algorithm>
using namespace std; int main()
{
string s = "hello"; reverse(s.begin(),s.end()); cout<<s<<endl; return ;
}

C++的string类提供下列3个成员函数将一个string类的对象s转换成C风格的字符串:

s.c_str():将存储在s中的字符作为字符数组返回,并添加一个空字符('\0')作为结束符

s.data():将存储在s中的字符作为字符数组返回,最后不添加('\0')

s.copy(a,n,pos):用s中从位置pos开始的n个字符替换a的前n个字符(如果n太大,则用从pos开始的n个字符直到s的末尾的所有字符),pos默认为0,返回用于替换的字符个数

最新文章

  1. 浅谈 jQuery 核心架构设计
  2. SpringMVC@RequestBody小细节
  3. &lt;fmt:formatDate&gt;标签的输出格式:
  4. AEAI WM V1.0 工作管理系统开源发版
  5. Android Studio 第一次新建Android Gradle项目超级慢的解决方案
  6. ORACLE R12 MOAC
  7. C#调用VC dll输出参数
  8. AngularJs 【使用】 -- ng-repart 排序使用
  9. DLog 技巧
  10. C语言入门(14)——结构体
  11. Java基础语法(上篇)
  12. [原创]Oracle 12c的备份和恢复策略
  13. Windows PE入门基础知识:Windows PE的作用、命名规则、启动方式、启动原理
  14. Python基础:语法基础(3)
  15. 高通 NXP NFC(PN547PN548) 移植流程 android6.0
  16. tomcat 启动慢问题
  17. Java并发编程(十四)-- 线程池实现原理
  18. 执行效率做比较,Go、python、java、c#、delphi、易语言等
  19. Bugku-CTF之flag.php(点了login咋没反应)
  20. AsyncTask中各个函数详细的调用过程,初步实现异步任务

热门文章

  1. 【转】 Pro Android学习笔记(五十):ActionBar(3):搜索条
  2. java对象在内存中的结构(HotSpot虚拟机)
  3. popup的简单应用举例
  4. VIsual Studio 2010 常用快捷键
  5. 面试题: redis面试题 有用 redis详细
  6. 树莓派 Learning 003 --- GPIO 000 --- GPIO引脚图
  7. R语言简单作图
  8. [转]hadoop运行mapreduce作业无法连接0.0.0.0/0.0.0.0:10020
  9. iObjects for java +weblogic
  10. MVC5手工添加System.Web.Optimization