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

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封装的接口函数,下期将讲解关于如何实现这些函数。

请大佬们一键三连~

相关文章:

  • 微信小程序页面制作——婚礼邀请函(含代码)
  • linux 解压缩
  • sql server 分区表查询
  • Android 12系统源码_窗口管理(八)WindowConfiguration的作用
  • springboot通过tomcat部署项目(包含jar、war两种方式,迄今为止全网最详细!2024更新..建议收藏,教学!万字长文!)
  • 二叉树的链式结构和递归程序的递归流程图
  • JavaScript substring() 方法
  • 关于循环Socket创建超Linux文件句柄限制现象分析
  • Etcd权限认证管理
  • HTML——基本标签
  • Basler 相机与LabVIEW进行集成
  • 便捷数据检索与下载,拟合曲线预测趋势 轻松管理多个项目,实现在线监测
  • Python和C++气候模型算法模型气候学模拟和统计学数据可视化及指标评估
  • SQLite的入门级项目学习记录(四)
  • SpringDataJPA基础增删改查
  • 解决内存8G但是需要读取一个几百G的文件到内存的方法
  • 学术界的利器:Zotero插件提升你的研究效率
  • 深入探究 Flask 的应用和请求上下文
  • 【深度学习】(1)--神经网络
  • leetcode:2124. 检查是否所有 A 都在 B 之前(python3解法)
  • 中欧金融工作组第二次会议在比利时布鲁塞尔举行
  • 北京航空航天大学首个海外创新研究院落户巴西
  • 习近平出席中拉论坛第四届部长级会议开幕式并发表主旨讲话
  • 马上评丨未成年人“擦边”短视频岂能成流量密码
  • 第1现场 | 印巴停火次日:当地民众逐渐恢复正常生活
  • 婚姻登记“全国通办”首日观察:数据多跑路,群众少跑腿