C++中常用函數總結(頭文件)

1 algorithm

1.1 函數

類型 函數 說明 舉例
最大值 Int/double = max( a,b) 結果類型和a、b對應 (int/ double); <cmath>也相同
最小值 Int/double = min( a,b)
絕對值 int = abs(int) 取絕對值(整數)
交換 swap(x, y) 基本數據類型均可:char 、string string str1="abc", str2="def"; swap(str1, str2);
逆轉 reverse( it1, it2 ) 數組、string int arr[4] = {1,2,3,4}; reverse(arr,arr+4);
string str = "abc"; reverse(str.begin(),str.end());
賦值 fill( it1, it2, x) 在[t1, t2) 都賦值x int arr[4]; fill(arr, arr+4, 0);
排序 sort( it1, it2, cmp) cmp可省略,默認升序
排列 下一輪排列數 string str="123"; next_permutation(str.begin(),str.end()); printf("%s", str.c_str());
查找(>=x) * 或 it = lower_bound( it1, it2, x) 在[t1, t2) 中,返回第一個 >= x的元素在的地方(指針 or 迭代器);若不存在,返回假設存在時應該在的地方
查找(>x) *或it = upper_bound( it1, it2, x) 在[t1, t2) 中,返回第一個 > x的元素在的地方(指針 or 迭代器);若不存在,返回假設存在時應該在的地方

1.2 代碼示例

#include<cstdio>
#include<algorithm>
#include<string>

using namespace std;

struct Node{
	int x,y;
	Node(int _x, int _y):x(_x), y(_y){}
    Node(){}
}nodes[3];

// 降序: x, 升序:y
bool node_cmp(Node no1, Node no2){
	if(no1.x!=no2.x){
		return no1.x > no2.x;
	}else{
		return no1.y < no2.y;
	}
}

int main(){
	/*
	fill
	*/
	int a[10];
	int b[5][10];
	fill(a, a+10, 2);
	fill(b[0], b[0]+5*10, 2);

    /*
    next_permutation
    */
    // 全排列
    string str="123";
    while(next_permutation(str.begin(),str.end())){
		printf("%s\n", str.c_str());
	}


	/*
	sort
	*/
    nodes[0] = Node(1, 2);
    nodes[1] = Node(1, 0);
    nodes[2] = Node(-1, 3);
    sort(nodes,nodes+3,node_cmp);
	for(int i=0;i<3;i++){
        printf("%d %d\n", nodes[i].x, nodes[i].y);
	}

	
	/*
	lower_bound、upper_bound
	*/
	int arr[4] = {1,2,2,3};
	int *p = lower_bound(arr, arr+4, 2);        // 第一個>=2
	int *q = upper_bound(arr, arr+4, 2);        // 第一個>2
    printf("%d %d\n", p-arr, q-arr);	// 元素位置


    return 0;
}


2 cmath(c++) / math(c)

2.1 函數

類型 函數
都是double/float類型
說明 舉例
絕對值 double = fabs(double x) 取絕對值(浮點數)
次方 double = pow(double x, double p) x的p次方
算術平方根 double = sqrt(double x) 根號下x
取整 (向下 / 小) double = floor(double x) 相當於 int(double x) 2 = floor(2.2) = int(2.2)
-3 = floor(-2.2)
取整 (向上 / 大) double = ceil(double x)
取整(四捨六入五成雙) double = round(double x) 四捨六入五成雙(不是四捨五入)
對數 double = log(double x) 以e爲底的對數 改寫:以a爲底的b的對數
double = log(double b)/log(double a)
三角函數 double = sin(double x)
double = asin(double x)
cos() acos()
tan() atan()

2.2 代碼示例

#include<cstdio>
#include<cmath>

using namespace std;

const double PI = acos(-1.0);

double round_up_down(double x){
	return (x>0.0)?floor(x+0.5):ceil(x-0.5);
}


int main(){
    /*
    四捨五入(不是round函數)
    */
    double db1 = round_up_down(1.5);
    printf("%f", db1);


    /*
    取整
    */
    double x = 2.2;
	printf("%f %d %f %f\n", ceil(x),int(x),floor(x),round(x));  //  3.000000     2      2.000000    2.000000
	x = 2.5;
	printf("%f %d %f %f\n", ceil(x),int(x),floor(x),round(x));  //  3.000000    2      2.000000     3.000000
	// 整數
	x = 2.0;
	printf("%f %d %f %f\n", ceil(x),int(x),floor(x),round(x));  //  2.000000    2       2.000000    2.000000
	
	
	/*
	三角函數
	*/
	double db = sin(PI * 30 / 180);    // db = sin(PI/6)
	printf("%f",db);

    return 0;
}


3 cstring (c++)/ string(c)

3.1 函數

類型 函數 說明 舉例
字符串數組:長度 strlen(char chs[]) 字符串實際字符數(不包括’\0’)
字符串數組:連接 strcat(char chs2[], char chs1[]) chs2 = chs2 + chs1
字符串數組:比較 int = strcmp(char chs1[], char chs2[]) 返回<0 ,則 chs1 < chs2
字符串數組:複製 strcpy(char chs2[], char chs1[]) chs2 = chs1(包括’\0’)
賦值 memset(arr, 0或-1, sizeof(arr)) 只能賦值0或-1
字符串 ——> 整數/浮點數 sscanf(str, “%d”, &n);
sscanf(str, “%lf”, &db);
str只能是字符串數組char*,不能是string
整數/浮點數 ——> 字符串 sprintf(str, “%d”, n);
sscanf(str, “%f”, db);
str只能是字符串數組char*,不能是string

注:
sscanf 和 sprintf記憶技巧:

scanf("%d", &n);		  	// 鍵盤 ——> n變量
sscanf(str, "%d", &n);		// str ——> n變量

printf("%d", n);			// n變量 ——> 屏幕
sprintf(str, "%d", n);		// n變量 ——> str

總結: 相當於鍵盤/屏幕是 str

3.2 代碼示例

#include<cstdio>
#include<cstring>

using namespace std;

int main(){
    /*
    memset
    */
    int arr1[4], arr2[4][4];
    // 一維
    memset(arr1, 0, sizeof(arr1));  // sizeof(arr1) = 16 : 字節數 = 4*int
    // 二維
    memset(arr2, -1, sizeof(arr2));
    for(int i=0; i<4; i++){
		printf("%d\n", arr1[i]);
	}
	for(int i=0; i<4;i++){
		for(int j=0; j<4; j++){
			printf("%d ",arr2[i][j]);
		}
		printf("\n");
	}


	/*
    sscanf
	*/
	int n;
	double db;
	char chs[20] = "222:3.14:hello";
	char chs2[20];
	sscanf(chs,"%d:%lf:%s",&n,&db,chs2);	//chs 轉換爲 n、db、chs2
	printf("%d %f %s\n",n,db,chs2);
	//
	sprintf(chs, "%d-%lf-%s",n,db,chs2);
	printf("%s\n", chs);

	
    return 0;
}


4 iomanip

cout 控制輸出精讀(保留小數點幾位)

#include<iostream>
#include<iomanip>

using namespace std;

int main(){
	// 輸出保留小數點 2 位
    double db = 2.5555;
    cout<<setiosflags(ios::fixed)<<setprecision(2)<<db;

    return 0;
}

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