# C++ 标准库

C++ 标准库是 ISO C++ 标准（ISO/IEC 14882）中规定的一组头文件、组件和行为保证。它提供容器、迭代器、范围、算法、字符串、数值计算、输入/输出、诊断、内存管理、工具和并发支持，几乎全部声明在命名空间 `std` 中。<sup>[1](https://eel.is/c++draft/library)</sup><sup> • </sup><sup>[2](https://cppreference.com/cpp/standard_library)</sup> 该库并非一份单独的文件：它定义在语言标准本身之内，约占 C++20 标准文本的四分之三。<sup>[3](https://stroustrup.com/hopl20main-p5-p-bfc9cd4--final.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义文件 | ISO/IEC 14882；标准库约占 C++20 标准的 3/4<sup>[3](https://stroustrup.com/hopl20main-p5-p-bfc9cd4--final.pdf)</sup> |
| 命名空间 | 除 `operator new` 和 `operator delete` 外，所有库实体都位于 `std` 或其嵌套命名空间中<sup>[2](https://cppreference.com/cpp/standard_library)</sup> |
| 实现类型 | 宿主实现提供所有必需的头文件；独立实现提供由实现定义的子集<sup>[1](https://eel.is/c++draft/library)</sup> |
| 主要实现 | libstdc++（GPL v3 + 运行时例外）、libc++（Apache 2.0 + LLVM 例外）、MSVC STL（Apache 2.0 + LLVM 例外）<sup>[4](https://cpp.wyattau.com/enviroment_and_toolchain/1_compiler_and_standards/3_standard_library_implementation/)</sup><sup> • </sup><sup>[5](https://learn.microsoft.com/en-us/cpp/standard-library/cpp-standard-library-overview?view=msvc-170)</sup> |
| 覆盖面 | libc++ 是 macOS、iOS、watchOS、tvOS、Android 和 FreeBSD 上的默认库，估计日活跃用户基数超过 10 亿<sup>[6](https://libcxx.llvm.org/)</sup> |
| 采用率（2024 年调查） | 90.81% 的受访者“基本上”使用 C++11 特性；20.02% 报告完全使用了 `expected` 和 `md_span` 等 C++23 特性<sup>[7](https://isocpp.org/files/papers/CppDevSurvey-2024-summary.pdf)</sup> |
| 编译开销 | 包含整个 C++23 库在 GCC 下展开为 128,846 行（净编译时间 1,175 毫秒），在 Clang 下为 87,186 行（1,598 毫秒）<sup>[8](https://github.com/whatchamacallem/cxx-std-include-bench/)</sup> |
| 安全方向 | C++26 标准库强化增加了运行时检查的强化前置条件，用于防范容器、`span`、`mdspan`、`string` 和 `string_view` 中的越界访问<sup>[9](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3471r1.html)</sup> |

## 结构与组件

库的每个元素都在头文件中声明或定义，头文件集合随标准的每次修订而增长。<sup>[10](https://eel.is/c++draft/headers)</sup> C++11 增加了 `<condition_variable>`、`<forward_list>`、`<unordered_map>`、`<type_traits>` 等；C++17 增加了 `<memory_resource>` 和 `<string_view>`；C++20 增加了 `<stop_token>` 和 `<source_location>`；C++23 增加了 `<flat_map>` 和 `<flat_set>`；C++26 增加了 `<hazard_pointer>`、`<inplace_vector>` 和 `<text_encoding>`。<sup>[2](https://cppreference.com/cpp/standard_library)</sup> [正则表达式](https://www.edgepedia.org/zh/regular-expression)匹配随 C++11 到来，文本格式化随 C++20 到来，文本编码识别随 C++26 到来。<sup>[2](https://cppreference.com/cpp/standard_library)</sup>

该库按命名的子库组织：语言支持、诊断、内存管理、通用工具、容器/迭代器/范围/算法组、字符串、文本处理、数值计算和执行支持。<sup>[2](https://cppreference.com/cpp/standard_library)</sup> 一项关于库使用情况的实证研究统计了 11 个子库，其中 8 个在 C++11 之前就已确立（容器、迭代器、算法、工具、字符串、数值计算、输入/输出、本地化），另有 3 个由 C++11 增加（正则表达式、原子操作、线程支持）。<sup>[11](https://ksiresearch.org/seke/seke15paper/seke15paper_9.pdf)</sup> 标准本身将容器、迭代器、范围和算法条款描述为提供对最广泛使用的算法和数据结构子集的访问。<sup>[1](https://eel.is/c++draft/library)</sup>

自 C++23 起，一个名为 `std` 的模块导出可导入库头文件所提供的命名空间 `std` 中的声明，为文本式 `#include` 提供了替代方案。<sup>[2](https://cppreference.com/cpp/standard_library)</sup>

## 设计与扩展机制

该库最初的扩展点是分配器。亚历山大·斯特潘诺夫（Alexander Stepanov）的标准模板库通过为分配器（封装内存模型信息的对象）提供一组标准要求来解决跨内存模型的可移植性问题，所有 STL 容器都以分配器为参数；这一设计延续到了标准化库中。<sup>[12](https://www.stepanovpapers.com/STL/DOC.PDF)</sup>

## 保证：复杂度、异常与安全

标准区分宿主实现和独立实现。宿主实现支持所有必需的标准库头文件；独立实现有一组由实现定义的头文件。<sup>[1](https://eel.is/c++draft/library)</sup><sup> • </sup><sup>[5](https://learn.microsoft.com/en-us/cpp/standard-library/cpp-standard-library-overview?view=msvc-170)</sup>

在非强化实现中，诸如 `vector::operator[]` 之类的容器访问器不进行范围检查；越界访问是未定义行为。C++26 通过标准库强化来解决这个问题。强化提案侧重于防止越界内存访问的强化前置条件，针对序列容器、`std::span`、`std::mdspan`、`std::string`、`std::string_view` 的访问器，`pop_back` 等修改器，以及 `optional` 和 `expected` 的访问器。<sup>[9](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3471r1.html)</sup> 在强化实现中，强化前置条件在运行时检查，违反即为具有终止语义的契约断言，在该函数任何其他可观察副作用之前求值；这些检查不能用 `ignore` 契约语义绕过。<sup>[2](https://cppreference.com/cpp/standard_library)</sup><sup> • </sup><sup>[9](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3471r1.html)</sup> 强化设计为避免 ABI（应用二进制接口：已编译代码的二进制级兼容契约）变更并适合生产使用，请求强化的机制由实现定义，例如 `-fhardened` 之类的编译器标志或 `-D_LIBCPP_HARDENING_MODE=` 之类的宏。<sup>[9](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3471r1.html)</sup>

## 实现

三种实现占据主导地位。libstdc++ 随 GCC 以 GPL v3 加运行时例外发布；libc++ 随 Clang 以 Apache 2.0 加 LLVM 例外发布；微软的 MSVC STL 以 [Apache License](https://www.edgepedia.org/zh/apache-license) 2.0 加 LLVM 例外发布。<sup>[4](https://cpp.wyattau.com/enviroment_and_toolchain/1_compiler_and_standards/3_standard_library_implementation/)</sup><sup> • </sup><sup>[5](https://learn.microsoft.com/en-us/cpp/standard-library/cpp-standard-library-overview?view=msvc-170)</sup> 微软的发行版还额外包含 `<hash_map>` 和 `<hash_set>`，这些头文件不是 C++ 标准所要求的。<sup>[5](https://learn.microsoft.com/en-us/cpp/standard-library/cpp-standard-library-overview?view=msvc-170)</sup>

**ABI 和布局存在明显差异。** libstdc++ 支持由 `_GLIBCXX_USE_CXX11_ABI` 选择的双 ABI；libc++ 通过 `__1` 内联命名空间对其符号进行版本管理；MSVC STL 将其 ABI 与 [Visual Studio](https://www.edgepedia.org/zh/visual-studio) 版本对齐。添加检查的调试模式（`_GLIBCXX_DEBUG`、`_ITERATOR_DEBUG_LEVEL`）会破坏 ABI，而 `_LIBCPP_HARDENING_MODE` 不会。<sup>[4](https://cpp.wyattau.com/enviroment_and_toolchain/1_compiler_and_standards/3_standard_library_implementation/)</sup> 小字符串优化也不同：libc++ 在 64 位平台上为 `std::string` 使用 22 字节的 SSO 缓冲区，而 libstdc++ 和 MSVC STL 为 15 字节，使得 `sizeof(std::string)` 分别为 24 字节和 32 字节。<sup>[4](https://cpp.wyattau.com/enviroment_and_toolchain/1_compiler_and_standards/3_standard_library_implementation/)</sup>

**完整性因特性而异。** 截至 LLVM 18，libc++ 未提供并行算法，而 libstdc++ 提供了；MSVC STL 也缺少这些算法。<sup>[4](https://cpp.wyattau.com/enviroment_and_toolchain/1_compiler_and_standards/3_standard_library_implementation/)</sup> libc++ 完全实现了 C++11 和 C++14，C++17、C++20、C++23、C++26 和 C++29 的支持标记为进行中。<sup>[6](https://libcxx.llvm.org/)</sup> 对于 GCC 14、Clang 18 和 VS 2022 17.10+，`std::print`/`println`、`std::expected`、`std::flat_map`/`flat_set`、`std::mdspan`、`std::move_only_function`、`std::stacktrace`、`std::out_ptr`/`inout_ptr` 和 `std::byteswap` 均可用；`std::generator` 仅在 libc++ 中完整，`std::ranges::to` 在 MSVC STL 中部分可用。<sup>[4](https://cpp.wyattau.com/enviroment_and_toolchain/1_compiler_and_standards/3_standard_library_implementation/)</sup>

## 数据一览

**编译期开销是可测量的。** 包含整个 C++23 标准库在 GCC 下展开为 128,846 行预处理代码，净编译时间 1,175 毫秒，在 Clang 下为 87,186 行，耗时 1,598 毫秒，而 C++11 库分别为 75,473 行 / 432 毫秒和 63,666 行 / 759 毫秒。<sup>[8](https://github.com/whatchamacallem/cxx-std-include-bench/)</sup> 对于 C++23，预编译头将全库包含开销在 GCC 下降至 367 毫秒，在 Clang 下降至 138 毫秒。<sup>[8](https://github.com/whatchamacallem/cxx-std-include-bench/)</sup> 在 libc++ 内部，仅预处理后的 `<vector>` 就展开为 2,046,574 字节，`<iostream>` 为 1,906,216 字节。<sup>[13](https://github.com/llvm/llvm-project/issues/80196)</sup>

**实现之间的运行时差异在微基准测试中很小。** libc++（Clang 17）从 `const char*` 构造字符串耗时 12 纳秒，而 libstdc++ 为 15 纳秒，MSVC STL 为 16 纳秒，SSO 路径命中分别为 5 纳秒、8 纳秒和 7 纳秒；一项向量基准测试显示 libstdc++（GCC 13）推入 100 万个元素耗时 12 毫秒，而 libc++ 为 11 毫秒，MSVC STL 为 13 毫秒。<sup>[4](https://cpp.wyattau.com/enviroment_and_toolchain/1_compiler_and_standards/3_standard_library_implementation/)</sup>

**采用率与标准年龄同步。** 在 2024 年 ISO C++ 开发者调查中，90.81% 的受访者报告“基本上”使用了 C++11 特性（auto、移动语义、`shared_ptr`、lambda），77.79% 报告完全使用了 C++17 特性，31.61% 完全使用了概念、协程和模块等 C++20 特性，20.02% 完全使用了 `expected` 和 `md_span` 等 C++23 特性。<sup>[7](https://isocpp.org/files/papers/CppDevSurvey-2024-summary.pdf)</sup>

## 与 Boost、Qt 和平台框架的比较

官方名称是 C++ 标准库，如 ISO 14882 所定义。“STL”这一名称最初指亚历山大·斯特潘诺夫的标准模板库；当该库的部分内容被纳入 C++ 标准库时，它们与 ISO C 运行时库、一些 Boost 库组件和其他特性一起加入其中。<sup>[5](https://learn.microsoft.com/en-us/cpp/standard-library/cpp-standard-library-overview?view=msvc-170)</sup>

## 历史

现代库可追溯到一次标准会议上的演示。在 1993 年 11 月的圣何塞会议上，当时受雇于惠普的亚历山大·斯特潘诺夫做了一场关于泛型编程的演示，大量使用了模板；到次年 3 月的圣迭戈会议时，他已将他和李蒙（Meng Lee）的标准模板库完善到委员会准备认真考虑的程度，即使这意味着推迟标准的完成。<sup>[14](https://stroustrup.com/cuj_interview.html)</sup> 斯特潘诺夫自己的开发时间线将 1986 年与戴夫·穆瑟（Dave Musser）合作的 Ada 泛型库、1993 至 1994 年与李蒙和戴夫·穆瑟合作的 STL，以及 1995 至 1998 年与马特·奥斯特恩（Matt Austern）和汉斯·博姆（Hans Boehm）合作的 SGI STL 列入其中。<sup>[15](https://www.stepanovpapers.com/stl.pdf)</sup> 获得批准后，STL 与 iostreams 一起成为标准 C++ 库的核心。<sup>[14](https://stroustrup.com/cuj_interview.html)</sup>

C++ 的创造者本贾尼·斯特劳斯特卢普（Bjarne Stroustrup）将 STL 的容器、迭代器和算法框架誉为标准库中最重要、远超其他部分的组件，称它及其开创的泛型编程技术拯救了 C++，使其成为一门活着的现代语言。<sup>[3](https://stroustrup.com/hopl20main-p5-p-bfc9cd4--final.pdf)</sup> C++11 对库进行了大幅现代化：约祖蒂斯（Josuttis）的第二版参考手册涵盖了纳入 C++11 的库，其整体内容比第一版增加了约 50%，增加了类型特征、元组、新容器、正则表达式、智能指针、随机数、时钟和并发。<sup>[16](http://www.cppstdlib.com/cppstdlib.html)</sup> 在该修订中，C++98 早期的独占所有权指针 `auto_ptr` 被共享所有权的 `shared_ptr` 和独占所有权的 `unique_ptr` 所取代。<sup>[3](https://stroustrup.com/hopl20main-p5-p-bfc9cd4--final.pdf)</sup> ISO C++ 委员会本身成立于 1991 年，制定了 1998 年 ISO 标准，这是库后续发展的基线。<sup>[17](https://dl.acm.org/doi/10.1145/1238844.1238848)</sup>

## 近期发展

C++23 最终确定，新增内容包括 `<flat_map>`、`<flat_set>` 和 `std` 模块。<sup>[2](https://cppreference.com/cpp/standard_library)</sup> 对于 C++26，N5050 是最终草案，也是 C++29 的初始草案，构成 C++26 国际标准草案的基础。<sup>[18](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2026/n5051.html)</sup> C++26 采纳了标准库强化，为容器、`span`、`mdspan`、`string` 和 `string_view` 的某些潜在越界访问增加了强化前置条件，在强化实现中违反这些条件会在运行时检查并具有终止语义。<sup>[9](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3471r1.html)</sup><sup> • </sup><sup>[2](https://cppreference.com/cpp/standard_library)</sup> 在实现方面，GCC 14、Clang 18 和 VS 2022 17.10+ 共同覆盖了上述 C++23 特性集，剩余差距集中在 `std::generator` 和 `std::ranges::to`。<sup>[4](https://cpp.wyattau.com/enviroment_and_toolchain/1_compiler_and_standards/3_standard_library_implementation/)</sup>

## References

1. [C++ working draft, [library], WG21](https://eel.is/c++draft/library)
2. [C++ Standard Library, cppreference.com](https://cppreference.com/cpp/standard_library)
3. [Bjarne Stroustrup, Thriving in a Crowded and Changing World: C++ 2006–2020, HOPL IV](https://stroustrup.com/hopl20main-p5-p-bfc9cd4--final.pdf)
4. [Standard Library Implementation, cpp.wyattau.com](https://cpp.wyattau.com/enviroment_and_toolchain/1_compiler_and_standards/3_standard_library_implementation/)
5. [C++ Standard Library Overview (STL), Microsoft Learn](https://learn.microsoft.com/en-us/cpp/standard-library/cpp-standard-library-overview?view=msvc-170)
6. [libc++ C++ Standard Library documentation, LLVM](https://libcxx.llvm.org/)
7. [2024 Annual C++ Developer Survey "Lite", Standard C++ Foundation](https://isocpp.org/files/papers/CppDevSurvey-2024-summary.pdf)
8. [cxx-std-include-bench, GitHub](https://github.com/whatchamacallem/cxx-std-include-bench/)
9. [P3471R1: Standard Library Hardening, WG21](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3471r1.html)
10. [C++ working draft, [headers], WG21](https://eel.is/c++draft/headers)
11. [How do developers use C++ libraries? An empirical study, SEKE 2015](https://ksiresearch.org/seke/seke15paper/seke15paper_9.pdf)
12. [The Standard Template Library, Alexander Stepanov](https://www.stepanovpapers.com/STL/DOC.PDF)
13. [[libc++] Extreme preprocessed size of core headers, LLVM issue 80196](https://github.com/llvm/llvm-project/issues/80196)
14. [C++: The Making of a Standard; Stroustrup Interviews](https://stroustrup.com/cuj_interview.html)
15. [STL and Its Design Principles, Alexander Stepanov](https://www.stepanovpapers.com/stl.pdf)
16. [The C++ Standard Library, 2nd Edition, Nicolai Josuttis](http://www.cppstdlib.com/cppstdlib.html)
17. [Evolving a language in and for the real world: C++ 1991–2006, HOPL III, ACM](https://dl.acm.org/doi/10.1145/1238844.1238848)
18. [N5051 Editors' Report, WG21](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2026/n5051.html)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 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/c-standard-library-ep2gtn4f1q

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