(1)字串連線函式 strcat、strncat
strcat(char[ ], const char[ ])
strncat(char[ ], const char[ ], int )
char str1[30]="people's republic of ";
char str2="china";
strcat(str1,str2);
cout<(2)字串複製函式 strcpy、strncpy
strcpy(char[ ], const char[ ])
strncpy(char[ ], const char[ ], int )
char str1[10],str2[10],str3="china";
strcpy(str1,str3);
cout<(3)字串比較函式 strcmp
strcmp(const char[ ],const char[ ])
strcmp(str1,str2);
1)如果str1=str2, 函式值=0;
2)如果str1>str2, 函式值》0;
3)如果str1(5)初始化函式memset
memset(char[ ], int ,int )
char str[30];
memset(str,'a',5);
cout<(6)字串分割
char *strtok(char src, const char *delim)
src為待分解的字串,delim為分隔符
由於c/c++沒有 split() 函式,這裡借用 strtok() 函式簡單實現一下:
int split(char *src,char *separator,char *dest)
return i;
}
(1)複製、連線、比較運算
string str1,str2,str3;
str1="abc"; //複製,abc
cout(2)查詢子串函式find、rfind
正向查詢
find(string )
find(string ,int )
string s="abcaabcddbc";
cout逆向查詢
find(string )
string s="abcaabcddbc";
cout注意:在條件控制語句中,不要將find()的返回值與0或-1作大小比較,而應與-1作等於或不等於比較。
string s="abcaabcddbc";
//正確做法
cout
replace(int ,int ,string )
string s="aaabbbccc";
s.replace(3,3,"**");
cout
(2)double fabs(double x) //返回雙精度引數x的絕對值
(3)long labs(long n) //返回長整型引數n的絕對值
(4)double exp(double x) //返回指數函式e^x的值
(5)double pow(double x,double y) //返回x^y的值
(6)double pow10(int p) //返回10^p的值
(7)double log(double x) //返回logex的值
(8)double log10(double x) //返回log10x的值
(9)double sqrt(double x) //返回√x的值
(10)int ceil(double x) //返回不小於x的最小整數
(11)int floor(double x) //返回不大於x的最大整數
(1)void exit(int code) //終止程式執行
(2)void * malloc(long numbytes) //申請記憶體
(3)void free(void *ptr) //釋放記憶體
#includeusing namespace std;
int main()
注意:new、delete、sizeof()都是運算子
(4)void srand(int seed) //隨機數生成器種子
(5)int rand(void) //生成0~32768之間的乙個隨機數
#include#include#includeusing namespace std;
int main();
int b=;
float f=;
char c="iebagdfhjc";
string s=;
sort(a,a+10); //公升序排序(整數)
sort(b,b+10,greater()); //降序排序(整數)
sort(f,f+10); //公升序排序(浮點數)
sort(c,c+10); //公升序排序(字元)
sort(s,s+5); //公升序排序(字串)
ofstream out("out.txt"); //檔案輸出流
for(i=0;i<5;i++)
out.close();
return 0;
}
C C 計時函式彙總
c c 計時函式比較 目前,存在著各種計時函式,一般的處理都是先呼叫計時函式,記下當前時間tstart,然後處理一段程式,再呼叫計時函式,記下處理後的時間tend,再tend和tstart做差,就可以得到程式的執行時間,但是各種計時函式的精度不一樣.下面對各種計時函式,做些簡單記錄.方法1,time...
常用函式彙總
常用內建 作業系統相關 檔案正規表示式 enumerate goods list coffee 10 chicken 20 iphone 8000 macpro 15000 car 100000 for i,good in enumerate goods list print s s i,good ...
C C 常用標頭檔案及其函式彙總(持續更新)
編號 函式原型 函式使用說明備註1 size t fwrite const void buffer,size t size,size t count,file stream 向指定的檔案中寫入若干資料塊,如成功執行則返回實際寫入的資料塊數目。寫入到檔案的 這個與檔案的開啟模式有關,如果是w 則是從f...