C++ 标准库头文件手册
本文档按功能分类整理 C++ 标准库头文件,涵盖 C++98 至 C++23 的主要特性。每个头文件简要说明其用途和常用组件,并标注引入版本(未标注则表示 C++98 已有)。
1. 语言支持
| 头文件 |
说明 |
常用组件 |
版本 |
<cstddef> |
定义与内存相关的类型和宏 |
std::size_t, std::ptrdiff_t, NULL, offsetof |
|
<limits> |
提供算术类型极限信息 |
std::numeric_limits |
|
<climits> |
C 风格整数类型极限宏 |
INT_MAX, CHAR_BIT 等 |
|
<cfloat> |
C 风格浮点类型极限宏 |
DBL_MIN, FLT_MAX 等 |
|
<cstdint> |
定宽整数类型 |
int32_t, uint64_t 等 |
C++11 |
<cstdlib> |
通用工具:内存管理、随机数、环境等 |
std::malloc, std::free, std::rand, std::exit |
|
<csetjmp> |
非局部跳转 |
std::setjmp, std::longjmp |
|
<csignal> |
信号处理 |
std::signal, std::raise |
|
<cstdarg> |
可变参数处理 |
va_list, va_start, va_arg, va_end |
|
<cassert> |
断言宏 |
assert |
|
<exception> |
异常处理基础 |
std::exception, std::terminate |
|
<stdexcept> |
标准异常类 |
std::logic_error, std::runtime_error 及其派生 |
|
<new> |
动态内存管理 |
new 表达式, std::bad_alloc, placement new |
|
<typeinfo> |
运行时类型信息 |
std::type_info, std::bad_cast |
|
<initializer_list> |
初始化列表支持 |
std::initializer_list |
C++11 |
<compare> |
三路比较运算符支持 |
std::strong_ordering, std::weak_ordering, std::partial_ordering |
C++20 |
<source_location> |
源代码位置信息 |
std::source_location |
C++20 |
<version> |
库版本特性测试宏 |
__cpp_lib_* 宏 |
C++20 |
2. 输入输出
| 头文件 |
说明 |
常用组件 |
版本 |
<iostream> |
标准输入输出流 |
std::cin, std::cout, std::cerr, std::clog |
|
<fstream> |
文件输入输出流 |
std::ifstream, std::ofstream, std::fstream |
|
<sstream> |
字符串流 |
std::istringstream, std::ostringstream, std::stringstream |
|
<iomanip> |
输入输出操纵符 |
std::setw, std::setprecision, std::setfill |
|
<ios> |
输入输出基类 |
std::ios_base, std::ios |
|
<iosfwd> |
输入输出类的前向声明 |
std::iostream 等的前向声明 |
|
<streambuf> |
流缓冲区类 |
std::streambuf |
|
<cstdio> |
C 风格输入输出 |
std::printf, std::scanf, std::FILE, std::fopen |
|
<spanstream> |
基于固定缓冲区的字符串流 |
std::ispanstream, std::ospanstream |
C++23 |
<syncstream> |
同步输出流(线程安全) |
std::osyncstream |
C++20 |
3. 字符串
| 头文件 |
说明 |
常用组件 |
版本 |
<string> |
字符串类模板 |
std::string, std::wstring, std::u8string (C++20), std::u16string, std::u32string |
|
<string_view> |
字符串视图(非拥有) |
std::string_view, std::wstring_view 等 |
C++17 |
<cstring> |
C 风格字符串函数 |
std::strlen, std::strcpy, std::strcmp |
|
<cctype> |
字符分类与转换 |
std::isalpha, std::isdigit, std::tolower |
|
<cwchar> |
宽字符 C 函数 |
std::wcslen, std::wcscpy |
|
<cwctype> |
宽字符分类 |
std::iswalpha, std::iswdigit |
|
<charconv> |
数值与字符转换(高性能、无本地化) |
std::to_chars, std::from_chars |
C++17 |
<format> |
格式化输出库 |
std::format, std::formatter |
C++20 |
<text_encoding> |
文本编码标识 |
std::text_encoding |
C++26(预览) |
4. 容器
| 头文件 |
说明 |
常用组件 |
版本 |
<vector> |
动态数组 |
std::vector |
|
<deque> |
双端队列 |
std::deque |
|
<list> |
双向链表 |
std::list |
|
<forward_list> |
单向链表 |
std::forward_list |
C++11 |
<array> |
固定大小数组 |
std::array |
C++11 |
<span> |
连续序列视图(非拥有) |
std::span |
C++20 |
<mdspan> |
多维数组视图 |
std::mdspan |
C++23 |
<queue> |
队列适配器 |
std::queue, std::priority_queue |
|
<stack> |
栈适配器 |
std::stack |
|
<map> |
关联容器(键值对,有序) |
std::map, std::multimap |
|
<set> |
关联容器(键集合,有序) |
std::set, std::multiset |
|
<unordered_map> |
无序关联容器(哈希表) |
std::unordered_map, std::unordered_multimap |
C++11 |
<unordered_set> |
无序关联容器(哈希集合) |
std::unordered_set, std::unordered_multiset |
C++11 |
<flat_map> |
有序容器(连续存储,扁平映射) |
std::flat_map, std::flat_multimap |
C++23 |
<flat_set> |
有序容器(连续存储,扁平集合) |
std::flat_set, std::flat_multiset |
C++23 |
5. 算法
| 头文件 |
说明 |
常用组件 |
版本 |
<algorithm> |
通用算法 |
std::sort, std::find, std::copy, std::transform, std::accumulate (C++ 中在 <numeric>) |
|
<numeric> |
数值算法 |
std::accumulate, std::inner_product, std::partial_sum, std::iota (C++11) |
|
<execution> |
并行算法执行策略 |
std::execution::seq, std::execution::par, std::execution::par_unseq |
C++17 |
<ranges> |
范围库(视图、适配器) |
std::views, std::ranges::sort 等 |
C++20 |
<bit> |
位操作函数 |
std::bit_cast, std::rotl, std::popcount |
C++20 |
<memory> |
内存管理工具、智能指针 |
std::unique_ptr, std::shared_ptr, std::allocator |
C++11(智能指针) |
<scoped_allocator> |
多级分配器 |
std::scoped_allocator_adaptor |
C++11 |
<memory_resource> |
多态内存资源 |
std::pmr::memory_resource, std::pmr::polymorphic_allocator |
C++17 |
6. 迭代器
| 头文件 |
说明 |
常用组件 |
版本 |
<iterator> |
迭代器定义、特性、适配器 |
std::iterator_traits, std::back_inserter, std::begin, std::end |
|
7. 数值与数学
| 头文件 |
说明 |
常用组件 |
版本 |
<cmath> |
C 风格数学函数 |
std::sqrt, std::pow, std::sin, std::cos |
|
<cstdlib> |
包含 std::abs 等整数绝对值 |
|
|
<cinttypes> |
整数类型格式宏及函数 |
std::intmax_t, std::strtoimax |
C++11 |
<complex> |
复数类 |
std::complex |
|
<random> |
随机数生成器与分布 |
std::mt19937, std::uniform_int_distribution |
C++11 |
<valarray> |
数值数组类 |
std::valarray |
|
<ratio> |
编译期有理数 |
std::ratio, std::milli |
C++11 |
<numbers> |
数学常数 |
std::numbers::pi, std::numbers::e |
C++20 |
<linalg> |
线性代数基础算法(BLAS 风格) |
std::linalg::vector_abs_sum |
C++26(预览) |
8. 时间与日期
| 头文件 |
说明 |
常用组件 |
版本 |
<chrono> |
时间库 |
std::chrono::duration, std::chrono::time_point, std::chrono::system_clock |
C++11 |
<ctime> |
C 风格日期时间 |
std::time, std::localtime, std::strftime |
|
<calendar> |
日历日期(原为 <chrono> 的一部分) |
std::chrono::year_month_day 等 |
C++20 |
<time_zone> |
时区支持(原为 <chrono> 的一部分) |
std::chrono::time_zone |
C++20 |
9. 多线程与并发
| 头文件 |
说明 |
常用组件 |
版本 |
<thread> |
线程类 |
std::thread |
C++11 |
<mutex> |
互斥锁 |
std::mutex, std::lock_guard, std::unique_lock, std::shared_mutex (C++17) |
C++11 |
<shared_mutex> |
共享互斥锁 |
std::shared_mutex, std::shared_lock |
C++14 |
<condition_variable> |
条件变量 |
std::condition_variable, std::condition_variable_any |
C++11 |
<future> |
异步任务与未来对象 |
std::future, std::promise, std::async |
C++11 |
<atomic> |
原子操作 |
std::atomic, std::atomic_flag |
C++11 |
<barrier> |
屏障(线程同步点) |
std::barrier |
C++20 |
<latch> |
一次性倒计时闩锁 |
std::latch |
C++20 |
<semaphore> |
信号量 |
std::counting_semaphore, std::binary_semaphore |
C++20 |
<stop_token> |
可停止线程的令牌 |
std::stop_token, std::stop_source |
C++20 |
10. 文件系统
| 头文件 |
说明 |
常用组件 |
版本 |
<filesystem> |
文件系统操作 |
std::filesystem::path, std::filesystem::directory_iterator, std::filesystem::copy |
C++17 |
11. 正则表达式
| 头文件 |
说明 |
常用组件 |
版本 |
<regex> |
正则表达式库 |
std::regex, std::smatch, std::regex_search |
C++11 |
12. 随机数生成
| 头文件 |
说明 |
常用组件 |
版本 |
<random> |
(同 7. 数值与数学) |
|
C++11 |
13. 异常处理
| 头文件 |
说明 |
常用组件 |
版本 |
<exception> |
(同 1. 语言支持) |
|
|
<stdexcept> |
(同 1. 语言支持) |
|
|
14. 实用工具
| 头文件 |
说明 |
常用组件 |
版本 |
<utility> |
通用工具:std::pair, std::move, std::forward |
std::pair, std::make_pair, std::move, std::forward, std::swap |
|
<tuple> |
元组 |
std::tuple, std::make_tuple, std::tie |
C++11 |
<optional> |
可能含值的包装器 |
std::optional |
C++17 |
<variant> |
类型安全的联合 |
std::variant |
C++17 |
<any> |
可存储任意类型的对象 |
std::any |
C++17 |
<expected> |
含值或错误的类型 |
std::expected |
C++23 |
<bitset> |
固定大小的位序列 |
std::bitset |
|
<type_traits> |
编译期类型特性 |
std::is_integral, std::is_same, std::enable_if |
C++11 |
<typeindex> |
type_info 的包装器,可用于关联容器 |
std::type_index |
C++11 |
<functional> |
函数对象、绑定器、std::function |
std::function, std::bind, std::placeholders, std::mem_fn, std::ref |
|
<ctime> |
(同 8. 时间与日期) |
|
|
<chrono> |
(同 8. 时间与日期) |
|
|
<cstdalign> |
对齐支持(C++11 中废弃) |
alignas, alignof |
C++11(废弃) |
<cstdbool> |
布尔宏(C++11 中废弃) |
bool 宏 |
C++11(废弃) |
<version> |
特性测试宏 |
__cpp_lib_* |
C++20 |
15. 动态内存管理
| 头文件 |
说明 |
常用组件 |
版本 |
<new> |
(同 1. 语言支持) |
|
|
<memory> |
(同 5. 算法) |
智能指针、std::allocator |
|
<scoped_allocator> |
(同 5. 算法) |
|
C++11 |
<memory_resource> |
(同 5. 算法) |
|
C++17 |
16. 类型支持
| 头文件 |
说明 |
常用组件 |
版本 |
<typeinfo> |
(同 1. 语言支持) |
|
|
<type_traits> |
(同 14. 实用工具) |
|
C++11 |
<concepts> |
概念库 |
std::same_as, std::convertible_to, std::derived_from 等 |
C++20 |
<coroutine> |
协程支持 |
std::coroutine_handle, std::suspend_always |
C++20 |
<any> |
(同 14. 实用工具) |
|
C++17 |
<optional> |
(同 14. 实用工具) |
|
C++17 |
<variant> |
(同 14. 实用工具) |
|
C++17 |
17. 原子操作
| 头文件 |
说明 |
常用组件 |
版本 |
<atomic> |
(同 9. 多线程与并发) |
|
C++11 |
18. 信号处理
| 头文件 |
说明 |
常用组件 |
版本 |
<csignal> |
(同 1. 语言支持) |
|
|
19. 本地化
| 头文件 |
说明 |
常用组件 |
版本 |
<locale> |
本地化设置 |
std::locale, std::ctype, std::collate |
|
<clocale> |
C 风格本地化 |
std::setlocale, std::localeconv |
|
<codecvt> |
字符编码转换(C++17 中废弃,C++26 中移除) |
std::codecvt_utf8 |
C++11(已废弃) |
20. C 兼容头文件
C++ 提供了对应的 C 标准库头文件(以 c 开头,如 <cstdio>),并确保所有名字在 std 命名空间中。此外也保留了不带 .h 的 C 头文件,但建议使用 C++ 风格的版本。
| 头文件 |
对应 C 头文件 |
说明 |
<cassert> |
<assert.h> |
断言 |
<cctype> |
<ctype.h> |
字符处理 |
<cerrno> |
<errno.h> |
错误码 |
<cfenv> |
<fenv.h> |
浮点环境 |
<cfloat> |
<float.h> |
浮点极限 |
<cinttypes> |
<inttypes.h> |
整数格式 |
<climits> |
<limits.h> |
整数极限 |
<clocale> |
<locale.h> |
本地化 |
<cmath> |
<math.h> |
数学函数 |
<csetjmp> |
<setjmp.h> |
非局部跳转 |
<csignal> |
<signal.h> |
信号 |
<cstdarg> |
<stdarg.h> |
可变参数 |
<cstddef> |
<stddef.h> |
常用类型 |
<cstdint> |
<stdint.h> |
定宽整数 |
<cstdio> |
<stdio.h> |
输入输出 |
<cstdlib> |
<stdlib.h> |
通用工具 |
<cstring> |
<string.h> |
字符串函数 |
<ctime> |
<time.h> |
时间 |
<cuchar> |
<uchar.h> |
Unicode 字符 |
<cwchar> |
<wchar.h> |
宽字符 |
<cwctype> |
<wctype.h> |
宽字符分类 |
使用建议
- 优先使用 C++ 标准库:它提供了类型安全、泛型、RAII 等现代 C++ 特性。
- 包含所需的最小头文件:避免包含不必要的巨大头文件(如
<bits/stdc++.h>),以加快编译速度。
- 注意版本兼容性:某些头文件(如
<format>、<ranges>)需要较新的编译器支持。
- C 兼容头文件:在 C++ 中应使用
<cxxx> 版本,并将标识符放入 std 命名空间。
本文档基于 C++23 标准整理,部分 C++26 特性作为预览提及。实际使用时请参考编译器的支持情况。