Skip to main content

1 / 4概念

Ecosystem: Standard Library

C++ 标准库是 ISO C++ 规定的接口集合,由具体实现提供二进制。判断一个组件是否用得对,看三件事:它属于哪个家族、以头文件还是编译库交付、跨模块时哪些布局由实现决定。语法与类型规则不在这里,本页只处理标准库自身的边界。

1 概念​

1.1 一句话定义​

C++ 标准库是随语言标准发布的设施集合;容器、迭代器、算法与函数对象四件套源自 STL,其余家族随标准逐版并入。接口由标准规定,实现由 libstdc++、libc++ 或 MSVC STL 提供,两者之间还夹着一层由编译器选项决定的 ABI。

1.2 它解决什么问题​

  • 不重写基础结构:动态数组、有序表、哈希表、字符串与文件遍历由标准提供,团队只写业务逻辑。
  • 复杂度与保证可预期:每个操作都有标准规定的复杂度上限与异常保证,评审时可以引用条款而不是猜。
  • 算法与容器解耦:迭代器与 ranges 让同一套算法作用于不同容器,减少重复循环。
  • 跨平台系统设施:filesystem、chrono、thread、format 把平台差异收进标准接口。
  • 可移植的资源模型:分配器、unique_ptr、pmr 把所有权与分配策略写成类型,而不是约定。

1.3 不属于本页的内容​

声明与初始化见 cpp-syntax;引用、推导与转换见 cpp-type-system;类与虚分派见 cpp-classes-and-abstraction;异常语义见 cpp-errors-and-resources;并发模型见 cpp-concurrency;编译与标准开关见 cpp-compilers;构建消费见 cpp-cmake;对象布局与 vtable 见 cpp-object-model-and-abi;跨语言边界见 cpp-ffi。

1.4 完整枚举:十三个组件家族​

家族成员交付形态ABI 敏感点
containersarray、vector、deque、list、forward_list;set、multiset、map、multimap、flat_set、flat_multiset、flat_map、flat_multimap;unordered_set、unordered_multiset、unordered_map、unordered_multimap;stack、queue、priority_queue;span、mdspan头文件模板元素类型与分配器决定布局;节点大小由实现定;调试宏改变对象大小
iteratorsiterator_traits、input_iterator 至 contiguous_iterator 的概念族、reverse_iterator、move_iterator、insert_iterator、back_insert_iterator、front_insert_iterator、istream_iterator、ostream_iterator、istreambuf_iterator、ostreambuf_iterator、common_iterator、counted_iterator、default_sentinel、ranges::iterator_t、ranges::sentinel_t头文件模板迭代器类别与调试迭代器;_GLIBCXX_DEBUG 改变表示
algorithms非修改 all_of、any_of、none_of、for_each、for_each_n、count、count_if、mismatch、equal、find、find_if、find_if_not、find_end、find_first_of、adjacent_find、search、search_n;修改 copy、copy_if、copy_n、copy_backward、move、move_backward、fill、fill_n、transform、generate、generate_n、remove、remove_if、remove_copy、remove_copy_if、replace、replace_if、replace_copy、replace_copy_if、swap_ranges、reverse、reverse_copy、rotate、rotate_copy、shuffle、sample、unique、unique_copy;排序与划分 sort、stable_sort、partial_sort、partial_sort_copy、nth_element、is_sorted、is_sorted_until、partition、stable_partition、partition_copy、partition_point;二分 lower_bound、upper_bound、equal_range、binary_search;集合 merge、inplace_merge、set_difference、set_intersection、set_symmetric_difference、set_union;堆 make_heap、push_heap、pop_heap、sort_heap、is_heap、is_heap_until;最值与比较 min、max、minmax、min_element、max_element、minmax_element、clamp、lexicographical_compare;排列 next_permutation、prev_permutation、is_permutation头文件模板,并行重载需 <execution> 后端谓词与比较器抛出的异常直接穿透;并行后端需额外链接
ranges概念 range、borrowed_range、sized_range、view、common_range、contiguous_range;视图 views::filter、transform、take、take_while、drop、drop_while、join、join_with、split、lazy_split、common、reverse、keys、values、elements、zip、zip_transform、adjacent、adjacent_transform、chunk、chunk_by、slide、stride、enumerate、iota、single、empty、all、counted;动作 ranges::sort、ranges::stable_sort、ranges::copy、ranges::to(C++23)头文件模板视图持有指针或迭代器,悬垂不报错;投影改变重载解析
numerics<numeric> 的 iota、accumulate、reduce、transform_reduce、inner_product、partial_sum、adjacent_difference、exclusive_scan、inclusive_scan、transform_exclusive_scan、transform_inclusive_scan、gcd、lcm、midpoint;<bit> 的 popcount、countl_zero、countr_zero、countl_one、countr_one、has_single_bit、bit_ceil、bit_floor、bit_width、rotl、rotr、byteswap;<numbers> 数学常量、<complex>、<valarray>、<random> 引擎与分布、<ratio> 编译期有理数头文件模板,<random> 部分在库内long double 与浮点格式随平台;随机序列不保证跨实现一致
stringsbasic_string 与 string、wstring、u8string、u16string、u32string 别名;basic_string_view 与同名别名;char_traits;getline;数值转换 stoi、stol、stoul、stoll、stoull、stof、stod、stold、to_string;to_chars、from_chars;operator+、substr、find 系列头文件模板,浮点 to_chars 支持按实现对象大小与 SSO 容量:libstdc++ 32 字节 15 字符,libc++ 24 字节 22 字符,MSVC 32 字节 15 字符
streamsios_base、basic_ios、basic_istream、basic_ostream、basic_iostream;basic_ifstream、basic_ofstream、basic_fstream;basic_istringstream、basic_ostringstream、basic_stringstream;basic_ispanstream、basic_ospanstream、basic_spanstream(C++23);basic_streambuf、basic_filebuf、basic_stringbuf、basic_spanbuf;操纵符 endl、ends、flush、setw、setprecision、setfill、hex、dec、oct、boolalpha、showbase、setbase;sync_with_stdio;locale 与 codecvt 面编译库,<iostream> 的初始化对象在库内ios_base 布局、locale 引用计数由实现定;静态初始化顺序
filesystempath、directory_entry、directory_iterator、recursive_directory_iterator、filesystem_error、file_status、file_type、space_info、file_time_type;absolute、canonical、weakly_canonical、relative、proximate、current_path、exists、equivalent、file_size、hard_link_count、last_write_time、permissions、read_symlink、status、symlink_status、temp_directory_path、copy、copy_file、copy_symlink、create_directory、create_directories、create_hard_link、create_symlink、remove、remove_all、rename、resize_file、space编译库;GCC 8 及以前需 -lstdc++fs,GCC 9 起并入 libstdc++path 内部表示与 directory_entry 缓存;错误走 error_code 重载或异常
chronoduration、time_point、duration_cast、floor、ceil、round;system_clock、steady_clock、high_resolution_clock;日历 year、month、day、weekday、year_month_day、hh_mm_ss;时区 time_zone、zoned_time、current_zone、locate_zone;字面量 1s、1ms、1h头文件加编译库,时区数据库在运行期读取system_clock 纪元与精度由实现定;time_zone 指针不可跨进程传递
formatformat、vformat、format_to、format_to_n、formatted_size、formatter 特化、make_format_args、basic_format_args、format_error;print、println(C++23)编译库;格式串检查在头文件,vformat 在库内std::format 与 fmt 的 ABI 不同;编译期检查依赖 consteval
memoryallocator、allocator_traits;unique_ptr、shared_ptr、weak_ptr、make_unique、make_shared、default_delete;addressof、align、pointer_traits、to_address、construct_at、destroy_at、uninitialized_copy、uninitialized_fill、uninitialized_move、uninitialized_value_construct;pmr::memory_resource、pmr::polymorphic_allocator、pmr::monotonic_buffer_resource、pmr::unsynchronized_pool_resource、pmr::synchronized_pool_resource、pmr::new_delete_resource头文件加编译库,控制块与 pmr 资源在库内控制块布局、allocate 与 deallocate 必须成对、memory_resource 虚表
concurrencythread、jthread、this_thread;mutex、recursive_mutex、timed_mutex、recursive_timed_mutex、shared_mutex;lock_guard、unique_lock、shared_lock、scoped_lock;condition_variable、condition_variable_any;atomic、atomic_flag、atomic_ref、memory_order;future、promise、packaged_task、async;latch、barrier、semaphore;stop_token、stop_source编译库加线程库,需 -pthread 或 Threads::Threadsthread 句柄表示、mutex 内嵌平台原语、future 共享状态布局
utilitiespair、tuple、apply、make_tuple、tie、forward_as_tuple;optional、variant、any、expected(C++23);function、bind、mem_fn、invoke、reference_wrapper;move、forward、swap、exchange;type_traits、integer_sequence;bitset;compare 的三种序;source_location、stacktrace、coroutine_handle、suspend_always、suspend_never;exception、nested_exception、uncaught_exceptions;initializer_list;type_info、type_index多为头文件模板,exception、stacktrace、coroutine 部分在库内function 的 SBO 大小、any 的 SBO、variant 的索引与存储由实现定