1.引子
《C++Primer第五版(中文版)》的附录A.1“标准库名字和头文件”一节,列出了该书程序使用到的标准库名字及对应的头文件,但该列表仅仅是单纯地按首字母顺序罗列出使用过的标准库名字,并无分类,于读者无益。为有效认识和理解C++11标准库,以下对其进行标准库分类,希望于读者有所帮助。本文基于广为流传的《C++标准库头文件介绍》,列出在C++Primer中使用的标准库名字(名字对应的描述待后续补上)。
2.C++标准库头文件
C++标准库的所有头文件都没有扩展名。C++标准库的内容总共在50个标准头文件中定义,其中18个提供了C库的功能。<cname>形式的标准头文件【<complex>例外】其内容与ISO标准C包含的name.h头文件相同,但容纳了C++扩展的功能。在<cname>形式标准的头文件中,与宏相关的名称在全局作用域中定义,其他名称在std命名空间中声明。在C++中还可以使用name.h形式的标准C库头文件名。
C++标准库的内容分为10类:
C1.语言支持 | C2.输入/输出 | C3.诊断 | C4.一般工具 | C5.字符串 |
C6.容器 | C7.迭代器支持 | C8.算法 | C9.数值操作 | C10.本地化 |
2.1 C1标准库
语言支持功能。
头文件 | 描述 |
<cstddef> | 定义宏NULL和offsetof,以及其他标准类型size_t和ptrdiff_t。与对应的标准C头文件的区别是,NULL是C++空指针常量的补充定义,宏offsetof接受结构或者联合类型参数,只要他们没有成员指针类型的非静态成员即可。 |
<limits> | 提供与基本数据类型相关的定义。例如,对于每个数值数据类型,它定义了可以表示出来的最大值和最小值以及二进制数字的位数。 |
<climits> | 提供与基本整数数据类型相关的C样式定义。这些信息的C++样式定义在 <limits>中。 |
<cfloat> | 提供与基本浮点型数据类型相关的C样式定义。这些信息的C++样式定义在 <limits>中。 |
<cstdlib> | 提供支持程序启动和终止的宏和函数。这个头文件还声明了许多其他杂项函数,例如搜索和排序函数,从字符串转换为数值等函数。它与对应的标准C头文件stdlib.h不同,定义了abort(void)。abort()函数还有额外的功能,它不为静态或自动对象调用析构函数,也不调用传给atexit()函数的函数。它还定义了exit()函数的额外功能,可以释放静态对象,以注册的逆序调用用atexit()注册的函数。清除并关闭所有打开的C流,把控制权返回给主机环境。 |
<new> | 支持动态内存分配。 |
<typeinfo> | 支持变量在运行期间的类型标识。 |
<exception> | 支持异常处理,这是处理程序中可能发生的错误的一种方式。 |
<cstdarg> | 支持接受数量可变的参数的函数。即在调用函数时,可以给函数传送数量不等的数据项。它定义了宏va_arg、va_end、va_start以及va_list类型。 |
<csetjmp> | 为C样式的非本地跳跃提供函数。这些函数在C++中不常用。 |
<csignal> | 为中断处理提供C样式支持。 |
C++ Primer中使用的C1标准库。
名字 | 标准库 | 描述 |
ptrdiff_t | <cstddef> |
|
size_t | <cstddef> |
|
abort | <cstdlib> |
|
free | <cstdlib> |
|
malloc | <cstdlib> |
|
bad_alloc | <new> |
|
bad_array_new_length | <new> |
|
nothrow | <new> |
|
nothrow_t | <new> |
|
bad_cast | <typeinfo> |
|
type_info | <typeinfo> |
|
exception | <exception> |
|
terminate | <exception> |
|
unexpected | <exception> |
|
2.2 C2标准库
支持流输入/输出。
头文件 | 描述 |
<iostream> | 支持标准流cin、cout、cerr和clog的输入和输出,它还支持多字节字符标准流wcin、wcout、wcerr和wclog。 |
<iomanip> | 提供操纵程序,允许改变流的状态,从而改变输出的格式。 |
<ios_base> | 不依赖模板参数的输入和输出流通用的存储和成员函数 |
<ios> | 定义iostream的基类。 |
<istream> | 为管理输入流缓存区的输入定义模板类。 |
<ostream> | 为管理输出流缓存区的输出定义模板类。 |
<sstream> | 支持字符串的流输入输出。 |
<fstream> | 支持文件的流输入输出。 |
<iosfwd> | 为输入输出对象提供向前的声明。 |
<streambuf> | 支持流输入和输出的缓存。 |
<cstdio> | 为标准流提供C样式的输入和输出。 |
<cwchar> | 支持多字节字符的C样式输入输出。 |
C++ Primer中使用的C2标准库。
名字 | 标准库 | 描述 |
ios_base | <ios_base> |
|
internal | <iostream> |
|
scientific | <iostream> |
|
cerr | <iostream> |
|
cin | <iostream> |
|
cout | <iostream> |
|
dec | <iostream> |
|
oct | <iostream> |
|
default_float_engine | <iostream> |
|
endl | <iostream> |
|
ends | <iostream> |
|
fixed | <iostream> |
|
flush | <iostream> |
|
hex | <iostream> |
|
hexfloat | <iostream> |
|
istream | <iostream> |
|
ostream | <iostream> |
|
left | <iostream> |
|
right | <iostream> |
|
boolalpha | <iostream> |
|
noboolalpha | <iostream> |
|
noshowbase | <iostream> |
|
showbase | <iostream> |
|
noshowpoint | <iostream> |
|
showpoint | <iostream> |
|
noskipws | <iostream> |
|
skipws | <iostream> |
|
nounitbuf | <iostream> |
|
unitbuf | <iostream> |
|
nouppercase | <iostream> |
|
uppercase | <iostream> |
|
setfill | <iomanip> |
|
setprecision | <iomanip> |
|
setw | <iomanip> |
|
stringstream | <sstream> |
|
istringstream | <sstream> |
|
ostringstream | <sstream> |
|
fstream | <fstream> |
|
ifstream | <fstream> |
|
ofstream | <fstream> |
|
2.3 C3标准库
诊断功能。
头文件 | 描述 |
<stdexcept> | 定义标准异常。异常是处理错误的方式 |
<cassert> | 定义断言宏,用于检查运行期间的情形 |
<cerrno> | 支持C样式的错误信息 |
C++ Primer中使用的C3标准库。
名字 | 标准库 | 描述 |
domain_error | <stdexcept> |
|
logic_error | <stdexcept> |
|
out_of_range | <stdexcept> |
|
range_error | <stdexcept> |
|
runtime_error | <stdexcept> |
|
2.4 C4标准库
工具函数。
头文件 | 描述 |
<utility> | 定义重载的关系运算符,简化关系运算符的写入,它还定义了pair类型,该类型是一种模板类型,可以存储一对值。这些功能在库的其他地方使用 |
<functional> | 定义了许多函数对象类型和支持函数对象的功能,函数对象是支持operator()()函数调用运算符的任意对象 |
<memory> | 给容器、管理内存的函数和auto_ptr模板类定义标准内存分配器 |
<ctime> | 支持系统时钟函数 |
<chrono> | C++11 新增。 提供了诸如 std::chrono::duration 和 std::chrono::time_point 等的时间元素,还有时钟。 |
C++ Primer中使用的C4标准库。
名字 | 标准库 | 描述 |
forward | <utility> |
|
make_pair | <utility> |
|
move | <utility> |
|
pair | <utility> |
|
swap | <utility> |
|
bind | <functional> |
|
cref | <functional> |
|
function | <functional> |
|
greater | <functional> |
|
hash | <functional> |
|
less | <functional> |
|
less_equal | <functional> |
|
negate | <functional> |
|
not1 | <functional> | ? |
placeholders | <functional> |
|
placeholders::_1 | <functional> |
|
plus | <functional> |
|
ref | <functional> |
|
mem_fn | <functional> |
|
allocator | <memory> |
|
auto_ptr | <memory> |
|
make_shared | <memory> |
|
uninitialized_copy | <memory> |
|
uninitialized_fill | <memory> |
|
shared_ptr | <memory> |
|
unique_ptr | <memory> |
|
weak_ptr | <memory> |
|
time | <ctime> |
|
2.5 C5标准库
字符串处理。
头文件 | 描述 |
<string> | 为字符串类型提供支持和定义,包括单字节字符串(由char组成)的string和多字节字符串(由wchar_t组成) |
<cctype> | 单字节字符类别 |
<cwctype> | 多字节字符类别 |
<cstring> | 为处理非空字节序列和内存块提供函数。这不同于对应的标准C库头文件,几个C样式字符串的一般C库函数被返回值为const和非const的函数对替代了 |
<cwchar> | 为处理、执行I/O和转换多字节字符序列提供函数,这不同于对应的标准C库头文件,几个多字节C样式字符串操作的一般C库函数被返回值为const和非const的函数对替代了。 |
<cstdlib> | 为把单字节字符串转换为数值、在多字节字符和多字节字符串之间转换提供函数 |
<regex> | C++11 新增。提供了用正则表达式进行字符串匹配的功能。 |
C++ Primer中使用的C5标准库。
名字 | 标准库 | 描述 |
getline | <string> |
|
stoi | <string> |
|
string | <string> |
|
strtod | <string> |
|
isalpha | <cctype> |
|
islower | <cctype> |
|
issprint | <cctype> |
|
ispunct | <cctype> |
|
isspace | <cctype> |
|
isupper | <cctype> |
|
tolower | <cctype> |
|
toupper | <cctype> |
|
strcmp | <cstring> |
|
strcpy | <cstring> |
|
strlen | <cstring> |
|
strncpy | <cstring> |
|
cmatch | <regex> |
|
csub_match | <regex> |
|
regex | <regex> |
|
regex_constants | <regex> |
|
regex_error | <regex> |
|
regex_match | <regex> |
|
regex_replace | <regex> |
|
regex_search | <regex> |
|
smatch | <regex> |
|
sregex_iterator | <regex> |
|
ssub_match | <regex> |
|
2.6 C6标准库
容器类的模板。
头文件 | 描述 |
<vector> | 定义vector序列模板,这是一个大小可以重新设置的数组类型,比普通数组更安全、更灵活 |
<list> | 定义list序列模板,这是一个序列的链表,常常在任意位置插入和删除元素 |
<deque> | 定义deque序列模板,支持在开始和结尾的高效插入和删除操作 |
<queue> | 为队列(先进先出)数据结构定义序列适配器queue和priority_queue |
<stack> | 为堆栈(后进先出)数据结构定义序列适配器stack |
<map> | map是一个关联容器类型,允许根据键值是唯一的,且按照升序存储。multimap类似于map,但键不是唯一的。 |
<set> | set是一个关联容器类型,用于以升序方式存储唯一值。multiset类似于set,但是值不必是唯一的。 |
<bitset> | 为固定长度的位序列定义bitset模板,它可以看作固定长度的紧凑型bool数组 |
<array> | C++11 新增。提供了容器类模板 std::array,固定大小数组的容器。 |
<forward_list> | C++11 新增。提供了单向链表容器类模板 std::forward_list。 |
<initializer_list> | C++11 新增。一个非常轻量级的表示初始化器列表的类模板initializer_list及有关函数。 |
<unordered_map> | C++11 新增。提供了容器类模板 std::unordered_map 和 std::unordered_multimap,它们都属于哈希表(散列表)。 |
<unordered_set> | C++11 新增。提供了容器类模板 std::unordered_set 和 std::unordered_multiset。 |
<tuple> | C++11 新增。提供了元组 std::tuple。 |
<type_traits> | C++11 新增。模板元基础库。可实现在编译期计算、判断、转换、查询等等功能 |
C++ Primer中使用的C6标准库。
名字 | 标准库 | 描述 |
vector | <vector> |
|
list | <list> |
|
deque | <deque> |
|
queue | <queue> |
|
priority_queue | <queue> |
|
stack | <stack> |
|
map | <map> |
|
multimap | <map> |
|
set | <set> |
|
multiset | <set> |
|
bitset | <bitset> |
|
array | <array> |
|
forward_list | <forward_list> |
|
initializer_list | <initializer_list> |
|
unordered_map | <unordered_map> |
|
unordered_multimap | <unordered_map> |
|
unordered_set | <unordered_set> |
|
unordered_multiset | <unordered_set> |
|
get | <tuple> |
|
make_tuple | <tuple> |
|
tuple | <tuple> |
|
tuple_element | <tuple> |
|
tuple_size | <tuple> |
|
remove_pointer | <type_traits> |
|
remove_reference | <type_traits> |
|
2.7 C7标准库
迭代器。
头文件 | 描述 |
<iterator> | 给迭代器提供定义和支持 |
C++ Primer中使用的C7标准库。
名字 | 标准库 | 描述 |
back_iterator | <iterator> |
|
begin | <iterator> |
|
end | <iterator> |
|
front_ inserter | <iterator> |
|
inserter | <iterator> |
|
istream_ iterator | <iterator> |
|
ostream_ iterator | <iterator> |
|
make_move_iterator | <iterator> |
|
reverse_ iterator | <iterator> |
|
2.8 C8标准库
算法。
头文件 | 描述 |
<algorithm> | 提供一组基于算法的函数,包括置换、排序、合并和搜索 |
<cstdlib> | 声明C标准库函数bsearch()和qsort(),进行搜索和排序 |
<ciso646> | 允许在代码中使用and代替&& |
C++ Primer中使用的C8标准库。
名字 | 标准库 | 描述 |
copy | <algorithm> |
|
count | <algorithm> |
|
count_if | <algorithm> |
|
equal_range | <algorithm> |
|
fill | <algorithm> |
|
fill_n | <algorithm> |
|
find | <algorithm> |
|
find_end | <algorithm> |
|
find_first_of | <algorithm> |
|
find_if | <algorithm> |
|
for_each | <algorithm> |
|
lower_bound | <algorithm> |
|
max | <algorithm> |
|
max_element | <algorithm> |
|
min | <algorithm> |
|
nth_element | <algorithm> |
|
partial_sort | <algorithm> |
|
replace | <algorithm> |
|
replace_copy | <algorithm> |
|
set_difference | <algorithm> |
|
set_intersection | <algorithm> |
|
set_union | <algorithm> |
|
sort | <algorithm> |
|
stable_sort | <algorithm> |
|
transform | <algorithm> |
|
unique | <algorithm> |
|
unique_copy | <algorithm> |
|
upper_bound | <algorithm> |
|
2.9 C9标准库
数值操作。
头文件 | 描述 |
<complex> | 支持复杂数值的定义和操作 |
<valarray> | 支持数值矢量的操作 |
<numeric> | 在数值序列上定义一组一般数学操作,例如accumulate和inner_product |
<cmath> | 这是C数学库,其中还附加了重载函数,以支持C++约定 |
<cstdlib> | 提供的函数可以提取整数的绝对值,对整数进行取余数操作 |
<random> | C++11新增。(伪)随机数相关。 |
C++ Primer中使用的C9标准库。
名字 | 标准库 | 描述 |
accumulate | <numeric> |
|
bernoulli_distribution | <random> |
|
default_random_engine | <random> |
|
normal_distribution | <random> |
|
rand | <random> |
|
random_device | <random> |
|
uniform_int_distribution | <random> |
|
uniform_real_distribution | <random> |
|
lround | <cmath> |
|
sqrt | <cmath> |
|
2.10 C10标准库
本地化。
头文件 | 描述 |
<locale> | 提供的本地化包括字符类别、排序序列以及货币和日期表示。 |
<clocale> | 对本地化提供C样式支持 |
<codecvt> | C++11新增。提供了不同代码页(code page)之间字符编码的转换功能。 |
2.11 线程支持
头文件 | 描述 |
<thread> | C++11新增。提供了使用线程所需要的类和 namespace。 |
<mutex> | C++11新增。这里面有互斥对象(mutex),锁(lock),还有 once(一种用来保证某函数在某进程中只执行一次的对象) |
<condition_variable> | C++11新增。条件变量。一个条件变量代表一个条件,当一个条件变量等待时,它会阻塞当前线程,直到它代表的条件为真。 |
<future> | C++11新增。可以用它来进行便捷的异步操作,以免代码中出现一大堆回调函数。 |
评论:《C++ Primer第五版》作为一本入门书籍,其附录A1确实写的不敢恭维,必须专门帮它分类整理。另外,其用意本是大力推广C++11标准,但却对线程支持只字不提,又不免为人所诟病。
3.参考资料
【1】C++标准库头文件介绍. https://blog.csdn.net/piao1024287076/article/details/52671278
【2】C++标准库概览(一分钟就看完了). https://www.cnblogs.com/findumars/p/6760029.html
【3】C++ GUI Qt4编程(第二版).D.3 C++标准库(寥寥2页却对C++标准库整理总结十分到位,推荐一看)