C++標準庫 Utilities library

該系列博文主要參考自 cppreference.com 和 cplusplus.com
由於博主水平有限,內容僅供參考


cstdlib

這個頭文件比較複雜,有各種東西。


String conversion (字符串轉換)


atof   atoi   atol   atoll(c++11)
函數原型:
double atof(const char *str);  (該函數較爲特殊,atof卻轉爲了double類型,其餘均正常)
作用:
字符串轉爲double類型。

strtod   strtof(c++11)   strtol   strtold(c++11)   strtoll(c++11)   strtoul   strtoull(c++11)
函數原型:
double strtod(const char *str, char **endptr);
作用:
字符串轉爲double類型,endptr指向剩餘部分的開頭。(其餘類似)

Pseudo-random sequence generation(僞隨機序列的生成)
rand   srand
函數原型:
int rand();
void srand(unsigned int seed);
作用:
這兩個函數用於產生隨機數。srand函數來設置隨機種子,一般選取系統時間,rand函數產生隨機數。

Dynamic memory management(動態內存管理)
malloc   calloc   realloc   free
函數原型:
void *malloc(size_t size);
void *calloc(size_t num, size_t size);
void *realloc(void *prt, size_t size);
void free(void *ptr);
作用:
malloc:分配size字節大小的空閒存儲空間,若成功,返回內存塊的最低地址,若失敗,返回NULL
calloc:分配num個size字節大小的存儲空間,若成功,返回內存塊的最低低至,若失敗,返回NULL
realloc:重新分配size字節大小的存儲空間,若成功,返回內存塊的最低地址,若失敗,返回NULL
free:釋放以上三個函數臨時分配的空間

Environment(環境)
abort   exit quick_exit(c++11)   _Exit(c++11)   atexit   at_quick_exit(c++11)   system   getenv
函數原型:
void abort();
void exit(int exit_code);
void quick_exit(int exit_code);
void _Exit(int exit_code);
int atexit(void (*func)());
int at_quick_exit(void (*func)());
int system(const char *command);
const char *getenv(const char *env_var);
作用:
abort:導致程序異常終止,不進行清理
exit:導致程序正常終止,進行清理
quick_exit:導致程序正常終止,不完全清理
_Exit:導致程序正常終止,不進行清理
atexit:程序正常終止時,執行函數func
at_qucik_exit:程序被quick_exit終止時,執行函數func
system:執行系統命令
getenv:獲得環境變量

Searching and sorting(查找與排序)
bsearch   qsort
函數原型:
void *bsearch(const void *key, const void *base, size_t num, size_t size, int (*compar)(const void *,const void *));
void qsort(void *base, size_t num, size_t size, int (*compar)(const void *, const void *));
作用:
二分查找與快排(這裏不做贅述)

Integer arithmethics(整數運算)
abs   div   labs   ldiv   llabs   lldiv
函數原型:
int abs(int n);
div_t div(int numer, int denom);
(其餘類似)
作用:
取絕對值與整除(div_t後文會講到)

Multibyte characters(多字節字符)
mblen   mbtowc   wctomb
函數原型:
int mblen(const char *pmb, size_t max);
int mbtowc(wchar_t *pwc, char *pmb, size_t max);
int wctomb(char *pmb, wchar_t wc);
作用:
mblen:獲得多字節字符的長度
mbtowc:多字節序列轉換爲寬字符
wctomb:寬序列轉換爲多字節字符

Multibyte strings(多字節字符串)

mbstowcs   wcstombs
函數原型:
size_t mbstowcs(wchar_t *dest, const char *src, size_t max);
size_t wcstombs(char *dest, const wchar_t *src, size_t max);
作用;
mbstowcs:多字節字符字符串轉換爲寬字符字符串
wcstombs:寬字符字符串轉換爲多字節字符字符串

Macro constants(宏常量)
EXIT_FAILURE   EXIT_SUCCESS   MB_CUR_MAX   NULL   RAND_MAX
定義:
EXIT_FAILURE 錯誤結束
EXIT_SUCCESS 正確結束
MB_CUR_MAX 多字節字符的最大值
NULL 空字符串
RAND_MAX rand函數返回的最大值

Types(類型)
div_t   ldiv_t   lldiv_t(c++11)   size_t
定義:
typedef struct {
  int quot;
  int rem;
} div_t;
ldiv_t lldiv_t 類似
typedef unsigned int size_t;
作用:
div_t 爲div函數的返回值類型,quot爲商,rem爲餘數





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章