string的概念及特性:
string是STL的字符串类型,通常用来表示字符串。在c语言中,我们通常用char*表示字符串
特性:
- char* 是一个指针,string是一个
类
(string封装了char*,管理这个字符串,是一个char*型的容器) - string封装了很多实用的成员方法
(查找find、拷贝copy、删除delete、替换replace、插入insert) - 不用考虑内存释放和越界
头文件
#include< iostream>
#include< string >
using namespace std;
初始化
string s; //构造一个空字符串s
s=s+"abcdefg";
string s1="abcd"; //将abcd赋值给s1
string s2(s1); //用s1初始化s2
string s3(3,'a'); //s3初始化为aaa
string s4("abcdef",3); //s4初始化为abc
string s5(s4,1,2); //用s4下标为3~5的字符串为s5初始化
代码实现一下:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s; //构造一个空字符串s
s=s+"abcdefg"; //此时s为abcdefg
string s1="abcd"; //将abcd赋值给s1
string s2(s1); //用s1初始化s2
string s3(3,'a'); //s3初始化为aaa
string s4("abcdef",3); //s4初始化为abc
string s5(s4,1,2); //用s4下标为3~5的字符串为s5初始化
cout <<"s:"<< s <<endl;
cout << "s1:"<< s1 <<endl;
cout << "s2:"<< s2 <<endl;
cout << "s3:"<< s3 <<endl;
cout << "s4:"<< s4 <<endl;
cout << "s5:"<< s5 <<endl;
return 0;
}
运行结果:
基本函数的使用
①~⑰
①length() 输出字符串长度
string s1="abcdef";
cout << s1.length() <<endl; //输出结果为6
②size() 输出字符的个数
string s1="abcdef";
cout << s1.size() <<endl; //输出结果为6
③capacity() 计算字符串容量大小
string s1="abcdef";
cout << s1.capacity() <<endl; //容量为6
④resize() 重新设置字符个数
s1.resize(4); //容量不变,个数,长度均改变为4
⑤reserve() 修改容量
string s1="abcdef";
s1.reserve(20); //只能变大,不能变小,容量从6变为20
对①~⑤个函数代码实现一下:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1="abcdef";
cout << s1 <<endl;
cout << s1.length() <<endl;
cout << s1.size() <<endl;
cout << s1.capacity() <<endl;
s1.resize(4);
cout <<"修改个数后:" <<endl;
cout << s1 <<endl;
cout << s1.length() <<endl;
cout << s1.size() <<endl;
cout << s1.capacity() <<endl;
s1.reserve(20);
cout <<"修改容量后:" <<endl;
cout << s1 <<endl;
cout << s1.length() <<endl;
cout << s1.size() <<endl;
cout << s1.capacity() <<endl;
return 0;
}
运行结果:
⑥输出( [ ]下标输出和at()函数输出)
string s1="abcdef";
cout << s1[0] <<' '<< s1[2] << endl; //区别:[]输出越界情况下会崩溃。
cout << s1.at(0) <<' '<< s1.at(2) << endl;
//at()函数输出遇到越界时可以抛出异常,使程序顺利进行
对⑥函数进行代码实现一下:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1="abcdef";
cout << s1[0] <<' '<< s1[2] << endl;
cout << s1.at(0) <<' '<< s1.at(2) << endl;
return 0;
}
运行结果:
⑦修改指定元素
string s1="abcdef";
s1[0]='m'; //将下标为0的字符改为m
s1.at(1)='f'; // 将下标为1的字符改为f
⑧insert() 插入
string s1="abcdef";
string s2="ccc"; //insert()是在对应下标之前插入
s1.insert(1,"123"); //s1为a123bcdef
s1.insert(1,s2); //s1为accc123bcdef
⑨append() 在字符串的末尾添加
string s1="abcdef";
string s2="xyz";
s1.append(s2); //将s2插入到s1末尾
s1.append("hello"); //将hello插入到s1末尾
s1.append(2,'x'); //将2个x插入到s1的末尾
s1.append(s2,1,2); //将s2的从下标为1开始的两个字符插入到s1末尾
对⑦~⑨个函数代码实现一下:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1="abcdef";
s1[0]='m'; //将下标为0的字符改为m
s1.at(1)='f'; // 将下标为1的字符改为f
cout << s1 <<endl;
s1="abcdef";
string s2="ccc"; //insert()是在对应下标之前插入
s1.insert(1,"123"); //s1为a123bcdef
cout << s1 <<endl;
s1="abcdef";
s1.insert(1,s2); //s1为accc123bcdef
cout << s1 <<endl;
s1="abcdef";
s1.append(s2); //将s2插入到s1末尾
cout << s1 <<endl;
s1="abcdef";
s1.append("hello"); //将hello插入到s1末尾
cout << s1 <<endl;
s1="abcdef";
s1.append(2,'x'); //将2个x插入到s1的末尾
cout << s1 <<endl;
s1="abcdef";
s1.append(s2,1,2); //将s2的从下标为1开始的两个字符插入到s1末尾
cout << s1 <<endl;
return 0;
}
运行结果:
⑩empty() 判断是否为空
string s;
string s1="abcdef";
string s2="abcd";
cout << s.empty() <<endl; //返回1
cout << s1.empty() <<endl; //返回0
⑪compare() 比较两个字符串大小
string s1="abcdef";
string s2="abcd";
cout << s1.compare(s2) << endl;
//若s1>s2,返回值>0;若s1<s2,返回值<0;s1=s2,返回值=0;
⑫copy() 将内容复制为一个字符数组
string s1="abcdef";
char xx[10]={0};
s1.copy(xx,3,2); //将s1中从下标为2开始的3个字符复制给数组xx
cout << xx <<endl;
⑬find() 查找
cout << s1.find('b') <<endl; //查找s1是否有b,若有,返回下标
cout << s1.find("abc") <<endl; //查找s1是否有abc,若有,返回下标
cout << s1.find(s2,0) <<endl;//从s1的下标为0开始寻找是否有s2
//若找到返回字符的下标(如果是查找串只返回串中第一个字符的下标)
⑭substr() 返回某个子字符串
cout << s1.substr(1,4) <<endl;
//返回s1下标为1到下标为4的字符串
⑮swap() 交换两个字符串的内容
s1.swap(s2);
⑯erase() 删除字符
s1.erase(1,2); //删除下标为1开始的2个字符
⑰replace() 替换字符
cout<<s1.replace(0,3,"hello")<<endl; //删除从0开始的3个字符,将这几个字符换成hello
对第⑩~⑰函数进行代码实现一下:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
string s1="abcdef";
string s2="abcd";
cout << s.empty() <<endl;
cout << s1.empty() <<endl;
cout << s1.compare(s2) << endl;
char xx[10]={0};
s1.copy(xx,3,2); //将s1中从下标为2开始的3个字符复制给数组xx
cout << xx <<endl;
cout << s1.find('b') <<endl; //查找s1是否有b,若有,返回下标
cout << s1.find("abc") <<endl; //查找s1是否有abc,若有,返回下标
cout << s1.find(s2,0) <<endl;//从s1的下标为0开始寻找是否有s2
cout << s1.substr(1,4) <<endl; //返回s1下标为1到下标为4的字符串
s1.swap(s2);
cout << "s1:" << s1 <<endl;
cout << "s2:" << s2 <<endl;
s1.erase(1,2); //删除下标为1开始的2个字符
cout <<"s1:" << s1 <<endl;
cout<<s1.replace(0,3,"hello")<<endl; //删除从0开始的3个字符,将这几个字符换成hello
return 0;
}
运行结果: