介绍 C++ 头文件 — <cstdlib> 头文件

介绍 C++ 头文件 — <cstdlib> 头文件

<cstdlib> 头文件

简介

C++ 标准库头文件 <cstdlib> 是 C++ 程序员使用最广泛的库头文件。它定义了一系列函数和宏,以实现跨团队、跨平台的高效且具有卓越表现的标准化 C++ 代码。

C++ 是一种广受欢迎的程序语言,它能崛起的最初原因就是可以与 C 语言兼容。C 语言曾经是且现在仍然是一种流行、成熟的程序语言。兼容意味着程序员更容易适应这种语言,更重要的是,C++ 开发人员还可以利用现有的 C 语言代码。

程序员不需要从核心函数开始重建所有内容,可以在按合理步调转向 C++ 时,重复使用成熟的代码块。具体来讲,他们能够利用 C 语言标准库头文件 <stdlib.h>

目前,C++ cstdlib 是原始 <stdlib.h> 的 C++ 增强版。

命名空间和宏

namespace std {
    using size_t = see definition;
    using div_t = see definition;
    using ldiv_t = see definition;
    using lldiv_t = see definition;
}

#define NULL
#define EXIT_FAILURE
#define EXIT_SUCCESS
#define RAND_MAX
#define MB_CUR_MAX

宏常量

宏常量用途
EXIT_SUCCESSEXIT_FAILURE指示程序执行的执行状态
MB_CUR_MAX当前本地环境中多字节字符的最大字节数
NULL实现定义的空指针常量
RAND_MAXstd::rand 生成的最大可能值

类型

类型用途
div_t结构体类型,std::div 函数的返回值
ldiv_t结构体类型,std::ldiv 函数的返回值
lldiv_t结构体类型,std::lldiv 函数的返回值
size_tsizeof 运算符返回的无符号整数类型

函数

进程控制

函数用途
abort导致非正常的程序终止(不进行清理)
exit导致正常的程序终止并进行清理
quick_exit导致快速程序终止,不进行完全的清理
_Exit导致正常的程序终止,不进行清理
atexit注册将于调用 std::exit() 时被调用的函数
at_quick_exit注册将于调用 quick_exit 时被调用的函数
system调用宿主环境的命令处理器
getenv访问环境变量列表

内存管理

函数用途
malloc分配内存
aligned_alloc分配对齐的内存
calloc分配并清零内存
realloc扩张先前分配的内存块
free解分配先前分配的内存

数值字符串转换

函数用途
atof转换字节字符串为浮点值
atoiatolatoll转换字节字符串为整数值
strtolstrtoll转换字节字符串为整数值
strtoulstrtoull转换字节字符串为无符号整数值
strtofstrtodstrtold转换字节字符串为浮点值

宽字符串操作

函数用途
mblen返回下一个多字节字符中的字节数
mbtowc将下一个多字节字符转换成宽字符
wctomb转换宽字符为其多字节表示
mbstowcs转换窄多字节字符串为宽字符串
wcstombs转换宽字符串为窄多字节字符串

杂项算法与数学

函数用途
rand生成伪随机数
srand初始化伪随机数生成器
qsort对未指定类型的元素的一个范围进行排序
bsearch在未指定类型的数组中搜索元素
abslabsllabs计算整数值的绝对值
divldivlldiv计算整数除法的商和余数

概要

namespace std {
  using size_t = ;
  using div_t = ;
  using ldiv_t = ;
  using lldiv_t = ;
}
#define NULL 
#define EXIT_FAILURE 
#define EXIT_SUCCESS 
#define RAND_MAX 
#define MB_CUR_MAX 
namespace std {
  extern "C" using /*c-atexit-handler*/ = void(); 
  extern "C++" using /*atexit-handler*/ = void(); 
  extern "C" using /*c-compare-pred*/ = int(const void* , const void*); 
  extern "C++" using /*compare-pred*/ = int(const void* , const void*); 
  // 启动与终止
  [[noreturn]] void abort() noexcept;
  int atexit(/*c-atexit-handler*/ * func) noexcept;
  int atexit(/*atexit-handler*/ * func) noexcept;
  int at_quick_exit(/*c-atexit-handler*/ * func) noexcept;
  int at_quick_exit(/*atexit-handler*/ * func) noexcept;
  [[noreturn]] void exit(int status);
  [[noreturn]] void _Exit(int status) noexcept;
  [[noreturn]] void quick_exit(int status) noexcept;
  char* getenv(const char* name);
  int system(const char* string);
  // C 标准库内存分配
  void* aligned_alloc(size_t alignment, size_t size);
  void* calloc(size_t nmemb, size_t size);
  void free(void* ptr);
  void* malloc(size_t size);
  void* realloc(void* ptr, size_t size);
  double atof(const char* nptr);
  int atoi(const char* nptr);
  long int atol(const char* nptr);
  long long int atoll(const char* nptr);
  double strtod(const char* nptr, char** endptr);
  float strtof(const char* nptr, char** endptr);
  long double strtold(const char* nptr, char** endptr);
  long int strtol(const char* nptr, char** endptr, int base);
  long long int strtoll(const char* nptr, char** endptr, int base);
  unsigned long int strtoul(const char* nptr, char** endptr, int base);
  unsigned long long int strtoull(const char* nptr, char** endptr, int base);
  // 多字节/宽字符串及字符转换函数
  int mblen(const char* s, size_t n);
  int mbtowc(wchar_t* pwc, const char* s, size_t n);
  int wctomb(char* s, wchar_t wchar);
  size_t mbstowcs(wchar_t* pwcs, const char* s, size_t n);
  size_t wcstombs(char* s, const wchar_t* pwcs, size_t n);
  // C 标准库算法
  void* bsearch(const void* key, const void* base,
                size_t nmemb, size_t size, /*c-compare-pred*/ * compar);
  void* bsearch(const void* key, const void* base,
                size_t nmemb, size_t size, /*compare-pred*/ * compar);
  void qsort(void* base, size_t nmemb, size_t size, /*c-compare-pred*/ * compar);
  void qsort(void* base, size_t nmemb, size_t size, /*compare-pred*/ * compar);
  // 低质量随机数生成
  int rand();
  void srand(unsigned int seed);
  // 绝对值
  int abs(int j);
  long int abs(long int j);
  long long int abs(long long int j);
  float abs(float j);
  double abs(double j);
  long double abs(long double j);
  long int labs(long int j);
  long long int llabs(long long int j);
  div_t div(int numer, int denom);
  ldiv_t div(long int numer, long int denom);
  lldiv_t div(long long int numer, long long int denom); 
  ldiv_t ldiv(long int numer, long int denom);
  lldiv_t lldiv(long long int numer, long long int denom);
}

参考

  • https://c-cpp.com/cpp/utility/program.html

  • https://zhuanlan.zhihu.com/p/447992440

  • https://c-cpp.com/cpp/numeric/random.html

  • https://learn.microsoft.com/zh-cn/cpp/standard-library/cstdlib?view=msvc-170

  • https://baike.baidu.com/item/cstdlib/5519425?fr=ge_ala

  • 4
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值