一、STL
1. STL基本概念
- STL(Standard Template Library):标准模板库
- 从广义上讲分为:容器(container)算法(algorithm)迭代器(iterator)
- 容器和算法之间通过迭代器进行无缝连接
- STL几乎所有的代码都采用了模板类或者模板函数
2. STL六大组件
六大组件分别为:容器、算法、迭代器、仿函数、适配器(配接器)、空间配置器
- 容器:各种数据结构,如vector、list、deque、set、map等,用来存放数据。
- 算法:各种常用的算法,如sort、find、copy、for_each等。
- 迭代器:扮演了容器与算法之间的胶合剂。
- 仿函数:行为类似函数,可作为算法的某种策略。
- 适配器:一种用来修饰容器或者仿函数或迭代器接口的东西。
- 空间配置器:负责空间的配置与管理。
3. STL中容器、算法、迭代器
(1)容器
STL容器就是将运用最广泛的一些数据结构实现出来。最常用的数据结构:数组、链表树、栈、队列、集合、映射表等。这些容器分为 序列式容器和关联式容器两种。
- 序列式容器:强调值的排序,序列是容器中的每个元素均有固定的位置。
- 关联式容器:二叉树结构,各元素之间没有严格的物理上的顺序关系,会自动进行排序。
(2)算法 Algorithms
分为 质变算法和非质变算法。
- 质变算法:会更改区间内的元素内容,如拷贝、替换、删除等
- 非质变算法:不会会更改区间内的元素内容,如查找、计数、遍历、寻找极值等。
(3)迭代器
提供一种方法,使之能够依顺序寻访某个容器所含的各个元素,而又无需暴露该容器的内部表示方式。每个容器都有自己专属的迭代器。算法通过迭代器访问容器中的数据。迭代器类似于指针。
迭代器种类:
- 输入迭代器 – 对数据的只读访问 – 支持运算:只读,支持++、==、!=
- 输出迭代器 – 对数据的只写访问 – 支持运算:只写,支持++
- 前向迭代器 – 读写操作,并能向前推进迭代器 – 支持运算:读写,支持++、==、!=
- 双向迭代器 – 读写操作,并能影响向前和向后操作 – 支持运算:读写,支持++、–
- 随机访问迭代器 – 读写操作,可以以跳跃的方式访问任意数据,功能最强的迭代器 – 支持运算:读写,支持++、–、[n]、-n、<、<=、>、>=
常用的容器中迭代器种类分为双向迭代和随机访问迭代器
4. 容器、算法、迭代器初识
(1)vector存放内置数据类型
- 容器:
vector
- 算法:
for_each
- 迭代器:
vector<int>::iterator
代码测试:
#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<vector>
#include<algorithm> //标准算法头文件
void myPrint(int val)
{
cout << val << endl;
}
//vector容器存放内置数据类型
void test01()
{
//创建了应该vector容器,也就是数组
vector<int> v;
//利用尾插法,向容器中插入数据
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
通过迭代器访问容器中的数据
v.begin()是起始迭代器,指向容器中第一个元素
//vector<int>::iterator itBegin = v.begin();
v.end()是结束迭代器,指向容器中最后一个元素的下一个位置
//vector<int>::iterator itEnd = v.end();
第一种遍历容器的方式
//while (itBegin != itEnd)
//{
// cout << *itBegin << endl;
// itBegin++;
//}
第二种遍历方式
//for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
//{
// cout << *it << endl;
//}
//第三种遍历方式,利用STL提供的遍历算法
for_each(v.begin(),v.end(), myPrint);
}
int main()
{
test01();
system("pause");
return 0;
}
输出:
10
20
30
40
请按任意键继续. . .
(2)Vector存放自定义数据类型
目标:vector中存放自定义数据类型,并打印输出
代码测试:
#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<vector>
#include<algorithm> //标准算法头文件
class Person
{
public:
Person(string name, int age)
{
this->m_Age = age;
this->m_Name = name;
}
string m_Name;
int m_Age;
};
void test01()
{
cout << "test01 run ..." << endl;
vector<Person> v;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
Person p5("eee", 50);
//向容器中尾插数据
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
//遍历容器中的数据
//it是个指针类型,解引用后是一个Person类型数据
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
//cout << "姓名:" << (*it).m_Name << " 年龄:" << (*it).m_Age << endl;
//或者
cout << "姓名:" << it->m_Name << " 年龄:" << it->m_Age << endl;
}
}
//存放自定义数据类型 指针
void test02()
{
cout << endl << "test02 run ..." << endl;
vector<Person*> v;//指针类型
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
Person p5("eee", 50);
//向容器中尾插数据
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3);
v.push_back(&p4);
v.push_back(&p5);
//遍历容器,(*it)之后是Person指针
for (vector<Person*>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "姓名:" << (*it)->m_Name << " 年龄:" << (*it)->m_Age << endl;
}
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
输出:
test01 run ...
姓名:aaa 年龄:10
姓名:bbb 年龄:20
姓名:ccc 年龄:30
姓名:ddd 年龄:40
姓名:eee 年龄:50
test02 run ...
姓名:aaa 年龄:10
姓名:bbb 年龄:20
姓名:ccc 年龄:30
姓名:ddd 年龄:40
姓名:eee 年龄:50
请按任意键继续. . .
(3)Vector容器嵌套容器
目标:容器中嵌套容器,将所有数据进行遍历输出
代码测试:
#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<vector>
#include<algorithm> //标准算法头文件
void test01()
{
vector<vector<int>>v;
vector<int>v1;
vector<int>v2;
vector<int>v3;
vector<int>v4;
//向小容器中添加数据
for (int i = 0; i < 4; i++)
{
v1.push_back(i + 1); // 1 2 3 4
v2.push_back(i + 2); // 2 3 4 5
v3.push_back(i + 3); // 3 4 5 6
v4.push_back(i + 4); // 4 5 6 7
}
//将小容器插入到大容器中
v.push_back(v1);
v.push_back(v2);
v.push_back(v3);
v.push_back(v4);
//通过大容器,把所有数据遍历一遍
// (*it)还是一个容器vector<int>,需要进一步 *(*it)解析成指针
for (vector<vector<int>>::iterator it = v.begin(); it < v.end(); it++)
{
for (vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++)
{
cout << *vit << " ";
}
cout << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
输出:
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7
请按任意键继续. . .