分享缩略图

分享到:
链接已复制
首页> 新闻中心>

C 初级学习第六弹-探索STL奥秘(1)-标准库中的string类

2025-06-24 12:22:54

来源:新华网

字体:

前言:

࿰在前面c;我们学习了C++类与对象,认识C++与C语言的一些差异,今日,我们将进入CƱ+的 关键部分-。STL。,学完这部分后,我们可以清楚地认识到C++与C语言相比,快捷方便。

目录。

为什么有string类?

二、标准库中的string类。

1、什么是string?

2、常用的string接口函数。

2.1 string类对象的结构。

2.2 string的容量操作。

2.3 访问和遍历string。

2.4 修改string类对象。

2.5 非成员函数常用于string类。

三、总结。


为什么有string类?

当我们学习C语言时,有一点很难处理,那就是字符串,字符串访问,增删查改非常不方便,因此,我们包装了一个string类,主要处理与字符串相关的问题。

二、标准库中的string类别。

1、什么是string?

我们可以简单地将string类理解为变长字符数组,我们可以添加、删除和修改它等一系列操作c;同时,我们可以直接使用,一般来说,我们需要功能函数。

string类的成员函数:

class string{ private:	char* a;	int _capacity;	int _size;};

2、常用的string接口函数。

在使用。string。类时,必须包含。#include。头文件。以及。using namespace std。;

我们将在后面讨论这些接口函数的原理,先说说这些接口函数的用法,如果你学会了使用它,你可以直接使用string类来解决问题,这部分内容没有重点讲解,以下主要是直接给出代码示例。

2.1 string类对象的结构。

代码实例:
#include#includeusing namespace std;int main(){ 	string();               //1、构建空的string对象,这个对象只在本行起作用,除非加const修改	string s1("abc");       //2、直接构造	cout << "s1:" << s1 << endl;	char arr[] = "abc";	string s2(arr);         //3、构建一个字符串的首地址	cout << "s2:" << s2 << endl;	string s3 = s1;         //4、复制结构(使用一个现有的类对象初始化另一个对象)	cout << "s3:" << s3 << endl;	string s4(3, 'x');      //5、在构造过程中,前N个赋值为同一个字符	cout << "s4:" << s4 << endl;	return 0;}。

运行结果:

2.2 string的容量操作。

代码实例:
#include#includeusing namespace std;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;        ///开放空间大小(当空间不足时,会自动扩容#xff0c;扩容空间是原空间的1.5倍(与环境相关的))	cout << "empty:" << s1.empty() << endl;     //检查字符串是否空,表示非空�1表示空	s1.clear();                                 ///清空字符串	cout <<"s1:"<< s1 << endl;	s1.reserve(100);                            //开辟指定大小的空间�通常会多一点)	cout << "capacity:" << s1.capacity() << endl;	s1.resize(5, 'a');	cout << "size:" << s1.size() << endl;	cout << "s1:" << s1 << endl;	return 0;}。

运行结果:

2.3 访问和遍历string。

代码实例:

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

运行结果:

2.4 修改string类对象。

代码实例:

#includeusing namespace std;#includeint main(){ 	string s1("zhan");	cout << s1 << endl;	//push_back  在末尾添加字符	cout << "push_back后:";	s1.push_back('g');	cout << s1 << endl;	//append     在末端添加字符串	cout << "append后:" ;	s1.append(" san");	cout << s1 << endl;	//operator+= 随意添加到末端	cout << "+=后:";	s1 += " 18";	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('a');	cout << npos1 << endl;	//rfind  从pos位置向前搜索字符,并返回其位置	cout << "rfind:";	int npos2 = s1.rfind('a');	cout << npos2 << endl;	//substr  从pos位置截取n个字符并返回	cout << "substr后:";	string tmp = s1.substr(npos1, npos2 - npos1);	cout << tmp << endl;	return 0;}。

运行结果:

2.5 非成员函数常用于string类。

代码实例:

#includeusing namespace std;#includeint main(){ 	string s1("hello ");	string s2("world");	//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;	//getline      获取一行字符串	cout << "getline:";	string s4;	getline(cin, s4);    ///这个程序无法测量,需要单独进行测试	cout << s4 << endl;	//relational operators   比较大小	///这个函数库中有各种比较函数(==、>、<...),bool࿰的函数类型c;感兴趣的人可以自己探索	return 0;}。

运行结果:

三、总结。

运行结果:

三、总结。

以上是我们常用的string类成员函数和类外函数,因为这些函数已经包装好了,因此,我们可以直接使用󿀌至于如何实现这些函数�下一章我们再谈。感谢您的观看,创作不容易󿀌还请一键三连!!!

【责任编辑:新华网】
返回顶部