STL大体分为六大组件,分别是:容器、算法、迭代器、仿函数、适配器(配接器)、空间配置器。
STL容器就是将运用最广泛的一些数据结构实现出来。 常用的数据结构:数组、链表、树、栈、队列、集合、映射表 等。
这些容器分为序列式容器和关联式容器两种:
有限的步骤,解决逻辑或数学上的问题,这一门学科我们叫做算法(Algorithms)。
算法分为:质变算法和非质变算法。
提供一种方法,使之能够依序寻访某个容器所含的各个元素,而又无需暴露该容器的内部表示方式。 每个容器都有自己专属的迭代器。 迭代器使用非常类似于指针,初学阶段我们可以先理解迭代器为指针。
迭代器种类:
| 种类 | 功能 | 支持运算 |
|---|---|---|
| 输入迭代器 | 对数据的只读访问 | 只读,支持++、==、!= |
| 输出迭代器 | 对数据的只写访问 | 只写,支持++ |
| 前向迭代器 | 读写操作,并能向前推进迭代器 | 读写,支持++、==、!= |
| 双向迭代器 | 读写操作,并能向前和向后操作 | 读写,支持++、– |
| 随机访问迭代器 | 读写操作,可以以跳跃的方式访问任意数据,功能最强的迭代器 | 读写,支持++、–、[n]、-n、<、⇐、>、>= |
常用的容器中迭代器种类为双向迭代器,和随机访问迭代器。
了解STL中容器、算法、迭代器概念之后,我们利用代码感受STL的魅力。 STL中最常用的容器为 `Vector`,可以理解为数组,下面我们将学习如何向这个容器中插入数据、并遍历这个容器。
容器: `vector` 算法: `for_each` 迭代器: `vector<int>::iterator`
示例:
#include <vector> #include <algorithm> #include <iostream> using namespace std; void MyPrint(int val) { cout << val << endl; } void test01() { // 创建vector容器对象,并且通过模板参数指定容器中存放的数据的类型 vector<int> v; // 向容器中放数据 v.push_back(10); v.push_back(20); v.push_back(30); v.push_back(40); // 每一个容器都有自己的迭代器,迭代器是用来遍历容器中的元素 // 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 it = v.begin(); it != v.end(); it++) { cout << *it << endl; } cout << endl; // 第三种遍历方式: // 使用STL提供标准遍历算法 头文件 algorithm for_each(v.begin(), v.end(), MyPrint); } int main() { test01(); system("pause"); return 0; }
学习目标: vector中存放自定义数据类型,并打印输出。
示例:
#include <vector> #include <string> #include <iostream> using namespace std; // 自定义数据类型 class Person { public: Person(string name, int age) { mName = name; mAge = age; } public: string mName; int mAge; }; // 存放对象 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 << "Name:" << (*it).mName << " Age:" << (*it).mAge << endl; // 或者使用箭头操作符 // cout << "Name:" << it->mName << " Age:" << it->mAge << endl; } } // 存放对象指针 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++) { Person * p = (*it); cout << "Name:" << p->mName << " Age:" << (*it)->mAge << endl; } } int main() { test01(); test02(); system("pause"); return 0; }
学习目标: 容器中嵌套容器,我们将所有数据进行遍历输出。
示例:
#include <vector> #include <iostream> 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); } // 将容器元素插入到vector v中 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(); system("pause"); return 0; }