C++中常用函数总结(头文件)

这篇博客总结了C++中常用的头文件函数,包括algorithm的常用操作,cmath库的数学函数,以及cstring中字符串处理的函数。还提到了记忆技巧,如sscanf和sprintf,以及iostream中的cout控制输出精度。
摘要由CSDN通过智能技术生成

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 、stringstring str1="abc", str2="def"; swap(str1, str2);
逆转reverse( it1, it2 )数组、stringint arr[4] = {1,2,3,4}; reverse(arr,arr+4);
string str = "abc"; reverse(str.begin(),str.end());
赋值fill( it1, it2, x)在[t1, t2) 都赋值xint arr[4]; fill(arr, arr+4, 0);
排序sort( it1, it2, cmp)cmp可省略,默认升序
排列next_permutation(it1 it2)下一轮排列数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>
#include<functional>

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
	*/
	// 1. 自定义 比较函数cmp
    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);
	}
	// 2. 用greater(降序)/less(升序、默认)  #include<functional>
	sort(a,a+10,greater<int>());		// int a[10];
	

	
	/*
	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 = 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 cctype

类型函数说明举例
判断 数字bool isdigit(char)
判断 字母bool isalpha(char)大写、小写
判断 字母数字bool isalnum(char)大写、小写、数字
判断 大写bool islower(char)
判断 小写bool isupper(char)
转换为小写char tolower(char)
转换为大写char toupper(char)

5 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;
}

  • 15
    点赞
  • 144
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值