STL容器string的使用

string的本质是一个类,类内部封装了char*,管理字符串,是一char型的容器。而char是一个指针。
string既然是一个类,那么他就可以像类那样初始化。

void test01()
{
	string s = string("asdfg");//显示法
	string s1("asdf00");//括号法
	string s2 = "asdfg";//隐式转化法
	string s3(s2);//拷贝构造
	//string一个独特的n个字符初始化;
	string s4(10, 'c');
	cout << s4 << endl;//cccccccccc
}

另外string内部重载了assign函数用于string对象的赋值。

void test02()
{
	string s1;
	s1.assign("asdfg");//将字符串"asdfg"赋予s1;
	string s2;
	s2.assign(s1);//相当于s2=s1
	string s3;
	const char* s = "qwert";
	s3.assign(s, 3);//将s1的前三个字符赋予s3,其中s不能是string类型
	cout << s3 << endl;//qwe
	string s4(10, 'w'); //把10个字符w赋予s4
}

string类重载了+=和append函数实现字符串的拼接

void test03()
{
	string s1 = "asdf";
	s1 += "qwer";
	s1 += s1;//两种全是在结尾处拼接
	cout << s1 << endl;//asdfqwerasdfqwer
	string s2("000");
	string s3("111");
	s2.append(s3);//将s3接到s2结尾等同于s2.append("111");
	cout << s2 << endl;//000111
	const char* s = "aqw";
	s2.append(s, 2);//将s的前两个字符接到s2结尾;其中s不能是string类型
	cout << s2 << endl;//000111aq
	string s4 = "qwertyu";
	string s5;
	//s5.append("1234", 0, 3);
	//s5.append(s4, 1, 3);
	//s5.append(s, 0, 2);//重载类型s5.append("s",pos,n),s是指上三种其中一种字符串,字符串s中从pos开始的n个字符连接到s5字符串尾,其中n可以大于s的长度,当大于s的长度即为将pos位置及其后的字符串接到s5尾部
	//cout << s5 << endl;
}

查找函数find和refind。
find查找第一次出现的位置

void test04()
{
	string s1 = "asdfghjkl";
	string s2 = "sdfg";
	int pos1=s1.find(s2, 0);//从0位置开始,查找s2第一次出现的位置,返回类型是int,s2也可以是char*类型
	cout << pos1 << endl;
	const char* s = "dfgh";
	int pos2 = s1.find(s, 0, 10);///从0位置开始,从前10个字符中查找s第一次出现的位置,返回类型是int,s只能是char*类型
	char c = 'f';
	int pos3 = s1.find(c, 0);//从0位置开始,查找字符c第一次出现的位置,返回类型是int,

}
refind查找最后一次的位置
void test05()
{
	string s1 = "asdfghjkl";
	string s2 = "sdfg";
	int pos1 = s1.rfind(s2, 0);//从0位置开始,查找s2最后一次出现的位置,返回类型是int,s2也可以是char*类型
	cout << pos1 << endl;
	const char* s = "dfgh";
	int pos2 = s1.rfind(s, 0, 10);///从0位置开始,从前10个字符中查找s最后一次出现的位置,返回类型是int,s只能是char*类型
	char c = 'f';
	int pos3 = s1.rfind(c, 0);//从0位置开始,查找字符最后一次出现的位置,返回类型是int,
}

替换函数replace

	string s1 = "asdfghjkl";
	string s2 = "dfg";
	const char* s = "dfg";
	s1.replace(0, 1, s);//将s1从0到1字符替换成字符串s,位置1的字符不发生改变,s既可以是string类型也可以是char*类型;
	cout << s1 << endl;//dfgsdfghjkl

比较函数compare

void test07()
{
	string s1 = "asfghjkl";
	string s2 = "asxcv";
	int result = s1.compare(s2);
	cout << result << endl;//-1
	//字符串比较是按照字符的ascii码进行对比
	//vs的比较结果,根据s1.compare(s2);若s1=s2,返回0;若s1>s2返回1;若s1<s2返回-1;
}

string中单个字符的存取

void test08()
{
	string s1 = "asdfghjkl";
	cout << s1[2] << endl; //d
	s1[2] = 'c';
	cout << s1 << endl; //ascfghjkl
	cout << s1.at(2) << endl;//c
	s1.at(2) = 'd';
	cout << s1 << endl;//asdfghjkl
}

string字符串的插入

void test09()
{
	string s1 = "asdfghjkl";
	s1.insert(1, "sdf");//从位置1前插入字符串,也就是s前
	cout << s1 << endl;//asdfsdfghjkl;
	const char* s = "qwer";
	s1.insert(0, s);//从位置0前插入字符串,s也可以是string类型
	cout << s1 << endl;//qwerasdfsdfghjkl
	s1.insert(1, 10, 'c');//在字符串s1位置1前插入10个字符c
	cout << s1 << endl;//qccccccccccwerasdfsdfghjkl
}

string字符串的删除

void test10()
{
	string s1 = "asdfghjkl";
	s1.erase(2, 5);//从下标是2的开始删除往后删除5个字符
	cout << s1 << endl;//askl
}

从string类型中获得子字符串

void test11()
{
	string s = "abcdefg";
	string s1 = s.substr(1, 4);//从下标1开始取四个字符;
	cout << s1 << endl;//bcde   
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值