CC14.【C++ Cont】string类字符串的push_back、pop_back、字符串+=与+运算和insert

目录

1.push_back函数

示例代码1:尾插一个字符

运行结果

示例代码2:使用循环连续尾插多个字符

运行结果

2.pop_back函数

示例代码1:尾删一个字符

运行结果

示例代码2:尾删多个字符

运行结果

注意事项

错误代码

运行结果

示例代码3:循环删除字符

 运行结果

3.+=和+运算

示例代码1:尾部添加字符串

运行结果

示例代码2:头部添加字符串

运行结果

4.insert函数

1.插入string类字符串

运行结果

2.插入C语言风格的字符串

执行结果

3.批量插入n个相同的字符

运行结果


★2024年最后一篇★

1.push_back函数

显然是尾插(之前讲过SLPushBack函数,参见83.【C语言】数据结构之顺序表的尾部插入和删除文章)

因此push_back()用于在字符串尾部插一个字符

示例代码1:尾插一个字符

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s = "abcde";
	s.push_back('f'); //在字符串s的尾部插入字符f
	cout<<s;
	return 0;
}

运行结果

示例代码2:使用循环连续尾插多个字符

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s;
	for (char i='a';i<='z';i++)
	{
		s.push_back(i);
	}
	cout<<s;
	return 0;
}
运行结果

2.pop_back函数

显然是尾删, (之前讲过SLPopBack函数,参见83.【C语言】数据结构之顺序表的尾部插入和删除文章)

pop_back用于在字符串尾部删除一个字符(C++11才引入)

示例代码1:尾删一个字符

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s = "abcde";
	s.pop_back();
	cout<<s;
	return 0;
}
运行结果

示例代码2:尾删多个字符

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s("abcdefghi");
	short n=5; 
	while (n--)//尾删5次
	{
		s.pop_back();
	}
	cout<<s;
	return 0;
}
运行结果

注意事项

当字符串为空时,禁止使用pop_back!!

错误代码

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s;
	s.pop_back();
	cout<<s;
	return 0;
}
运行结果

报错提示:size()为0,不能删字符串,而且是未定义行为

参见cplusplus官网的说明https://legacy.cplusplus.com/reference/string/string/pop_back/

示例代码3:循环删除字符

注意:size()不为0时才能删除,因此需要判断

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s="abcdefg";
	while(s.size())
		s.pop_back();
	cout<<s.size();
	return 0;
}
 运行结果

3.+=和+运算

示例代码1:尾部添加字符串

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s="abc";
	s+="def";//s的字符串已被修改
	cout<<s;
	return 0;
}

注意:在s+="def";中,字符串s已被修改

运行结果

 对比下面的代码

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s="abc";
	cout<<s+"def"<<endl;//s并没有改变
	cout<<s;
	return 0;
}

注意:在cout<<s+"def"<<endl;中,字符串s并没有改变,只是s和"def"放在一起打印了

 尾部添加字符串也可以用strcat函数,参见52.【C语言】 字符函数和字符串函数(strcat函数)文章

示例代码2:头部添加字符串

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s="abc";
	s="def"+s;
	cout<<s;
	return 0;
}
运行结果

和示例代码1对比可以看出:s="def"+s;s=s+"def";的结果是不同的!!!一个是头部追加,一个是尾部追加

4.insert函数

类比85.【C语言】数据结构之顺序表的中间插入和删除及遍历查找文章的SLInstert函数,显然insert用于在字符串中间的某个位置插入一个字符串

cplusplus对insert的介绍 点我跳转中该函数的用法有很多种,这里简单介绍其中的三种

string& insert (size_t pos, const string& str); //pos位置前面插入一个string字符串
string& insert (size_t pos, const char* s); //pos位置前面插入一个C风格的字符串
string& insert (size_t pos, size_t n, char c); //pos位置前面插入n个字符c

注意:1.都是在pos位置前(注意pos为0为第一个字符,下标从0开始)处理字符串 2.第二个参数的类型可以有三种 3.返回值为string& insert

1.插入string类字符串

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string ori="abfg";
	string insert_str="cde";
	ori.insert(2,insert_str);
	cout<<ori; 
	return 0;
}
运行结果

2.插入C语言风格的字符串

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string ori="abfg";
	char insert_str[]="cde";
	ori.insert(2,insert_str);//写法等同于ori.insert(2,"cde");
	cout<<ori; 
	return 0;
}

 C语言风格的字符串存储在字符数组中,因此传字符数组的指针

执行结果

3.批量插入n个相同的字符

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string ori="abfg";
	char insert_str[]="cde";
	ori.insert(2,7,'A');//在b的前面插入7个A
	cout<<ori; 
	return 0;
}
运行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangcoder

赠人玫瑰手有余香,感谢支持~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值