Introduction to C++ header files— <cstdlib>
header files
<cstdlib>
head File
Introduction
The C++ Standard Library headers <cstdlib>
are the most widely used library headers by C++ programmers. It defines a set of functions and macros to enable efficient and performant standardized C++ code across teams and platforms.
C++ is a popular programming language, and the original reason for its rise is that it can be compatible with the C language. The C language was and still is a popular, mature programming language. Compatibility means that it is easier for programmers to adapt to the language, and more importantly, C++ developers can also leverage existing C code.
Programmers don't need to rebuild everything from the core functions, and can reuse mature code blocks as they move to C++ at a reasonable pace. Specifically, they can take advantage of the C language standard library header files<stdlib.h>
Currently, the C++ cstdlib is <stdlib.h>
an enhanced version of the original C++.
namespaces and macros
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
macro constant
macro constant | use |
---|---|
EXIT_SUCCESS 、EXIT_FAILURE |
Indicates the execution status of program execution |
MB_CUR_MAX |
The maximum number of bytes for a multibyte character in the current locale |
NULL |
implementation-defined null pointer constant |
RAND_MAX |
std::rand The largest possible value generated |
type
type | use |
---|---|
div_t |
Structure type, std::div function return value |
ldiv_t |
Structure type, std::ldiv function return value |
lldiv_t |
Structure type, std::lldiv function return value |
size_t |
sizeof the unsigned integer type returned by the operator |
function
process control
function | use |
---|---|
abort |
Causes an abnormal program termination (without cleanup) |
exit |
Causes normal program termination and cleanup |
quick_exit |
Causes quick program termination without complete cleanup |
_Exit |
Causes normal program termination without cleanup |
atexit |
registers std::exit() a function that will be called when |
at_quick_exit |
Register a function that will be called when quick_exit is called |
system |
Invoke the host environment's command handler |
getenv |
Access the list of environment variables |
memory management
function | use |
---|---|
malloc |
Allocate memory |
aligned_alloc |
allocate aligned memory |
calloc |
Allocate and zero memory |
realloc |
Expand a previously allocated block of memory |
free |
Deallocates previously allocated memory |
Numeric String Conversion
function | use |
---|---|
atof |
converts a byte string to a floating point value |
atoi 、atol 、atoll |
Converts a byte string to an integer value |
strtol 、strtoll |
Converts a byte string to an integer value |
strtoul 、strtoull |
converts a byte string to an unsigned integer value |
strtof 、strtod 、strtold |
converts a byte string to a floating point value |
Wide String Operations
function | use |
---|---|
mblen |
Returns the number of bytes in the next multibyte character |
mbtowc |
Convert the next multibyte character to a wide character |
wctomb |
converts a wide character to its multibyte representation |
mbstowcs |
converts a narrow multibyte string to a wide string |
wcstombs |
converts a wide string to a narrow multibyte string |
Miscellaneous Algorithms and Mathematics
function | use |
---|---|
rand |
生成伪随机数 |
srand |
初始化伪随机数生成器 |
qsort |
对未指定类型的元素的一个范围进行排序 |
bsearch |
在未指定类型的数组中搜索元素 |
abs 、labs 、llabs |
计算整数值的绝对值 |
div 、ldiv 、lldiv |
计算整数除法的商和余数 |
概要
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