文章目录
字符串
在c++中,构造一个string的方法就是直接在string后面写变量名。
string str;
注意string在头文件里。
如果要初始化,可以直接给string赋值
string str="abcd";
operator+=
在string中可以通过加法,将两个string拼在一起。
#include<iostream>
#include<cstring>
using namespace std;
int main(){
string s1="ABC",s2="DEF",s3;
s3=s1+s2;
s1+=s2;
cout<<s1<<endl;
cout<<s3<<endl;
return 0;
}
输出结果
ABCDEF
ABCDEF
compare operator(比大小)
两个string可以直接使用==、!=、>=、<=……直接比较大小,比较规则时字典序。
#include<iostream>
#include<cstring>
using namespace std;
int main(){
string s1="ABC",s2="DEF";
string s3="AAA",s4="AAA";
if(s3==s4) cout<<"OK1"<<endl;
if(s1<s2) cout<<"OK2"<<endl;
if(s3!=s2) cout<<"OK3"<<endl;
return 0;
}
输出结果
OK1
OK2
OK3
长度
length()和size()均可返回string的长度,即存放的字符数量,两者基本相同。
#include<iostream>
#include<cstring>
using namespace std;
int main(){
string s1="ABCDEF";
printf("%d %d\n",s1.length(),s1.size());
return 0;
}
插入
string里的insert()有多种写法,这里我介绍几个常用的写法。
insert(post.string)
在pos号位置插入字符串string
#include<iostream>
#include<cstring>
using namespace std;
int main(){
string s1="ABCDEF";
string s2="GHIJKL";
s1.insert(3,s2);
cout<<s1;
return 0;
}
insert(it,it2,it3)
it为原字符串的欲插入位置,it2和it3是待插入字符串的首尾迭代器,用来表示串[it2,it3)将被插在it的位置上。
#include<iostream>
#include<cstring>
using namespace std;
int main(){
string s1="ABCDEF";
string s2="GHIJKL";
s1.insert(s1.begin()+3,s2.begin(),s2.end());
cout<<s1;
return 0;
}
输出结果
ABCGHIJKLDEF
删除元素方法
erase()有两种用法,删除单个元素,删除一个区间内的所有元素
。
删除单个元素
s.erase(it)用于删除单个元素,it为要删除的元素的迭代器。
#include<iostream>
#include<cstring>
using namespace std;
int main(){
string s1="ABCDEF";
s1.erase(s1.begin()+2);
cout<<s1;
return 0;
}
输出结果
ABDEF
删除多个元素
s.erase(pos,length),其中pos为需要开始删除的起始位置,length为需要删除的字符个数。
#include<iostream>
#include<cstring>
using namespace std;
int main(){
string s1="ABCDEF";
s1.erase(3,2);
cout<<s1;
return 0;
}
输出结果
ABCF
清空数据
#include<iostream>
#include<cstring>
using namespace std;
int main(){
string s1="ABCDEF";
s1.clear();
cout<<s1.size();
return 0;
}
输出结果
0
返回元素
substr(pos,len)返回从pos号位开始,长度为len的子串。
#include<iostream>
#include<cstring>
using namespace std;
int main(){
string s1="Happy birthday to you";
cout<<s1.substr(0,5)<<endl;
cout<<s1.substr(15,2)<<endl;
cout<<s1.substr(18,3)<<endl;
return 0;
}
输出结果
Happy
to
you
string::npos
string::npos是个常数,本身值为-1,但是unsigned_int类型最大值,string::npos用以作为find函数失配时的返回值。通常情况下string::npos等于-1。
#include<iostream>
#include<cstring>
using namespace std;
int main(){
if(string::npos==-1)
cout<<-1<<" is true.";
return 0;
}
输出结果
-1 is true.
查找方法
str.find(str2),当str2为str的子串时,返回str中第一次出现的位置,如果str2不是str的子串,那么返回string::npos。
#include<iostream>
#include<cstring>
using namespace std;
int main(){
string s1="Happy birthday to you.";
string s2="you";
string s3="me";
if(s1.find(s2)!=string::npos)
cout<<s1.find(s2)<<endl;
if(s1.find(s3)!=string::npos)
cout<<s1.find(s3)<<endl;
else
cout<<"We can not find it."<<endl;
return 0;
}
输出结果
18
We can not find it.
更改元素
str.replace(pos,len,str2),把str从第pos号位开始,长度为len的子串替换为str2。
str.replace(it1,it2,str2)把str的迭代器[it1,it2)范围的子串替换为str2。
#include<iostream>
#include<cstring>
using namespace std;
int main(){
string s1="Happy birthday to you.";
string s2="me";
cout<<s1.replace(18,3,s2)<<endl;
return 0;
}
输出结果
Happy birthday to me.
获取子串
substr(pos,len)表示从字符串的第pos位取长为len的子串
#include<iostream>
#include<cstring>
using namespace std;
int main(){
string s1="Happy birthday to you.";
cout<<s1.substr(0,5)<<endl;
return 0;
}