# 标准模板库

**标准模板库**（Standard Template Library，STL）是一个泛型 C++ 库，包含容器类、算法和迭代器，提供了计算机科学中许多基本的算法和数据结构，其组件高度参数化<sup>[1](https://stl.boost.org/stl_introduction.html)</sup>。它由当时就职于惠普实验室（Hewlett-Packard Laboratories）的亚历山大·斯捷潘诺夫（Alexander Stepanov）与李梦（Meng Lee）设计，于 1994 年被接纳为 C++ 标准库的一部分<sup>[2](https://www.informit.com/store/generic-programming-and-the-stl-using-and-extending-9780201309560)</sup>。尼古拉·约苏蒂斯（Nicolai Josuttis）的权威参考书（其第二版涵盖 C++11）将 STL 本身——即容器、迭代器和函数对象——视为完整的 ANSI/ISO 标准库的核心<sup>[3](https://josuttis.com/libbook/libbook.html)</sup>。

| 关键事实 | 详情 |
|---|---|
| 定义 | 一个泛型 C++ 库，包含容器类、算法和迭代器，高度参数化<sup>[1](https://stl.boost.org/stl_introduction.html)</sup> |
| 起源 | 由斯捷潘诺夫和李在惠普实验室开发；1994 年被纳入 C++ 标准；惠普实现 SL95 于同年发布<sup>[2](https://www.informit.com/store/generic-programming-and-the-stl-using-and-extending-9780201309560)</sup> |
| 迭代器类别 | 五种：输入迭代器、输出迭代器、前向迭代器、双向迭代器和随机访问迭代器，其中前向、双向和随机访问迭代器构成一个精化序列，而输入和输出迭代器是独立的类别<sup>[4](https://www.stepanovpapers.com/STL/DOC.PDF)</sup> |
| 复杂度单位 | 所有容器复杂度要求均仅以对所包含对象的操作次数来表述<sup>[5](https://eel.is/c++draft/containers)</sup> |
| 无序容器 | 哈希结构，平均 O(1)、最坏情况 O(n) 复杂度内可查找<sup>[6](https://cppreference.dev/w/cpp/container)</sup> |
| 实测开销 | 在 GCC 11.3.0 基准测试中，std::map 插入 486 MB 数据耗时 2800 毫秒，而排序向量映射为 164 毫秒、86 MB，约慢 17 倍<sup>[7](https://github.com/tc3t/benchmarks/blob/master/map/mapPerformanceComparison.md)</sup> |
| 当前草案 | N5050 是 C++26 之前的最后一份工作草案，也是面向 C++29 的第一份草案<sup>[8](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2026/n5051.html)</sup> |

## 历史与起源

斯捷潘诺夫将最初将算法与数学理论相关联的直觉追溯到 1976 年<sup>[9](https://www.stepanovpapers.com/stl.pdf)</sup>。随后的发展脉络贯穿了与戴夫·穆瑟（Dave Musser）和迪帕克·卡普尔（Deepak Kapur）于 1979 至 1983 年构建的规约语言 Tecton；1984 至 1986 年与柯申鲍姆（Kershenbaum）在 Scheme 中的高阶编程；1986 年的 Ada 泛型库；以及 1987 至 1988 年的 UKL 标准组件<sup>[9](https://www.stepanovpapers.com/stl.pdf)</sup>。

**STL 本身。** 斯捷潘诺夫将 STL 本身追溯到 1993 年和 1994 年，与李梦和戴夫·穆瑟共同构建<sup>[9](https://www.stepanovpapers.com/stl.pdf)</sup>。它由斯捷潘诺夫和李在惠普实验室开发，大量基于斯捷潘诺夫和伦斯勒理工学院（Rensselaer Polytechnic Institute）的穆瑟的早期工作，并成为 ANSI/ISO C++ 标准库的主要组成部分<sup>[10](https://www.informit.com/store/c-plus-plus-standard-template-library-9780134376332)</sup>。基础文档是惠普实验室技术报告 HPL-94-34(R.1)，描述了一组结构良好、可无缝协作的泛型 C++ 组件<sup>[11](https://shiftleft.com/mirrors/www.hpl.hp.com/techreports/94/HPL-94-34.html)</sup>；修订版报告 HPL-95-11(R.1) 阐述了一个全面、可扩展的泛型组件库的设计，且不牺牲运行时效率<sup>[12](https://web.archive.org/web/20180508065835/http:/www.hpl.hp.com/techreports/95/HPL-95-11.html)</sup>。可免费获取的惠普实现 SL95 作为 STL 能力的演示，于 1994 年发布，同年该设计被接纳为 C++ 标准库的一部分<sup>[2](https://www.informit.com/store/generic-programming-and-the-stl-using-and-extending-9780201309560)</sup>。[计算机历史博物馆](https://www.edgepedia.org/zh/computer-history-museum)的 C++ 档案记录了在加利福尼亚州圣迭戈标准会议上被接纳的情况，加拿大滑铁卢也被提及，见于 ISO IEC JTC1/SC22/WG21 委员会文件<sup>[13](https://softwarepreservation.computerhistory.org/c_plus_plus/index.html)</sup>。

斯捷潘诺夫与马特·奥斯特恩（Matt Austern）和汉斯·伯姆（Hans Boehm）于 1995 至 1998 年继续以 SGI STL 推进这项工作<sup>[9](https://www.stepanovpapers.com/stl.pdf)</sup>。

## 架构：容器、迭代器、算法

马修·H·奥斯特恩（Matthew H. Austern）将 STL 呈现为一个泛型算法库，而非容器类库；容器的存在是为了让算法有操作的对象<sup>[2](https://www.informit.com/store/generic-programming-and-the-stl-using-and-extending-9780201309560)</sup>。两侧之间的连接是迭代器。每种容器类型都自带迭代器类型，容器的元素只能通过此类迭代器访问；因此迭代器充当将算法与其所操作数据连接起来的接口，使得算法可以仅依据迭代器来编写，而无需了解产生这些迭代器的容器。<sup>[14](https://stroustrup.com/hopl-almost-final.pdf)</sup>

**迭代器类别。** 原始 STL 文档根据定义在其上的操作定义了五类迭代器：输入迭代器、输出迭代器、前向迭代器、双向迭代器和随机访问迭代器<sup>[4](https://www.stepanovpapers.com/STL/DOC.PDF)</sup>。在容器一侧，容器的 C::iterator 必须是值类型为 T 的 LegacyForwardIterator，且 C::iterator 可转换为 C::const_iterator，容器管理为其元素分配的存储空间，提供成员函数以直接或通过迭代器访问它们<sup>[15](https://en.cppreference.com/w/cpp/named_req/Container)</sup>。原始 SGI 规范补充说，容器“拥有”其元素：存储在[容器](https://www.edgepedia.org/zh/container)中的元素的生命周期不能超过容器本身<sup>[16](https://stl.boost.org/Container.html)</sup>。

**分配器。** STL 中的所有容器都按分配器参数化，分配器是封装内存模型信息的对象，这简化了处理多种内存模型的工作<sup>[4](https://www.stepanovpapers.com/STL/DOC.PDF)</sup>。在现代标准中，每个可能需要分配或释放存储的组件——从 std::string 和 std::vector 到 std::shared_ptr——都通过分配器（Allocator）来完成，但有两个例外：std::array（自 C++11 起）和 std::inplace_vector（自 C++26 起）<sup>[17](https://cppreference.dev/w/cpp/named_req/Allocator)</sup>。许多分配器要求是可选的，因为所有分配器感知容器都通过 std::allocator_traits 间接访问分配器，后者提供默认实现；分配器可以是有状态的或无状态的<sup>[17](https://cppreference.dev/w/cpp/named_req/Allocator)</sup>。分配器感知容器使用 allocator_traits<allocator_type>::rebind_traits::construct 构造元素，并使用相应的 destroy 函数销毁它们<sup>[5](https://eel.is/c++draft/containers)</sup>。

## 容器及其保证

自 C++11 起，容器库包含三类容器：序列容器、关联容器和无序关联容器；C++11 之前有两类<sup>[6](https://cppreference.dev/w/cpp/container)</sup>。无序关联容器实现可快速搜索的无序（哈希）数据结构，平均 O(1)、最坏情况 O(n) 复杂度<sup>[6](https://cppreference.dev/w/cpp/container)</sup>。

标准以刻意抽象的单位来表述复杂度：容器条款中的所有复杂度要求均仅以对所包含对象的操作次数来表述<sup>[5](https://eel.is/c++draft/containers)</sup>。

**迭代器失效。** 除非另有规定，调用容器成员函数不得使指向该容器内对象的迭代器失效，也不得改变其值<sup>[5](https://eel.is/c++draft/containers)</sup>。另有规定的情况因容器而异，因为底层数据结构不同：

- 对于 std::vector，若容量改变，插入使所有迭代器和引用失效；否则，插入使插入点及之后的迭代器和引用失效，擦除使被修改元素及之后的迭代器和引用失效；对于 std::list 和 std::forward_list，擦除仅使被擦除元素的迭代器和引用失效<sup>[6](https://cppreference.dev/w/cpp/container)</sup>。
- std::set::end 永不失效，std::unordered_set::end 仅在重哈希时失效（自 C++11 起），而 std::vector::end 总是因修改而失效，因为它总是在被修改元素之后<sup>[6](https://cppreference.dev/w/cpp/container)</sup>。对于无序容器一般而言，导致重哈希的插入使所有迭代器失效，但不使引用失效<sup>[6](https://cppreference.dev/w/cpp/container)</sup>。
- 擦除 std::deque 的最后一个元素会使尾后迭代器失效，即使该迭代器不指向从容器中移除的任何元素。<sup>[6](https://cppreference.dev/w/cpp/container)</sup>
- 排序向量容器 std::flat_map 和 std::flat_set 在插入或擦除元素时可能使迭代器和引用失效，这与基于节点的容器不同<sup>[18](https://mreid-consulting.github.io/cpp-lessons/lessons/10-stl-containers-tour.html)</sup>。

## 数据说话

基准测试表明，实测行为可能与大 O 预期相去甚远。在 GCC 11.3.0 配 libstdc++ 的基准测试中，std::map 插入耗时 2800 毫秒，而排序向量映射为 164 毫秒（约慢 17 倍），占用 486 MB 对 86 MB，整对象删除耗时 118 毫秒，而扁平映射约为 0.15 毫秒<sup>[7](https://github.com/tc3t/benchmarks/blob/master/map/mapPerformanceComparison.md)</sup>。在同一基准测试中，使用 reserve() 的 std::unordered_map 插入耗时 195 毫秒、删除耗时 56.2 毫秒，接近扁平映射的性能，但占用 409 MB，而 boost::flat_map 为 86 MB<sup>[7](https://github.com/tc3t/benchmarks/blob/master/map/mapPerformanceComparison.md)</sup>。

**缓存局部性。** 典型的查找开销说明了其机制：std::map 约为 100 至 200 纳秒，其树结构意味着约 10 层冷内存访问；std::unordered_map 为 60 至 120 纳秒；用 lower_bound 搜索的向量为 40 至 80 纳秒；std::flat_map 为 30 至 60 纳秒。迭代方面，std::vector 每元素约 0.5 至 2 纳秒，而 std::map 或 std::unordered_map 为 80 至 100 纳秒<sup>[18](https://mreid-consulting.github.io/cpp-lessons/lessons/10-stl-containers-tour.html)</sup>。

Chromium 的工程指南量化了内存方面：std::map 和 std::set 每项占 32 字节，空容器大小为 16 字节；std::unordered_map 和 std::unordered_set 每项占 16 至 24 字节，空大小为 128 字节；base::flat_map 每项占 0 字节，空大小为 24 字节<sup>[19](https://chromium.googlesource.com/chromium/src/+/69c721ec714b0650dec773173ef4be5a25347a49/base/containers/)</sup>。由于 Chrome 中的大多数映射都很小，Chromium 推荐使用 flat_map，尽管其插入和删除为 O(n)，导致 O(n²) 的构造；在数十项以内，插入仍具竞争力<sup>[19](https://chromium.googlesource.com/chromium/src/+/69c721ec714b0650dec773173ef4be5a25347a49/base/containers/)</sup>。

甚至空容器的大小也因实现而异。Boost.Container 的对比表列出 std::deque 在 libstdc++ 中为 40/80 字节（32 位/64 位），libc++ 中为 24/48，微软 STL 中为 20/40，Boost.Container 中为 16/32；微软 STL 对 list 和 set/map 使用堆分配的哨兵，使得 sizeof 更小但构造时需要一次分配<sup>[20](https://www.stage.boost.org/doc/libs/latest/doc/html/container/some_comparisons.html)</sup>。

## 与其他方案比较

**EASTL。** EASTL 是电子艺界（Electronic Arts）的 STL 变体，作为论文 N2271 提交给 C++ 委员会，其提供的容器、迭代器和算法在接口和行为上与 std STL 版本相同，只有一个例外：分配器，它使用了不同且更简单的规范<sup>[21](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html)</sup>。其设计文档指出主要偏差为：简化且更灵活的自定义分配方案，所有容器仅使用一个分配器类接口且用户可访问；扩展容器和算法；以及为游戏开发设计的优化<sup>[22](https://github.com/electronicarts/EASTL/blob/master/doc/Design.md)</sup>。扩展约有 30 个，包括 intrusive_list、vector_map、fixed_hash_set、slist、ring_buffer、radix_sort 和 has_trivial_relocate<sup>[21](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html)</sup>。

**业界分歧。** 在大多数情况下，Chromium 的容器指南建议不要使用 std::unordered_set 和 std::unordered_map：查找通常相比 std::map 没有明显的速度优势，插入稍慢，且容器消耗的内存明显更多<sup>[19](https://chromium.googlesource.com/chromium/src/+/69c721ec714b0650dec773173ef4be5a25347a49/base/containers/)</sup>。

## 近期发展

作为当前使用的工作草案，N5050 构成 C++26 之前的最后一份草案和面向 C++29 的第一份草案；它取代 N5046，并作为 C++26 国际标准草案的基础<sup>[8](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2026/n5051.html)</sup>。

C++26 库特性及其实现状态包括：用于文本编码识别的 std::text_encoding（GCC 14）、std::function_ref（GCC 16、libc++ 24）、[饱和算术](https://www.edgepedia.org/zh/saturation-arithmetic)（GCC 14/16、libc++ 18/23、MSVC 16.0.0 部分支持）、std::submdspan（GCC 16）、constexpr 稳定排序（GCC 15、libc++ 21），以及 std::ratio 上的新 SI 前缀<sup>[23](https://cppreference.com/cpp/26)</sup>。libc++ 按论文跟踪 C++26 完成情况；例如 P3029“改进 mdspan 的 CTAD”在 2024 年 3 月东京会议上通过，已标记为完成，状态表列出 LLVM 19，备注栏说明自 LLVM 23 起作为针对 C++20 的缺陷报告实现<sup>[24](https://libcxx.llvm.org/Status/Cxx26.html)</sup>。

## 批评与实际限制

各方来源对 std::unordered_map 意见不一。Chromium 的指南称应避免使用它，因为查询性能很少比 std::map 好多少，插入稍差，且内存开销高<sup>[19](https://chromium.googlesource.com/chromium/src/+/69c721ec714b0650dec773173ef4be5a25347a49/base/containers/)</sup>。GCC 基准测试在速度上得出相反结论：使用 reserve 的 std::unordered_map 插入耗时 195 毫秒，而 std::map 为 2800 毫秒，接近扁平映射的性能，尽管仍占用 409 MB 对 86 MB<sup>[7](https://github.com/tc3t/benchmarks/blob/master/map/mapPerformanceComparison.md)</sup>。同一基准测试发现情况因实现而异：在 MSVC 上，std::unordered_map 的表现与 std::map 相似，reserve() 有重大影响，而在 GCC 和 Clang 上，它与扁平映射相当，reserve() 的影响小得多<sup>[7](https://github.com/tc3t/benchmarks/blob/master/map/mapPerformanceComparison.md)</sup>。

## References

1. [The Standard Template Library: Introduction, SGI STL documentation (hosted by Boost)](https://stl.boost.org/stl_introduction.html)
2. [Matthew H. Austern, Generic Programming and the STL, Addison-Wesley, InformIT page](https://www.informit.com/store/generic-programming-and-the-stl-using-and-extending-9780201309560)
3. [Nicolai Josuttis, The C++ Standard Library: A Tutorial and Reference, official book site](https://josuttis.com/libbook/libbook.html)
4. [Alexander Stepanov and Meng Lee, The Standard Template Library, HP Labs STL documentation](https://www.stepanovpapers.com/STL/DOC.PDF)
5. [C++ working draft, [containers], ISO C++ standard draft](https://eel.is/c++draft/containers)
6. [Containers library, cppreference](https://cppreference.dev/w/cpp/container)
7. [mapPerformanceComparison.md, tc3t benchmarks, GitHub](https://github.com/tc3t/benchmarks/blob/master/map/mapPerformanceComparison.md)
8. [N5051, Editors' Report, WG21](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2026/n5051.html)
9. [Alexander Stepanov, STL and Its Design Principles](https://www.stepanovpapers.com/stl.pdf)
10. [Stepanov, Lee, and Musser, The C++ Standard Template Library, InformIT page](https://www.informit.com/store/c-plus-plus-standard-template-library-9780134376332)
11. [HP Labs technical report HPL-94-34(R.1), The Standard Template Library](https://shiftleft.com/mirrors/www.hpl.hp.com/techreports/94/HPL-94-34.html)
12. [HP Labs technical report HPL-95-11(R.1), The Standard Template Library, archived](https://web.archive.org/web/20180508065835/http:/www.hpl.hp.com/techreports/95/HPL-95-11.html)
13. [C++ Historical Sources Archive, Computer History Museum](https://softwarepreservation.computerhistory.org/c_plus_plus/index.html)
14. [Bjarne Stroustrup, Evolving a language in and for the real world: C++ 1991-2006, ACM HOPL](https://stroustrup.com/hopl-almost-final.pdf)
15. [C++ named requirements: Container, cppreference](https://en.cppreference.com/w/cpp/named_req/Container)
16. [Container, SGI STL documentation (hosted by Boost)](https://stl.boost.org/Container.html)
17. [C++ named requirements: Allocator, cppreference](https://cppreference.dev/w/cpp/named_req/Allocator)
18. [The STL Containers, Honestly, mreid-consulting](https://mreid-consulting.github.io/cpp-lessons/lessons/10-stl-containers-tour.html)
19. [base/containers, Chromium source documentation](https://chromium.googlesource.com/chromium/src/+/69c721ec714b0650dec773173ef4be5a25347a49/base/containers/)
20. [Boost.Container: attributes and comparison with standard library implementations](https://www.stage.boost.org/doc/libs/latest/doc/html/container/some_comparisons.html)
21. [EASTL, ISO C++ committee paper N2271](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html)
22. [EASTL Design documentation, Electronic Arts, GitHub](https://github.com/electronicarts/EASTL/blob/master/doc/Design.md)
23. [C++26, cppreference.com](https://cppreference.com/cpp/26)
24. [libc++ C++26 Status](https://libcxx.llvm.org/Status/Cxx26.html)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026; Oct 21, 2026; Oct 25, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Development tools and collaboration infrastructure › Frameworks, libraries, and APIs › Mobile and platform frameworks

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/standard-template-library

English original: https://www.edgepedia.org/standard-template-library
