STL称为标准模板库,可以分为容器container,迭代器iterator和算法algorithm。容器和算法之间通过迭代器进行无缝连接。STL的几乎所有代码都采用了类模板或者函数模板。
STL大体分为六大组件:容器、算法、迭代器、仿函数、适配器、空间配置器
容器:各种数据结构,如vector,list,deque,set,map等,用于存放数据。
算法:各种常用算法,如sort,find,copy,for_each遍历。
迭代器:扮演了容器和算法之间的胶合剂。
仿函数:行为类似函数,可作为算法的某种策略。
适配器:一种用来修饰容器或者仿函数或者迭代器接口的东西。
空间配置器:负责空间的配置与管理。
容器可以分为序列式容器和关联式容器,前者强调值的排序,序列式容器中每个元素均有固定的位置;关联式容器中各元素之间没有严格上的物理顺序关系,如二叉树。
算法用于解决逻辑或数学上的问题,可分为质变算法和非质变算法。质变算法是指运算过程中会更改元素的内容,例如拷贝、替换、删除等;非质变算法指的是运算过程中不会改变区间内元素的内容,例如查找、计数、遍历、寻找极值等。
算法要通过迭代器才能访问容器中的元素,每个容器都有自己专属的迭代器,迭代器的使用非常类似于指针。
迭代器可分为5种:
| 种类 | 功能 | 支持运算 |
| 输入迭代器 | 对数据的只读访问 | 只读,支持++、==、!= |
| 输出迭代器 | 对数据的只写访问 | 只写,支持++ |
| 前向迭代器 | 读写操作,并能够向前推进迭代器 | 读写,支持++、==、!= |
| 双向迭代器 | 读写操作,并能向前和向后 | 读写,支持++、-- |
| 随机访问迭代器 | 读写操作,可以以跳跃的方式访问任意数据,功能最强的迭代器 | 读写,支持++、--、[n]、<、>、<=、>= |
目前接触的迭代器一般都是后两个迭代器。下面简单给了一个例子来说明迭代器与容器。
- #include<vector>
- #include<iostream>
- #include<algorithm>
- using namespace std;
- void test01()
- {
- //创建一个vector容器,数组
- vector<int> v;
- //向容器中插入数据
- v.push_back(10);
- v.push_back(20);
- v.push_back(30);
- v.push_back(40);
-
- vector<int>::iterator itBegin = v.begin(); //起始迭代器,指向容器中第一个元素
- 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;
-
- }
- //第三种遍历方式 for_each
-
- }
- int main()
- {
- test01();
- }
- #include<vector>
- #include<iostream>
- #include<algorithm>
- #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 test01()
- {
- //创建一个vector容器,数组
- vector<Person> v;
- //向容器中插入数据
- Person p1("WXQ",10);
- Person p2("QWE",15);
- Person p3("QTR", 20);
- v.push_back(p1);
- v.push_back(p2);
- v.push_back(p3);
- for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
- {
- cout << (*it).m_Name <<" " << (*it).m_Age<<endl;
- }
- }
- void test02()
- {
- //存放自定义数据类型的指针
- vector<Person*> v1;
-
- Person p1("WXQ", 10);
- Person p2("QWE", 15);
- Person p3("QTR", 20);
- v1.push_back(&p1);
- v1.push_back(&p2);
- v1.push_back(&p3);
- for (vector<Person*>::iterator it = v1.begin(); it != v1.end(); it++)
- {
- cout << (*it)->m_Name << " " << (*it)->m_Age << endl;
- }
- }
-
- int main()
- {
- test01();
- test02();
- }
类似于实现二维数组,实际中用的很多。
遍历二维向量时,首先先用一个迭代器遍历各个向量的首元素,在循环内部再遍历该向量中的各个元素。
- #include<vector>
- #include<iostream>
- #include<algorithm>
- #include"string"
- using namespace std;
-
- 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);
- v2.push_back(i + 2);
- v3.push_back(i + 3);
- v4.push_back(i + 4);
- }
-
- //将小容器插入到大容器
- 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++)
- {
- //(*it)——容器vector<int>
- for (vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++)
- {
- cout << *vit << " ";
- }
- cout << endl;
- }
- }
-
- int main()
- {
- test01();
- }