1. 程式人生 > >C++常見庫函式

C++常見庫函式

C++常用庫函式

 

1、常用數學函式

    標頭檔案 #include <math> 或者 #include <math.h>

 

函式原型

功能

返回值

int abs(int x)

求整數x的絕對值

絕對值

double acos(double x)

計算arcos(x)的值

計算結果

double asin(double x)

計算arsin(x)的值

計算結果

double atan(double x)

計算arctan(x)的值

計算結果

double cos(double x)

計算cos(x)的值

計算結果

double cosh(double x)

計算x的雙曲餘弦cosh(x)的值

計算結果

double exp(double x)

的值

計算結果

double fabs(double x)

求實數x

的絕對值

絕對值

double fmod(double x)

x/y的餘數

餘數的雙精度數

long labs(long x)

求長整型數的絕對值

絕對值

double log(double x)

計算In(x)的值

計算結果

double log10(double x)

計算的值

計算結果

double modf(double x, double *y)

x的整數部分送到y所指向的單元格中

x

的小樹部分

double pow(double x, double y)

                                     的值

計算結果

double sin(double x)

計算sin(x)的值

計算結果

double sqrt(double x)

的值

計算結果

double tan(double x)

計算tan(x)的值

計算結果

fcvt

將浮點型數轉化為字串

 

 

 

 

 

 

 

 

 

2、常用字串處理函式

標頭檔案 #include <string> 或者 #include <string.h>

 

函式原型

功能

返回值

void *memcpy(void *p1, const void *p2 size_t n)

儲存器拷貝,將p2所指向的共n個位元組拷貝到p1所指向的儲存區中

目的儲存區的起始地址

實現任意資料型別之間的拷貝

void *memset(void *p int v, size_t n)

v的值作為p所指向的區域的

值,np所指向區域的大小

該區域的起始地址

char *strcpy(char *p1, const char *p2)

p2所指向的字串拷貝到

p1所指向的儲存區中

目的儲存區的起始地址

char *strcat(char *p1, const

char *p2)

p2所指向的字串連線到

p1所指向的字串後面

目的儲存區的起始地址

int strcmp(const char *p1, const char *p2)

比較p1,p2所指向的兩個

字串的大小

兩個字串相同,返回0;若p1所指向的字串小於p2所指的字串,返回負值;否則,返回正值

int strlen(const char *p)

p所指向的字串的長度

字串所包含的字元個數

不包括字串結束標誌’\n’

char *strncpy(char *p1, const char *p2, size_t n)

p2所指向的字串(至多n個字元)拷貝到p1所指向的儲存區中

目的儲存區的起始地址

(strcpy()類似)

char *strncat(char *p1, const char *p2, size_t n)

p2所指向的字串(至多n個字元)連線到p1所指向的字串的後面

目的儲存區的起始地址

(strcpy()類似)

char *strncmp(const char *p1, const char *p2, size_t n)

比較p1,p2所指向的兩個字串的大小,至多比較n個字元

兩個字串相同,返回0;若p1所指向的字串小於p2所指的字串,返回負值;否則,返回正值

(strcpy()類似)

char *strstr(const char *p1, const char *p2)

判斷p2所指向的字串是否是p1所指向的字串的子串

若是子串,返回開始位置的地址;否則返回0

 

 

 

 

 

 

 

 

3、其他常用函式

標頭檔案#include <stdlib> 或者 #include <stdlib.h>

函式原型

功能

返回值

說明

void abort(void)

終止程式執行

 

不能結束工作

void exit(int)

終止程式執行

 

做結束工作

double atof(const char *s)

s所指向的字串轉換成實數

實數值

 

int atoi(const char *s)

s所指向的字串轉換成整數

整數值

 

long atol(const char *s)

s所指的字串轉換成長整數

長整數值

 

int rand(void)

產生一個隨機整數

隨機整數

 

void srand(unsigned int)

初始化隨機數產生器

 

 

int system(const char *s)

s所指向的字串作為一個可執行檔案,並加以執行

 

 

max(a, b)

求兩個數中的大數

大數

引數為任意型別

min(a,b)

求兩個數中的小數

小數

引數為任意型別

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

4、實現鍵盤和檔案輸入/輸出的成員函式

標頭檔案#include <iostream> 或者 #include <iostream.h>

函式原型

功能

返回值

cin >> v

輸入值送給變數

 

cout << exp

輸出表達式exp的值

 

istream & istream::get(char &c)

輸入字元送給變數c

 

istream & istream::get(char *, int , char = ‘\n’)

輸入一行字串

 

istream & istream::getline(char *, int , char = ‘\n’)

輸入一行字串

 

void ifstream::open(const char*,int=ios::in,

int = filebuf::openprot )

開啟輸入檔案

 

void ofstream::open(const char*,int=ios::out,

int = filebuf::openprot)

開啟輸出檔案

 

void fsream::open(const char*,int ,

int = filebuf::openprot)

開啟輸入/輸出檔案

 

ifstream::ifstream(const char*,int = ios::in,

int = filebuf::openprot)

建構函式開啟輸入檔案

 

ofstream::ofstream(const char*,int=ios::out,

int = filebuf::openprot)

建構函式開啟輸出函式

 

fstream::fstream(const char*, int,

int = filebuf::openprot)

建構函式開啟輸入/輸出檔案

 

void istream::close()

關閉輸入檔案

 

void ofsream::close()

關閉輸出檔案

 

void fsream::close()

關閉輸入/輸出檔案

 

istream & istream::read(char*, int)

從檔案中讀取資料

 

ostream & istream::write(const char*,int)

將資料寫入檔案中

 

int ios::eof()

判斷是否到達開啟檔案的尾部

1為到達2為沒有

istream & istream::seekg(streampos)

移動輸入檔案的指標

 

istream & istream::seekg(streamoff,ios::seek_dir)

移動輸入檔案的指標

 

streampos istream::tellg()

取輸入檔案的指標

 

ostream & ostream::seekp(streampos)

移動輸出檔案的指標

 

ostream & ostream::seekp(streamoff,ios::seek_dir)

移動輸出檔案的指標

 

streampos ostream::tellp()

取輸出檔案的指標

 

 

C++的標頭檔案! 
#include <ctype.h>//
字元處理 

#include <errno.h>//
定義錯誤碼 

#include <float.h>//
浮點數處理 

#include <fstream.h>//
檔案輸入/輸出 

#include <iomanip.h>//
引數化輸入/輸出 

#include <iostream.h> //
資料流輸入/輸出 

#include <limits.h> //
定義各種資料型別最值常量 

#include <locale.h> //
定義本地化函式 

#include <math.h> //
定義數學函式 

#include <stdio.h> //
定義輸入/輸出函式 

#include <stdlib.h> //
定義雜項函式及記憶體分配函式 

#include <string.h> //
字串處理 

#include <strstrea.h> //
基於陣列的輸入/輸出 

#include <time.h> //
定義關於時間的函式 

#include <wchar.h> //
寬字元處理及輸入/輸出 

#include <wctype.h> //
寬字元分類 


標準 C++ (同上的不再註釋) 

#include <algorithm> //STL 
通用演算法 

#include <bitset> //STL 
位集容器 

#include <cctype> 

#include <cerrno> 

#include <clocale> 

#include <cmath> 

#include <complex> //
複數類 

#include <cstdio> 

#include <cstdlib> 

#include <cstring> 

#include <ctime> 

#include <deque> //STL 
雙端佇列容器 

#include <exception> //
異常處理類 

#include <fstream> 
#include <functional> //STL 
定義運算函式(代替運算子) 

#include <limits> 

#include <list> //STL 
線性列表容器 

#include <map> //STL 
對映容器 

#include <iomanip> 

#include <ios> //
基本輸入/輸出支援 

#include <iosfwd> //
輸入/輸出系統使用的前置宣告 

#include <iostream> 

#include <istream> //
基本輸入流 

#include <ostream> //
基本輸出流 

#include <queue> //STL 
佇列容器 

#include <set> //STL 
集合容器 

#include <sstream> //
基於字串的流 

#include <stack> //STL 
堆疊容器 

#include <stdexcept> //
標準異常類 

#include <streambuf> //
底層輸入/輸出支援 

#include <string> //
字串類 

#include <utility> //STL 
通用模板類 

#include <vector> //STL 
動態陣列容器 

#include <cwchar> 

#include <cwctype> 

using namespace std; 


C99 
增加 

#include //
複數處理 
#include //
浮點環境 
#include //
整數格式轉換 
#include //
布林環境 
#include //
整型環境 
#include //
通用型別數學巨集數學函式,所在函式庫為math.hstdlib.hstring.hfloat.h
int abs(int i) 
返回整型引數i的絕對值
double cabs(struct complex znum) 返回複數znum的絕對值
double fabs(double x) 返回雙精度引數x的絕對值
long labs(long n) 返回長整型引數n的絕對值
double exp(double x) 返回指數函式ex的值
double frexp(double value,int *eptr) 返回value=x*2nx的值,n存貯在eptr
double ldexp(double value,int exp); 返回value*2exp的值
double log(double x) 返回logex的值
double log10(double x) 返回log10x的值
double pow(double x,double y) 返回xy的值
double pow10(int p) 返回10p的值
double sqrt(double x) 返回+√x的值
double acos(double x) 返回x的反餘弦cos-1(x),x為弧度
double asin(double x) 返回x的反正弦sin-1(x),x為弧度
double atan(double x) 返回x的反正切tan-1(x),x為弧度
double atan2(double y,double x) 返回y/x的反正切tan-1(x),yx為弧度
double cos(double x) 返回x的餘弦cos(x),x為弧度
double sin(double x) 返回x的正弦sin(x),x為弧度
double tan(double x) 返回x的正切tan(x),x為弧度
double cosh(double x) 返回x的雙曲餘弦cosh(x),x為弧度
double sinh(double x) 返回x的雙曲正弦sinh(x),x為弧度
double tanh(double x) 返回x的雙曲正切tanh(x),x為弧度
double hypot(double x,double y) 返回直角三角形斜邊的長度(z),
x
y為直角邊的長度,z2=x2+y2
double ceil(double x) 
返回不小於x的最小整數
double floor(double x) 返回不大於x的最大整數
void srand(unsigned seed) 初始化隨機數發生器
int rand() 產生一個隨機數並返回這個數
double poly(double x,int n,double c[])從引數產生一個多項式
double modf(double value,double *iptr)將雙精度數value分解成尾數和階
double fmod(double x,double y) 返回x/y的餘數
double frexp(double value,int *eptr) 將雙精度數value分成尾數和階
double atof(char *nptr) 將字串nptr轉換成浮點數並返回這個浮點數
double atoi(char *nptr) 將字串nptr轉換成整數並返回這個整數
double atol(char *nptr) 將字串nptr轉換成長整數並返回這個整數
char *ecvt(double value,int ndigit,int *decpt,int *sign)
將浮點數value轉換成字串並返回該字串
char *fcvt(double value,int ndigit,int *decpt,int *sign)
將浮點數value轉換成字串並返回該字串
char *gcvt(double value,int ndigit,char *buf)
將數value轉換成字串並存於buf,並返回buf的指標
char *ultoa(unsigned long value,char *string,int radix)
將無符號整型數value轉換成字串並返回該字串,radix為轉換時所用基數
char *ltoa(long value,char *string,int radix)
將長整型數value轉換成字串並返回該字串,radix為轉換時所用基數
char *itoa(int value,char *string,int radix)
將整數value轉換成字串存入string,radix為轉換時所用基數
double atof(char *nptr) 將字串nptr轉換成雙精度數,並返回這個數,錯誤返回0
int atoi(char *nptr) 
將字串nptr轉換成整型數並返回這個數,錯誤返回0
long atol(char *nptr) 
將字串nptr轉換成長整型數,並返回這個數,錯誤返回0
double strtod(char *str,char **endptr)
將字串str轉換成雙精度數,並返回這個數,
long strtol(char *str,char **endptr,int base)
將字串str轉換成長整型數,
並返回這個數,
int matherr(struct exception *e)
使用者修改數學錯誤返回資訊函式(沒有必要使用)
double _matherr(_mexcep why,char *fun,double *arg1p,
double *arg2p,double retval)
使用者修改數學錯誤返回資訊函式(沒有必要使用)
unsigned int _clear87() 
清除浮點狀態字並返回原來的浮點狀態
void _fpreset() 重新初使化浮點數學程式包
unsigned int _status87() 返回浮點狀態字目錄函式,所在函式庫為dir.hdos.h
int chdir(char *path) 
使指定的目錄path(如:"C:\\WPS")變成當前的工作目錄,
功返回0
int findfirst(char *pathname,struct ffblk *ffblk,int