C语言中你可能不熟悉的头文件<cstdlib>(stdlib.h)
C Standard General Utilities Library (header)
C标准通用工具库(头文件)
此头文件定义了一些通用功能函数,包括动态存储器管理,随机数生成,与操作系统环境通信,整数算术,搜索,排序和转换。
函数:
字符串转换
atof
将字符串(char[])转换为double类型数, 即(char) array-> float(函数)atoi
字符串转换为整型(函数)atol
字符串转换为long类型(函数)atoll (c++11)
字符串转换为长整型long long类型(函数)strtod
字符串转换为double类型(函数)strtof (c++11)
字符串转换为浮点类型(函数)strtol
字符串转换为long integer类型(函数)strtold (c++11)
字符串转换为long double类型(函数)strtoll (c++11)
字符串转换为long long integer类型(函数)strtoul
字符串转换为无符号long integer类型(函数)strtoull (c++11)
字符串转换为无符号long long integer类型(函数)
函数(非标准):
itoa
将整数转换为字符串.
注: itoa并不是一个标准的C函数,它是Windows特有的,如果要写跨平台的程序,请用sprintf。
伪随机序列生成
rand
产生一个伪随机数(函数)srand
初始化随机数生成器, 为伪随机数生成器设置种子(函数)
内存管理">动态内存管理
calloc
分配内存且用 0 数组初始化(函数)
free
释放内存(函数)
malloc
分配内存(函数)
realloc
重分配内存(函数)
系统环境
abort
使程序异常中止,不会主动释放资源(函数)
atexit
注册一个一旦程序发起
exit
请求就会调用的函数(函数)at_quick_exit (C++11)
注册一个一旦程序发起
quick_exit
请求就会调用的函数(函数)exit
使程序正常中止,主动释放资源(函数)
getenv
获取系统环境变量列表(函数)
quick_exit (C++11)
使程序正常中止,不会主动释放所有资源(函数)
system
执行系统命令(函数)
_Exit (C++11)
使程序正常中止,不会主动释放资源(函数)
搜索和排序
bsearch
在数组中进行二分(Binary)查找(函数)
qsort
对数组元素进行快速排序(函数)
整数算术
abs
获得整数(Integer)或浮点数(Floating point number)的绝对值(函数)
div
同时获得两数相除的商(Quotient)及余数(Remainder)(函数)
labs
获得整数的绝对值(函数)
ldiv
同时获得两数相除的商及余数(函数)
llabs
获得整数的绝对值(函数)
lldiv
同时获得两数相除的商及余数(函数)
多字节(Multibyte)字符
mblen
返回下一个多字节字符的字节数(函数)
mbtowc
将下一个多字节字符转化成宽字符(函数)
wctomb
将一个宽字符转化成对应的多字节字符(函数)
多字节字符串
mbstowcs
将一个多字节字符串转化成宽字符串(函数)
wcstombs
将一个宽字符串转化成多字节字符串(函数)
宏常数
EXIT_FAILURE
程序失败终止码, 表明一个程序执行失败 (macro)
EXIT_SUCCESS
程序成功终止码, 表明一个程序非正常完成执行 (macro)
MB_CUR_MAX
多字节字符的最大尺寸 (macro)
NULL
空指针 (macro)
RAND_MAX
std::rand
可能产生的最大随机数值 (macro)
类型(type)
类型 | 描述 |
---|---|
div_t | 结构体类型,由 div 函数返回 |
ldiv_t | 结构体类型,由 ldiv 函数返回 |
lldiv_t | 结构体类型,由 lldiv 函数返回 |
size_t | 无符号整型 (type ) |
部分函数的具体函数原型:
序号 | 函数 & 描述 |
---|---|
1 | double atof(const char str)把参数 *str 所指向的字符串转换为一个浮点数(类型为 double 型)。 |
2 | int atoi(const char str)把参数 *str 所指向的字符串转换为一个整数(类型为 int 型)。 |
3 | long int atol(const char str)把参数 *str 所指向的字符串转换为一个长整数(类型为 long int 型)。 |
4 | double strtod(const char str, char **endptr)把参数 *str 所指向的字符串转换为一个浮点数(类型为 double 型)。 |
5 | long int strtol(const char str, char **endptr, int base)把参数 *str 所指向的字符串转换为一个长整数(类型为 long int 型)。 |
6 | unsigned long int strtoul(const char str, char **endptr, int base)把参数 *str 所指向的字符串转换为一个无符号长整数(类型为 unsigned long int 型)。 |
7 | void *calloc(size_t nitems, size_t size)分配所需的内存空间,并返回一个指向它的指针。 |
8 | void free(void ptr)释放之前调用 *calloc、malloc 或 realloc 所分配的内存空间。 |
9 | void *malloc(size_t size)分配所需的内存空间,并返回一个指向它的指针。 |
10 | void realloc(void *ptr, size_t size)尝试重新调整之前调用 *malloc 或 calloc 所分配的 ptr 所指向的内存块的大小。 |
11 | void abort(void)使一个异常程序终止。 |
12 | int atexit(void (*func)(void))当程序正常终止时,调用指定的函数 func。 |
13 | void exit(int status)使程序正常终止。 |
14 | char *getenv(const char *name)搜索 name 所指向的环境字符串,并返回相关的值给字符串。 |
15 | int system(const char *string)由 string 指定的命令传给要被命令处理器执行的主机环境。 |
16 | void bsearch(const void *key, const void *base, size_t nitems, size_t size, int (*compar)(const void , const void *))执行二分查找。 |
17 | void qsort(void base, size_t nitems, size_t size, int (*compar)(const void , const void*))数组排序。 |
18 | int abs(int x)返回 x 的绝对值。 |
19 | div_t div(int numer, int denom)分子除以分母。 |
20 | long int labs(long int x)返回 x 的绝对值。 |
21 | ldiv_t ldiv(long int numer, long int denom)分子除以分母。 |
22 | int rand(void)返回一个范围在 0 到 RAND_MAX 之间的伪随机数。 |
23 | void srand(unsigned int seed)该函数播种由函数 rand 使用的随机数发生器。 |
24 | int mblen(const char str, size_t n)返回参数 *str 所指向的多字节字符的长度。 |
25 | size_t mbstowcs(schar_t pwcs, const char *str, size_t n)把参数 *str 所指向的多字节字符的字符串转换为参数 pwcs 所指向的数组。 |
26 | int mbtowc(whcar_t pwc, const char *str, size_t n)检查参数 *str 所指向的多字节字符。 |
27 | size_t wcstombs(char str, const wchar_t *pwcs, size_t n)把数组 *pwcs 中存储的编码转换为多字节字符,并把它们存储在字符串 str 中。 |
28 | int wctomb(char str, wchar_t wchar)检查对应于参数 *wchar 所给出的多字节字符的编码。 |
参考链接: http://www.cplusplus.com/reference/cstdlib/