文章目录
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可省略,默认升序 | |
排列 | 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;
}