介绍 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_MAX std::rand 生成的最大可能值

类型

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

函数

进程控制

函数 用途
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

猜你喜欢

转载自blog.csdn.net/ZH_qaq/article/details/132279981