C/C++部分库函数总结

来源:互联网 发布:手机淘宝小二在哪里找 编辑:程序博客网 时间:2024/05/19 06:17

C/C++部分库函数总结 

                                                           作者:xhanwu    日期:2007-04-23  

下面是部分C/C++函数库的总结,希望对学习C或C++的朋友们有点帮助.

下表根据函数用途进行分类(而非所属的类库)

数学函数

函数声明

说明

头文件

int abs(int)

绝对值

cstdlib

long labs(long)

绝对值

cstdlib

double fabs(double)

绝对值

cmath

double sqrt(double)

平方根

cmath

double pow(double,double)

返回第1个参数的第二个参数次幂

cmath

double exp(double)

返回指数,以e为底

cmath

double log(double)

自然对数(ln

cmath

double log10(double)

10为底的对数

cmath

double floor(double)

返回小于或者等于参数的最大整数

cmath

double ceil(double)

返回大于或者等于参数的最小整数

cmath

 

字符函数

   在所有情况下,参数的实际类型为int,但是针对大多数用途,都可以将参数视为char,假如返回的是一个int类型的值,必须显式或者隐式的进行类型转换,从而获得一个char类型。

函数声明

说明

头文件

bool isalnum(char)

如果参数满足isaphaisdigit,则返回true,否则返回false

cctype

bool isalphachar

如果是一个大写或者小写字母,则返回true,否则返回false

cctype

bool isdigit(char)

如果参数是数字,则返回true,否则返回false

cctype

bool isspace(char)

如果参数是空白字符,则返回true,否则返回false

cctype

bool iscntr(char)

如果参数是控制字符,则返回true,否则返回false

cctype

bool islower(char)

如果参数是小写字母,则返回true,否则返回false

cctype

bool isupper(char)

如果参数是大写字母,则返回true,否则返回false

cctype

int tolower(char)

返回参数的小写字母

cctype

int toupper(char)

返回参数的大写字母

cctype

 

字符串函数(一)

函数声明

说明

头文件

int atoi(const char a[])

将字符串(字符数组)转换成int类型

cstdlib

long atol(const char a[])

将字符串(字符数组)转换成long类型

cstdlib

double atof(const char a[])

将字符串(字符数组)转换成double类型

cstdlib

 

字符串函数(二)

函数调用形式

说明

头文件

strcat(string1,string2)

string2的值加到string1字符串的末尾

cstring

bool strcmp(string1,string2)

比较两个字符串,如果相同则返回true,否则返回false

cstring

strcpy(string1,string2)

string1的值更改为string2的值

cstring

int strlen(string1)

返回stirng1的长度

cstring

strncat(string1,string2)

strcat相同,但是最多只能追加limit个字符

cstring

strncmp(string1,string2,limit)

strncmp相同,但是最多只能比较limit个字符

cstring

strncpy(string1,string2,limit)

strncmp相同,但是最多只能替换limit个字符

cstring

strstr(string1,string2,pattern)

返回一个指向string2中出现的第一个string1的指针,如果没有找到pattern,则返回NULL指针

cstring

strchr(string1,character)

返回一个指向string1中出现的第一个character的指针,如果没有找到character,则返回NULL指针

cstring

char* strrchar(string1,character)

返回一个指向string1中出现的最后一个character的指针,如果没有找到character,则返回NULL指针

cstring

 

随机数生成器

函数声明

说明

头文件

int random(int )

调用random(n)会返回一个大于或等于0,同时小于或等于n-1的伪随机数(0<= random(n) <= n-1

cstdlib

int rand()

调用rand(),返回>=0 同时<= RAND_MAZ的一个伪随机数。(其中RAND_MAX是预定义整数常量,该常量定义在头文件cstdlib中)

cstdlib

void srand(unsigned int)

(unsigned int类型只允许非负整数)

根据参数重新初始化伪随机数生成器。参数是种子数。

cstdlib

 

 

 

 

三角函数

  这些函数使用的都是弧度,而不是角度

函数声明

说明

头文件

double cos(double)

余弦

 

double sin(double)

正弦

 

double tan(double)

正切

 

double acos(double)

反余弦

 

double asin(double)

反正弦

 

double atan(double)

反正切

 

double coshdouble

双曲余弦

 

double sinh(double)

双曲正弦

 

double tanh(double)

双曲正切

 

 

输入输出函数

函数声明

说明

头文件

streamVar.open(

external_File_Name)

将名为external_File_Name文件连接到streamVar流中

fstream

streamVar.fail()

如果在前面的一个对流的操作失败,则返回true

fstreamiostream

streamVar.close()

将流streamVar与文件断开连接

fstreamiostream

streamVar.bad()

如果流streamVar被破坏,则返回true

fstreamiostream

streamVar.eof()

在读入文件时如果到达文件最后一个字符,将返回true,否则返回false

fstreamiostream

streamVar.get(charVar)

从流streamVar中读取一个字符,并将该字符存入charVar

fstreamiostream

streamVar.getline(stringVar,

MAX_characters+1)

从流streamVar中读取一行字符,并将字符串放在stringVar中,如果这一行的长度超过Max_Character,只读取前Max_Characters+1个字符

fstreamiostream

char streamVar.peek()

从输入流streamVar中读取一个字符,并返回该字符。但是流位置没有前移一个位置

fstreamiostream

streamVar.put(char)

charVar值写入输出流streamVar

fstreamiostream

streamVar.putback(charVar)

charVal的值放入输入流streamVar中,使那个值成为从流中读取的下一个值。连接到流的文件没有改变。

fstreamiostream

streamVar.precision(int_Exp)

将发送到输出流的浮点数指定小数点後的位数

fstreamiostream

streamVar.width(int_Exp)

为输出流streamVar的下一个值设置域宽

fstreamiostream