当前位置: 首页 > article >正文

C++初阶学习第六弹------标准库中的string类

目录

一.标准库中的string类

二.string的常用接口函数

2.1string类对象的构造

 2.2 string的容量操作

 2.3 string类的访问与遍历

 2.4 string类对象的修改

2.5 string类常用的非成员函数

 三、总结


一.标准库中的string类

可以简单理解成把string类理解为变长的字符数组,我们可以对它进行增删查改等一系列操作,同时有一些列封装的接口函数提供给我们可以让我们直接使用。

二.string的常用接口函数

2.1string类对象的构造


	string s2("hello bit"); // 用C格式字符串构造string类对象s2
	cout << s2 << endl;

	cout << endl;

	string s3(s2); // 拷贝构造s3string s3(s2); // 拷贝构造s3
	cout << s3 << endl;

 我们普便使用以上的两种方法来构造string。

 2.2 string的容量操作

int main()
{
	string s1("abcdef");
	cout << "s1:" << s1 << endl;

	cout << "size:" << s1.size() << endl;        //有效字符的个数
	cout << "length:" << s1.length() << endl;    //有效字符的个数
	//上面这两个功能上差别不大,一般我们用size()用的多一点

	cout << "capacity:" << s1.capacity() << endl;
	//开辟的空间大小(当空间不够时会自动扩容,扩容空间为原空间的1.5倍(与环境有关))

	cout << "empty:" << s1.empty() << endl;     //检查字符串是否为空,0表示非空,1表示空

	s1.clear();                                 //清空字符串
	cout << "s1:" << s1 << endl;

	s1.reserve(50);                            //开辟指定大小空间(一般会多一点)
	cout << "capacity:" << s1.capacity() << endl;

	s1.resize(5, 'a');
	cout << "size:" << s1.size() << endl;
	cout << "s1:" << s1 << endl;

	return 0;
}

 

 2.3 string类的访问与遍历

int main()
{
	string s1("abcdef");
 
	//访问方法:下标访问法
	cout << s1[1] << endl;
	cout << s1[2] << endl;
	s1[0] = 'h';
	
	
	cout << "下标遍历法:";
	for (int i = 0; i < s1.size(); i++)
	{
		cout << s1[i] << " ";
	}
	cout << endl;
 
	
	cout << "迭代器法(正向):";
	string::iterator it = s1.begin();
	for (; it != s1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
 
	
	cout << "迭代器(反向):";
	string::reverse_iterator rit = s1.rbegin();
	while (rit != s1.rend())
	{
		cout << *rit << " ";
		rit++;
	}
	cout << endl;
 
	
	cout << "范围for法:";
	for (auto e : s1)
	{
		cout << e << " ";
	}
	cout << endl;
 
	return 0;

}

 

 2.4 string类对象的修改

int main()
{
	string s1("sssssssss");
	cout << s1 << endl;
 
	//push_back  在末尾加入字符
	cout << "push_back后:";
	s1.push_back('w');
	cout << s1 << endl;
 
	//append     在末端加入字符串
	cout << "append后:" ;
	s1.append(" www");
	cout << s1 << endl;
 
	//operator+= 在末端随意添加
	cout << "+=后:";
	s1 += " yy";
	cout << s1 << endl;
 
	//c_str    返回C格式字符串
	cout << "c_str:";
	const char* m = s1.c_str();
	cout << m << endl;
 
	//find  从pos位置开始查找字符并返回其位置
	cout << "find:";
	int npos1 = s1.find('y');
	cout << npos1 << endl;
 
	//rfind  从pos位置开始往前查找字符并返回其位置
	cout << "rfind:";
	int npos2 = s1.rfind('y');
	cout << npos2 << endl;
 
	//substr  从pos位置开始截取n个字符并返回
	cout << "substr后:";
	string tmp = s1.substr(npos1, npos2 - npos1);
	cout << tmp << endl;
 
	return 0;
}

 

2.5 string类常用的非成员函数

int main()
{
	string s1("hello ");
	string s2("bit");
 
	//operator+    涉及深层拷贝,不建议多用
	cout << "operator+后:";
	cout << operator+(s1, s2) << endl;
 
	//operator>>   输入运算符重载
	cout << "operator>>:";
	string s3;
	operator>>(cin,s3);
	cout << s3 << endl;
 
	//operator<<    输出运算符重载
	cout << "operator<<:";
	operator<<(cout, s1) << endl;
 
 
	return 0;
}

 

 三、总结

以上就是常见的string封装的接口函数,下期将讲解关于如何实现这些函数。

请大佬们一键三连~


http://www.kler.cn/news/311090.html

相关文章:

  • 每日刷题(算法)
  • 开发一个电商API接口的步骤!!!
  • microchip中使用printf给AVR单片机串口重定向
  • Redis实现发布/订阅功能(实战篇)
  • uniapp中实现<text>文本内容点击可复制或拨打电话
  • tronado websocket
  • Java基础:Api 文档注释,字符串种类,String字符串创建,特点及常用方法
  • 【洛谷】P1546 [USACO3.1] 最短网络 Agri-Net 的题解
  • SqlServer自定义类型的使用
  • 【数据结构-一维差分】力扣1893. 检查是否区域内所有整数都被覆盖
  • 无人机滑环的核心特点及其应用分析
  • [论文笔记] LLM端侧小模型篇——1、剪枝量化的latency
  • MySQl篇(基本介绍)(持续更新迭代)
  • Leetcode—删除有序数组的重复项
  • 408算法题leetcode--第七天
  • Llama 3.1 大模型指令微调提升中文能力
  • 【系统架构设计师-2019年真题】案例分析-答案及详解
  • Scikit-learn 学习笔记
  • 尚品汇-秒杀商品存入缓存、Redis发布订阅实现状态位(五十一)
  • 全球首个!复旦大学冯建峰团队开发数字孪生脑平台,具备 860 亿神经元规模
  • 旷视轻量化网络shufflenet算法解读
  • MySQL——数据库的高级操作(二)用户管理(3)删除普通用户
  • 机器学习1--概述
  • Linux创建虚拟磁盘并分区格式化
  • 「Netmarble 小镇」活动来了:踏上穿越标志性世界的旅程!
  • OpenHarmony鸿蒙( Beta5.0)智能门铃开发实践
  • Unity-Transform-坐标转换
  • k8s环境下的相关操作
  • (SERIES12)DM性能优化
  • Bandicam简体中文版下载与安装百度网盘资源