1.STL(标准模板库)
- 诞生原因:提高代码复用性,提供一套标准的数据结构和代码
1.1基本概念
- 1.广义上分-容器,算法,迭代器、
- 2.容器和算法经过迭代器进行连接
- 3.STL几乎所有所有代码都采用了模板类或模板函数
1.2 STL六大组件
STL大体分为:容器,算法,迭代器,仿函数,适配器(配接器),空间配置器
- 1.容器:各种数据结构,用来存放数据
- 2.算法:各种常用算法,
- 3.迭代器: 扮演容器与算法之间的胶合剂
- 4.仿函数: 行为类似函数,可作为算法的某种策略
- 5.适配器:一种用来修饰容器或者仿函数或迭代器接口的东西(不常用)
- 6.空间配置器:负责空间的配置与管理(不常用)
1.3 STL中容器,算法,迭代器
容器:将运用最广泛的一些数据结构实现出来
容器分类:
- 1.序列式容器:强调值排序,序列式容器中的每个元素均有固定位置
- 2.关联式容器:二叉树结构,各元素之间没有严格的物理上的顺序关系
常见容器有:向量(vector),列表(list),队列(queue),双端队列(deque),优先队列(Priority queue),集合(set),多种集合(multiset),映射(map),多重映射(multimap)。
算法分类
- 1.质变算法:运算过程中会更改区间内元素的内容的算法。-如拷贝,替换,删除
- 2.非质变算法:指运算过程中不会更改区间内的元素内容。-如查找,计数,遍历等。
迭代器:提供一种方法,算法要通过迭代器才能访问容器中的元素。
初学可理解为指针。
每个容器都有自己专属的迭代器。
迭代器种类:
- 1.输入迭代器
- 2.输出迭代器
- 3.前向迭代器
- 4.双向迭代器: 读写操作,并能向前和向后操作
- 5.随机访问迭代器:读写操作,可用跳跃的方式访问任何数据,功能最强的迭代器
1.4算法容器迭代器初识
STL中最常用的容器为vector,可理解为数组
1.4.1 vector容器存放内置数据类型
容器: vector
算法: for_each
迭代器: vector::iterator
#include<iostream>
#include<vector>//包含容器库
#include<algorithm>//STL提供的标准遍历算法头文件
using namespace std;
//打印函数
void Myprint(int val)
{
cout << val << endl;
}
void test1()
{
//创建vector容器对象,且通过模板参数指定容器中存放的数据的类型
vector<int> v;
//向文件中放数据
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
//每一个容器都有自己的迭代器,迭代器用于遍历容器中的元素
//v.begin()返回迭代器,这个迭代器指向容器中第一个数据
//v.end()返回迭代器,这个迭代器指向容器中最后一个数据的下一个位置
//vector<int>::iterator拿到vector<int>这个容器的迭代器类型
vector<int>::iterator pBegin = v.begin();
vector<int>::iterator pEnd = v.end();
//第一种遍历方式
/*while (pBegin != pEnd)
{
cout << *pBegin << endl;
pBegin++;
}*/
//第二种遍历方式
/*for (vector<int>::iterator t = v.begin(); t != v.end(); t++)
cout << *t << endl;*/
//第三种遍历方式
//使用STL提供的标准遍历算法-头文件algorithm
for_each(v.begin(), v.end(),Myprint);
}
int main()
{
test1();
system("pause");
return 0;
}
1.4.2 vector存放自定义数据类型
vector存放自定义数据类型,并打印输出
#include<iostream>
#include<vector>//包含容器库
#include<algorithm>//STL提供的标准遍历算法头文件
#include<string>
using namespace std;
class Person {
public:
Person(string name, int age)
{
this->m_name = name;
this->m_age = age;
}
string m_name;
int m_age;
};
void test02()
{
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);
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
cout << "姓名:" << (*it) .m_name<<"\t年龄:"<<(*it).m_age << endl;
}
//打印函数
void Myprint(Person* p)
{
cout << "姓名:" << p->m_name << "\t年龄:" <<p->m_age << endl;
}
void test01()
{
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);
//方法一:循环遍历输出
/*for (vector<Person*>::iterator it = v.begin(); it != v.end(); it++)
cout << "姓名:" << (*it)->m_name << "\t年龄:" << (*it)->m_age << endl;
*/
//方法二:调用STL遍历输出函数
for_each(v.begin(), v.end(), Myprint);
}
1.4.3 vector容器嵌套容器
容器嵌套容器
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);
v2.push_back(i+1);
v3.push_back(i+2);
v4.push_back(i+3);
}
//将小容器压入大容器内
v.push_back(v1);
v.push_back(v2);
v.push_back(v3);
v.push_back(v4);
//
//循环遍历输出
for (vector<vector<int>>::iterator it = v.begin(); it != v.end(); it++)
{
//此时(*t)为vector<int>类型
for (vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++)
{
cout <<"\t" << *vit;
}
cout << endl;
}
}