C ++ standard library utilities Library

Source: Internet
Author: User
Tags random seed

This series of blog posts mainly refer to from cppreference.com and cplusplus.com
The level of bloggers is limited. The content is for reference only.

Cstdlib

This header file is complicated and has a variety of things.


String Conversion (String Conversion)


Atof atoi atol Atoll (C ++ 11)
Function prototype:
Double atof (const char * Str); (this function is special, but atof is converted to the double type, and the rest are normal)
Purpose:
Convert string to double type.

Strtowstrtof (C ++ 11) strtol strtold (C ++ 11) strtoll (C ++ 11) strtoul strtoull (C ++ 11)
Function prototype:
Double strtodd (const char * STR, char ** endptr );
Purpose:
String to double type, endptr points to the beginning of the remaining part. (Others are similar)

Pseudo-random sequence generation (generation of pseudo-random sequences)
Rand srand
Function prototype:
Int rand ();
Void srand (unsigned int seed );
Purpose:
These two functions are used to generate random numbers. The srand function is used to set Random Seed. Generally, the system time is selected, and the rand function generates a random number.

Dynamic Memory Management (dynamic memory management)
Malloc calloc realloc free
Function prototype:
Void * malloc (size_t size );
Void * calloc (size_t num, size_t size );
Void * realloc (void * PRT, size_t size );
Void free (void * PTR );
Purpose:
Malloc: allocate the free storage space of size bytes. If the storage space is successful, the lowest address of the memory block is returned. If the storage space fails, null is returned.
Calloc: allocate num storage space of size bytes. If the storage space is successful, the returned memory block is as low as possible. If the storage space fails, null is returned.
Realloc: reallocates the size-byte storage space. If the storage space is successfully allocated, the lowest address of the memory block is returned. If the storage space fails, null is returned.
Free: Release the space temporarily allocated by the preceding three functions.

Environment (Environment)
Abort exit quick_exit (C ++ 11) _ exit (C ++ 11) atexit at_quick_exit (C ++ 11) system getenv
Function prototype:
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 );
Purpose:
Abort: causes abnormal program termination and does not clean up
Exit: causes the program to terminate normally and cleans up the program.
Quick_exit: Causes normal program termination and incomplete cleaning
_ Exit: causes the program to terminate normally and does not clean up.
Atexit: when the program ends normally, the function func is executed.
At_qucik_exit: when the program is terminated by quick_exit, the function func is executed.
System: execute system commands
Getenv: Get Environment Variables

Searching and sorting (search and sorting)
Bsearch qsort
Function prototype:
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 *));
Purpose:
Binary Search and quick sorting (this is not described here)

Integer arithmethics)
ABS Div labs ldiv llabs lldiv
Function prototype:
Int ABS (int n );
Div_t Div (INT numer, int denom );
(Others are similar)
Purpose:
Take absolute value and Division (div_t will be discussed later)

Multibyte characters (Multi-byte characters)
Mblen mbtowc wctomb
Function prototype:
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 );
Purpose:
Mblen: Get the length of Multi-byte characters
Mbtowc: Convert multi-byte sequence to wide character
Wctomb: the wide sequence is converted to multi-byte characters.

Multibyte strings (Multi-byte string)

Mbstowcs wcstombs
Function prototype:
Size_t mbstowcs (wchar_t * DEST, const char * SRC, size_t max );
Size_t wcstombs (char * DEST, const wchar_t * SRC, size_t max );
Function;
Mbstowcs: convert a multi-byte character string to a wide character string
Wcstombs: converts a wide character string to a multi-byte character string.

Macro constants (macro constant)
Exit_failure exit_success mb_cur_max null rand_max
Definition:
Exit_failure error ended
Exit_success ends correctly
Maximum Value of mb_cur_max multi-byte characters
Null String
Maximum value returned by the rand_max rand function

Types (type)
Div_t ldiv_t lldiv_t (C ++ 11) size_t
Definition:
Typedef struct {
Int quot;
Int REM;
} Div_t;
Similar to ldiv_t lldiv_t
Typedef unsigned int size_t;
Purpose:
Div_t is the type of the DIV function's return value, quot; stands for the quotient, and REM stands for the remainder.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.