C++ Standard Library List

C++标准库列表

截至 C++26(2026 年 3 月已定稿),标准库的组件非常庞大。下面按功能类别整理了一份核心标准库组件表格,覆盖从 C++11 到 C++26 的主要头文件及其作用,并附上常见用法示例。


一、语言支持与基础工具

头文件主要组件作用常见用法
<cstddef>size_t, nullptr_t, byte基础类型定义size_t n = v.size();
<cstdint>int32_t, uint64_t 等固定宽度整数类型int32_t x = 42;
<type_traits>is_integral_v, enable_if_t编译期类型查询与变换static_assert(std::is_integral_v<int>);
<concepts>std::integral, std::floating_point概念库(C++20)template<std::integral T> T sum(T a, T b);
<coroutine>coroutine_handle, suspend_always协程底层支持(C++20)配合 std::generator 使用
<source_location>source_location::current()源码位置信息(C++20)auto loc = std::source_location::current();
<version>特性测试宏检查库实现状态(C++20)#if __cpp_lib_generator >= 202207L
<any>std::any类型安全的任意值容器(C++17)std::any a = 42; a = std::string("hi");
<optional>std::optional<T>可能为空的值的封装(C++17)std::optional<int> o = f(); if (o) use(*o);
<variant>std::variant<Ts...>类型安全的联合体(C++17)std::variant<int,string> v = 1;
<tuple>std::tuple, std::get异构值集合auto t = std::make_tuple(1, "a");
<utility>std::move, std::forward, std::pair移动语义与转发auto p = std::make_pair(1, 2);
<bit>std::bit_cast, std::popcount位操作(C++20)auto n = std::popcount(0b1011u);
<expected>std::expected<T,E>值或错误的封装(C++23)std::expected<int,string> r = parse(s);
<print>std::print, std::println格式化输出(C++23)std::println("Hello, {}!", name);
<format>std::format类型安全格式化(C++20)auto s = std::format("{:.2f}", 3.14);

用法示例(std::expected) :

c++c++
#include <expected>std::expected<double, std::string> parse_number(const std::string& s) {    try { return std::stod(s); }    catch (...) { return std::unexpected("invalid number"); }}auto r = parse_number("3.14");if (r) std::println("{}", *r);else   std::println("Error: {}", r.error());

std::expected 是 C++23 引入的标准错误处理方式,可以在不使用异常的情况下返回“值或错误”。


二、容器库

头文件主要组件作用常见用法
<array>std::array<T,N>固定大小数组(C++11)std::array<int,3> a = {1,2,3};
<vector>std::vector<T>动态数组std::vector<int> v{1,2,3}; v.push_back(4);
<list>std::list<T>双向链表std::list<int> l = {1,2,3}; l.push_front(0);
<forward_list>std::forward_list<T>单向链表(C++11)std::forward_list<int> fl = {1,2,3};
<deque>std::deque<T>双端队列std::deque<int> d; d.push_back(1); d.push_front(0);
<map>std::map<K,V>有序键值对(红黑树)std::map<string,int> m; m["a"] = 1;
<set>std::set<T>有序唯一集合std::set<int> s = {3,1,2};
<unordered_map>std::unordered_map<K,V>哈希键值对(C++11)std::unordered_map<string,int> um;
<unordered_set>std::unordered_set<T>哈希集合(C++11)std::unordered_set<int> us = {1,2,3};
<stack>std::stack<T>LIFO 栈适配器std::stack<int> st; st.push(1);
<queue>std::queue<T>FIFO 队列适配器std::queue<int> q; q.push(1);
<flat_map>std::flat_map<K,V>连续内存映射(C++23)std::flat_map<int,string> fm; fm[1]="a";
<flat_set>std::flat_set<T>连续内存集合(C++23)std::flat_set<int> fs = {3,1,2};
<inplace_vector>std::inplace_vector<T,N>固定容量栈上向量(C++26)std::inplace_vector<int,10> iv; iv.push_back(1);
<hive>std::hive<T>稳定地址容器(C++26)适合频繁插入删除、需地址稳定的场景

用法示例(std::vector + std::map) :

c++c++
#include <vector>#include <map>#include <string>std::vector<int> v = {5, 2, 9, 1};v.push_back(6);for (const auto& x : v) std::print("{} ", x);std::map<std::string, int> scores;scores["Alice"] = 95;scores["Bob"]   = 87;for (const auto& [name, score] : scores)    std::println("{}: {}", name, score);

vector 和 map 是 C++ 最常用的容器,分别适合动态数组和键值对场景。

C++23 新增容器说明:std::flat_map 和 std::flat_set 将元素存储在连续内存中,提供更好的缓存性能。C++26 的 std::inplace_vector 提供固定容量、栈上分配的序列,避免堆分配开销。


三、算法与迭代器

头文件主要组件作用常见用法
<algorithm>sort, find, count, transform通用算法std::sort(v.begin(), v.end());
<numeric>accumulate, reduce, iota数值算法int s = std::accumulate(v.begin(), v.end(), 0);
<ranges>views::filter, views::transform, ranges::to范围库(C++20/23)v | views::filter(pred) | views::take(5)
<iterator>begin, end, advance, next迭代器工具auto it = std::next(v.begin(), 2);
<execution>std::execution::par并行执行策略(C++17)std::sort(std::execution::par, v.begin(), v.end());

用法示例(std::ranges + std::views) :

c++c++
#include <ranges>#include <vector>std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8};// 惰性视图:筛选偶数,取前3个,平方auto result = v    | std::views::filter([](int n) { return n % 2 == 0; })    | std::views::take(3)    | std::views::transform([](int n) { return n * n; });for (int x : result) std::print("{} ", x);  // 4 16 36// C++23: ranges::to 将视图物化为容器auto vec = result | std::ranges::to<std::vector>();

范围库让算法可以管道式组合,std::ranges::to(C++23)可将惰性视图直接转为具体容器。


四、内存管理

头文件主要组件作用常见用法
<memory>unique_ptr, shared_ptr, weak_ptr智能指针auto p = std::make_unique<Foo>();
<memory_resource>pmr::vector, memory_resource多态分配器(C++17)std::pmr::vector<int> v(&pool);
<scoped_allocator>scoped_allocator_adaptor嵌套分配器(C++11)容器嵌套时的分配器传播

智能指针对比:

智能指针所有权引用计数适用场景
unique_ptr独占无独占资源,性能最优
shared_ptr共享有多对象共享同一资源
weak_ptr不拥有不参与计数观测共享对象、打破循环引用
c++c++
// 独占所有权auto p = std::make_unique<Resource>();process(std::move(p));// 共享所有权auto shared = std::make_shared<Resource>();auto copy = shared;  // 引用计数: 2// 弱引用(不增加计数)std::weak_ptr<Resource> weak = shared;if (auto locked = weak.lock()) { /* 使用 locked */ }

智能指针是 RAII 原则在指针管理上的应用,自动管理资源生命周期,避免内存泄漏和悬空指针。


五、字符串与文本处理

头文件主要组件作用常见用法
<string>std::string, std::to_string动态字符串std::string s = "hello"; s += " world";
<string_view>std::string_view非拥有字符串视图(C++17)void f(std::string_view sv);
<charconv>std::from_chars, std::to_chars高效数字转换(C++17)std::from_chars(s.data(), s.data()+s.size(), x);
<format>std::format格式化字符串(C++20)std::format("{} = {:.2f}", name, val);
<regex>std::regex, std::smatch正则表达式(C++11)std::regex re(R"(\d+)");
<text_encoding>std::text_encoding编码检测(C++26)识别和转换字符编码

六、并发与并行

头文件主要组件作用常见用法
<thread>std::thread, std::jthread线程管理(jthread C++20)std::jthread t([]{ work(); });
<mutex>std::mutex, lock_guard, scoped_lock互斥量std::lock_guard lk(mtx);
<atomic>std::atomic<T>原子操作std::atomic<int> counter{0}; counter++;
<condition_variable>std::condition_variable条件变量cv.wait(lk, []{ return ready; });
<future>std::future, std::promise, std::async异步任务auto f = std::async([]{ return 42; });
<barrier>std::barrier线程屏障(C++20)std::barrier sync(4); sync.arrive_and_wait();
<latch>std::latch一次性倒计时(C++20)std::latch l(4); l.count_down();
<semaphore>std::counting_semaphore信号量(C++20)std::counting_semaphore sem(3);
<execution>std::execution::sender/receiver统一异步框架(C++26)结构化并发,避免数据竞争
<simd>std::simd数据并行(C++26)向量化运算

C++26 std::execution(Senders/Receivers) :这是 C++26 的旗舰并发特性,提供统一的异步执行框架。Senders 描述异步操作,Receivers 接收结果,通过 connect 连接后由 start 启动。

c++c++
// 概念示意auto work = std::execution::just(42)    | std::execution::then([](int x) { return x * 2; });auto [result] = std::this_thread::sync_wait(work).value();// result == 84

该框架支持结构化并发,从构造上避免数据竞争和死锁。


七、数值计算

头文件主要组件作用常见用法
<cmath>std::sqrt, std::sin, std::pow数学函数double r = std::sqrt(2.0);
<complex>std::complex<T>复数类型std::complex<double> z(1, 2);
<random>std::mt19937, uniform_int_distribution随机数生成std::mt19937 gen{std::random_device{}()};
<ratio>std::ratio<1,1000>编译期有理数(C++11)using ms = std::ratio<1,1000>;
<numbers>std::numbers::pi数学常量(C++20)double area = std::numbers::pi * r * r;
<bit>std::popcount, std::bit_width位操作(C++20)auto n = std::popcount(x);
<linalg>std::linalg::dot, matrix_product基础线性代数(C++26)BLAS 风格线性代数算法
c++c++
// C++26 线性代数示例(概念示意)#include <linalg>#include <mdspan>// 基于 std::mdspan 的矩阵运算// std::linalg::matrix_product(A, B, C);

八、输入/输出

头文件主要组件作用常见用法
<iostream>std::cin, std::cout, std::cerr标准流 I/Ostd::cout << "Hello" << std::endl;
<fstream>std::ifstream, std::ofstream文件 I/Ostd::ifstream f("data.txt");
<sstream>std::stringstream, std::ostringstream字符串流std::ostringstream oss; oss << 42;
<print>std::print, std::println现代格式化输出(C++23)std::println("x = {}, y = {}", x, y);
<syncstream>std::osyncstream线程安全输出(C++20)std::osyncstream(std::cout) << "msg\n";
<span>std::span<T>连续内存视图(C++20)void f(std::span<int> data);
<mdspan>std::mdspan<T>多维数组视图(C++23)非拥有式多维数据访问

std::print / std::println(C++23) 提供了类似 Python 的格式化输出,相比 iostream 更简洁、类型安全、性能更好:

c++c++
std::println("{} + {} = {}", 1, 2, 3);std::print("{:.3f}\n", 3.14159);

九、C++23/26 重点新组件速览

组件标准作用一句话用法
std::expected<T,E>C++23值或错误return std::unexpected("err");
std::generator<T>C++23协程生成器std::generator<int> gen() { co_yield 1; }
std::print/printlnC++23格式化输出std::println("{}", x);
std::flat_map/flat_setC++23连续内存容器缓存友好替代 map/set
std::mdspanC++23多维视图std::mdspan m(data, 3, 4);
std::ranges::toC++23视图物化view | std::ranges::to<std::vector>()
std::views::zip/enumerateC++23新范围适配器views::enumerate(v)
import std;C++23模块化标准库import std; 替代所有头文件
std::inplace_vectorC++26固定容量栈向量std::inplace_vector<int,10> iv;
std::hiveC++26稳定地址容器频繁增删且需地址稳定
std::copyable_functionC++26std::function 替代可复制的类型擦除可调用体
std::function_refC++26轻量非拥有引用替代 const std::function& 参数
std::executionC++26统一异步框架Sender/Receiver 结构化并发
std::linalgC++26线性代数基于 mdspan 的 BLAS 风格算法
std::text_encodingC++26字符编码编码检测与转换
std::optional<T&>C++26引用可选支持引用类型的 optional
std::simdC++26数据并行显式向量化类型

std::generator(C++23)用法示例:

c++c++
#include <generator>std::generator<int> fibonacci() {    int a = 0, b = 1;    while (true) {        co_yield a;        auto next = a + b;        a = b; b = next;    }}for (int x : fibonacci() | std::views::take(8))    std::print("{} ", x);  // 0 1 1 2 3 5 8 13

std::generator 是 C++23 引入的基于协程的惰性序列生成器,模型化 input_range,可直接用于范围 for 循环。


参考来源

以上内容综合了 cppreference 标准库头文件列表、C++23/26 新特性总结以及 C++26 定稿报道。由于 C++26 刚刚定稿,部分组件的编译器支持仍在完善中,建议在实际使用时关注所用编译器的版本和支持状态。

除非特别说明,本站文章采用 CC BY-NC-SA 4.0 许可协议。

在 GitHub 编辑

相关阅读

4 阅读

机器学习·智械起源

数字如何从0到1构造智能

从「观察 → 表示 → 预测 → 决策 → 行动 → 反馈 → 更新」的闭环出发,本文梳理机器学习的本质与流形假设,给出神经网络的标准数学模型与损失函数,再用…

机器学习# 神经网络# 反向传播# 流形假设# MNIST
14 阅读

How to Design an Elegant Agent System

从 cell.cpp 提炼的十条构造原则 —— 把不确定性关进确定性的笼子

Agent 框架越做越厚,但真正的问题只有一个 —— 如何让一个只会预测下一个 token 的东西,可靠地改变外部世界。我以单文件 C++ Agent「cell.cpp」为样…

工程# Agent# 系统设计# 工程哲学# LLM
1 阅读

最小系统的设计原理

如何优雅地设计一个极客风格的系统

一般而言,根据钱老的《工程控制论》思想,一个系统可以由以下部分组成: 其基本结构可表示为: 其中: 比较器:计算偏差,例如 e=r−ye=r−y; 控制器…

随笔# 随笔# 工程哲学
6 阅读

你好,墨痕 —— 一个用 Python 渲染的水墨博客引擎

从 Markdown 到可部署站点:构建管线、渲染能力与思维链的全景笔记

这个站点由 Inkstone 渲染 —— 一个用 Python 写的静态博客引擎。它只做三件事:读 Markdown、读配置、写出一坨可以直接丢到任何静态托管上的文件。 设…

工程# 静态站点# Python# 构建系统# 前端

评论