【C++】:STL——标准模板库介绍 || string类

📚1.什么是STL

STL(standard template libaray-标准模板库):是C++标准库的重要组成部分,不仅是一个可复用的组件库,而且
是一个包罗数据结构与算法的软件框架

📚2.STL的版本

原始版本
Alexander Stepanov、Meng Lee 在惠普实验室完成的原始版本,本着开源精神,他们声明允许任何人任意运用、拷贝、修改、传播、商业使用这些代码,无需付费。唯一的条件就是也需要向原始版本一样做开源使用。 HP 版本–所有STL实现版本的始祖
P. J. 版本
由P. J. Plauger开发,继承自HP版本,被Windows Visual C++采用,不能公开或修改,缺陷:可读性比较低,
符号命名比较怪异
RW版本
由Rouge Wage公司开发,继承自HP版本,被C+ + Builder 采用,不能公开或修改,可读性一般
SGI版本
由Silicon Graphics Computer Systems,Inc公司开发,继承自HP版 本。被GCC(Linux)采用,可移植性好,可公开、修改甚至贩卖,从命名风格和编程 风格上看,阅读性非常高。我们后面学习STL要阅读部分源代码,主要参考的就是这个版本

📚3. STL的六大组件

📚4.STL的重要性


📚5.如何学习STL





简单总结一下:学习STL的三个境界:能用,明理,能扩展

📚6.STL的缺陷

📚1. STL库的更新太慢了。这个得严重吐槽,上一版靠谱是C++98,中间的C++03基本一些修订。C++11出来已经相隔了13年,STL才进一步更新
📚2. STL现在都没有支持线程安全。并发环境下需要我们自己加锁。且锁的粒度是比较大的
📚3. STL极度的追求效率,导致内部比较复杂。比如类型萃取,迭代器萃取
📚4. STL的使用会有代码膨胀的问题,比如使用vector/vector/vector这样会生成多份代码,当然这是模板语法本身导致的

📚7.为什么学习string类?

C语言中的字符串
C语言中,字符串是以’\0’结尾的一些字符的集合,为了操作方便,C标准库中提供了一些str系列的库函数,但是这些库函数与字符串是分离开的,不太符合OOP的思想,而且底层空间需要用户自己管理,稍不留神可能还会越界访问
面试题(暂不做讲解)
在OJ中,有关字符串的题目基本以string类的形式出现,而且在常规工作中,为了简单、方便、快捷,基本都使用string类,很少有人去使用C库中的字符串操作函数

📚8.标准库中的string类

string类(了解)
string类的文档介绍
📚1. 字符串是表示字符序列的类
📚2. 标准的字符串类提供了对此类对象的支持,其接口类似于标准字符容器的接口,但添加了专门用于操作
单字节字符字符串的设计特性
📚3. string类是使用char(即作为它的字符类型,使用它的默认char_traits和分配器类型(关于模板的更多信息,请参阅basic_string)
📚4. string类是basic_string模板类的一个实例,它使用char来实例化basic_string模板类,并用char_traits和allocator作为basic_string的默认参数(根于更多的模板信息请参考basic_string)
📚5. 注意,这个类独立于所使用的编码来处理字节:如果用来处理多字节或变长字符(如UTF-8)的序列,这个
类的所有成员(如长度或大小)以及它的迭代器,将仍然按照字节(而不是实际编码的字符)来操作
总结:
📚1. string是表示字符串的字符串类
📚2. 该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操作
📚3. string在底层实际是:basic_string模板类的别名,typedef basic_string<char, char_traits, allocator>string;
📚4. 不能操作多字节或者变长字符的序列
在使用string类时,必须包含#include头文件以及using namespace std;

我将展示一段代码让大家详细的通过代码来解释上面的问题 因为英文很多同学看不太懂 计算机的知识也比较晦涩难懂

#include<iostream>
#include<string>
#include<Windows.h>
#include<assert.h>
using namespace std;
int main()
{
	string s1;
	string s2("hello world");
	string s3 = s2;
	string s4(s2);
	
	cout << s1;
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;
	
	string s5(s2, 1, 6);
	cout << s5 << endl;
	
	string s6(s2, 1);
	cout << s6 << endl;
	
	string s7(s2, 1, 100);
	cout << s7 << endl;
	
	string s8("hello world", 5);
	cout << s8 << endl;
	
	string s9(10, 'x');
	cout << s9 << endl;
	
	s1 = s2;
	cout << s1 << endl;
	
	s1 = "world";
	cout << s1 << endl;
	
	s1 = 'x';
	cout << s1 << endl;
	
	system("pause");
	return 0;


我们这里仅仅讲解一下size和length
剩下的我们到后面的STL再讲

#include<iostream>
#include<string>
#include<Windows.h>
#include<assert.h>
using namespace std;
namespace bit
{
	class string
	{
	private:
		char* _str;
		size_t _size;
		size_t _capacity;
	};
}
int main()
{
	//遍历和访问
	string s1("hello world");
	
	cout << s1.size() << endl;
	cout << s1.length() << endl;
	
	for (size_t i = 0; i < s1.size(); i++)
	{
		cout << s1[i] << " ";
		cout << s1.operator[](i) << " ";
	}
	cout << endl;
	
	s1[0] = 'x';
	cout << s1 << endl;
	system("pause");
	return 0;


这里我们都只是挑选其中部分来讲 因为我们现在学习的知识还不足以支撑我们去写更高级的东西

#include<iostream>
#include<string>
#include<Windows.h>
#include<assert.h>
#include<vector>
#include<list>
int main()
{
	string s1 = "hello bit";
	string::iterator it = s1.begin();
	while (it != s1.end())
	{
		cout << *it;
		it++;
	}
	cout << endl;
	for (auto ch : s1)
	{
		cout << ch << " ";
	}
	cout << endl;
	string s1;
	string s2("hello bit");
	string s3 = "hello bit";//先构造再拷贝构造,创建中间临时变量进行拷贝构造,编译器优化直接进行构造
	for (size_t i = 0; i < s2.size(); i++)
	{
		s2[i]++;
	}
	cout << s2 << endl;
	for (size_t i = 0; i < s2.size(); ++i)
	{
		cout << s2[i] << " ";
	}
	cout << endl;
	for (size_t i = 0; i < s3.size(); i++)
	{
		s3[i]++;
	}
	cout << s3 << endl;
	system("pause");
	return 0;
	// string给逆置一下
	size_t begin = 0, end = s1.size() - 1;
	while (begin < end)
	{
		/*char tmp = s1[begin];
		s1[begin] = s1[end];
		s1[end] = tmp;*/
		swap(s1[begin], s1[end]);

		++begin;
		--end;
	}
	cout << s1 << endl;

	// iterator用法像指针
	string::iterator it = s1.begin();
	while (it != s1.end())
	{
		*it += 1;
		cout << *it << " ";
		++it;
	}
	cout << endl;
	reverse(s1.begin(), s1.end());
	cout << s1 << endl;

	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	vector<int>::iterator vit = v.begin();
	while (vit != v.end())
	{
		cout << *vit << " ";
		++vit;
	}
	cout << endl;

	reverse(v.begin(), v.end());

	vit = v.begin();
	while (vit != v.end())
	{
		cout << *vit << " ";
		++vit;
	}
	cout << endl;

	list<double> lt;
	lt.push_back(1.1);
	lt.push_back(2.1);
	lt.push_back(3.1);
	lt.push_back(4.1);
	list<double>::iterator lit = lt.begin();
	while (lit != lt.end())
	{
		cout << *lit << " ";
		++lit;
	}
	cout << endl;

	reverse(lt.begin(), lt.end());
	lit = lt.begin();
	while (lit != lt.end())
	{
		cout << *lit << " ";
		++lit;
	}
	cout << endl;

	return 0;
}

int main()
{
	string s1("hello world");
	const string s2("hello world");
	s1[0] = 'x';
	//s2[0] = 'x';
	cout << s2[0] << endl;

	string::const_iterator it = s2.begin();
	while (it != s2.end())
	{
		//*it += 1;
		cout << *it << " ";
		++it;
	}
	cout << endl;

	// yyds
	for (auto e : s1)
	{
		cout << e << " ";
	}
	cout << endl;

	return 0;
}

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
青葱年少的头像青葱年少普通用户
上一篇 2023年12月7日
下一篇 2023年12月7日

相关推荐