C++STL--------string

发布于:2024-10-11 ⋅ 阅读:(123) ⋅ 点赞:(0)

图片来源: 查询C/C++内置库的网站

一、STL介绍

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

二、string

string是一个串的数据结构,是在头文件<string>里面的一个类,在标准库中,已经写好了,程序员可以直接使用的类。
跟C的字符串一样,会在结尾处加一个\0
封装在C++标准库std中
在这里插入图片描述

1、constructor构造函数

在这里插入图片描述
设计了七种构造函数,其中第七种是一个模版,用来复制字符顺序表范围内的字符。
string是一个类,在堆上申请空间。

2、operator[]方括号运算符重载

在这里插入图片描述
这个运算符重载后能像数组一样访问数据,重载了两个函数,一个是用于修改的,加const是用于对不能修改内容使用的。

  • 但比数组好的是可以检查越界,string类里面有个成员变量是记录有效个数的,在用方括号访问时会断言有没有超过有效个数。
3、iterator迭代器

string遍历通过下标来访问遍历

#include <iostream>
#include <string>

using namespace std;

int main()
{

	string s("hello world");

	//下标
	for (size_t i = 0; i < s.size(); i++)
	{
		cout << s[i];
	}

	cout << endl;

	return 0;
}

使用迭代器iterator来访问

#include <iostream>
#include <string>

using namespace std;

int main()
{

	string s("hello world");
	//迭代器
	string::iterator it = s.begin();
	while (it != s.end())
	{
		cout << *it;
		++it;
	}
	return 0;
}

iterator是属于string类域的一个内部类
**beging()**是一个成员函数返回类型是迭代器类型,返回的是数据的第一个位置
在这里插入图片描述
**end()**是一个成员函数,返回的是最后一个数据的下一个位置,理解为\0的位置

  • 当然在string这里数据存储是连续的用下标更爽一下,对于链表这些数据结构就能体现迭代器的优势。
  • 如果对象是const修饰的,那么迭代器要使用const_iterator
4、reverse_iterator反向迭代器
	string s("hello wordl");
	string::reverse_iterator rit = s.rbegin();
	while (rit != s.rend())
	{
		cout << *rit;
		rit++;
	}

在这里插入图片描述
rbegin返回最后一个数据位置
rend返回第一个数据位置

  • 如果对象是const修饰的,迭代器使用const_reverse_iterator
5、size和length

计算有效数据个数

string s("hello wordl");
cout << s.size() << endl;
cout << s.length() << endl;

size 和 length功能一样

6、capacity
	string s("hello wordl");
	cout << s.capacity() << endl;

计算空间容量大小

7、clear
	string s("hello wordl");
	cout << s.size() << endl;
	s.clear();
	cout << s.length() << endl;
	cout << s.capacity() << endl;

在这里插入图片描述
清空有效个数
但释不释放空间看编译器标准没规定

8、shrink_to_fit
	string s(100,'x');
	cout << s.capacity() << endl;

	s.resize(10);
	cout << s.capacity() << endl;

	s.shrink_to_fit();
	cout << s.capacity() << endl;

在这里插入图片描述

减少有效空间,是有效空间大小跟它的有效个数匹配

9、at

在这里插入图片描述
放问某个位置的数据,跟运算符重载方括号一样,区别是at失败了会抛异常

10、push_back

在这里插入图片描述

尾插数据,只能一次插入一个字符

11、append

在这里插入图片描述

二、auto类型(C++11)

1、使用

自动推导类型

	int i = 10;
	auto a = 1;//int 
	auto b = 1.1;//double
	auto c = &i;//int*

根据右边的值来推导类型

  • 不能推导引用类型
	int& r = i;
	auto d = r;//int
	auto& e = r;//int&

手动加&才能推出引用类型

2、真正的价值

相迭代器这种很长的类型,用auto就会很方便

	string s("hello world");
	//迭代器用auto
	auto it = s.begin();
	while (it != s.end())
	{
		cout << *it;
		++it;
	}
  • C++20才支持auto做参数,和返回类型

三、范围for(C++11)

1、使用

跟方便和简化代码,跟auto一样不是新的功能,只是提供一直语法简化代码量
自动判断结束,自动++
比如用范围for来遍历string:

	string s("hello world");
	cout << endl;
	//用范围for
	for (char ch : s)
	{
		cout << ch;
	}
	cout << endl;
	//跟懒的方法
	for (auto ch : s)
	{
		cout << ch;
	}

底层也是使用迭代器来遍历,编译器来当牛马,但上面这些代码是无法修改原数据的,是把原数据赋值给ch的
想要修改数据就要使用引用

	for (auto& ch : s)
	{
		ch++;
		cout << ch;
	}
2、数组也可以使用

编译器经过特殊处理,数组没有迭代器,但数组的存储时连续的,使用指针就可以直接访问,所以可以使用范围for

int a[] = { 1,2,3,4,5,6 };
for (auto i : a)
{
	cout << i;
}
  • 总结:适用于容器遍历和数组遍历

网站公告

今日签到

点亮在社区的每一天
去签到