1. 文件操作
#include <iostream>
#include <cstdio>  // 头文件
using namespace std;
int main() {
    freopen("输入文件", "r", stdin);  // 从文件里读取输入
    freopen("输出文件", "w", stdout);  // 向文件里写入输出

    fclose(stdin);  // 停止读取输入
    fclose(stdout);  // 停止写入输出
    return 0;
}
  1. 格式化输入输出(可提升输入速度)
#include <iostream>
#include <cstdio>  // 头文件
using namespace std;
int main() {
    int n;
    scanf("%d", &n);  // 输入
    printf("%d", n);  // 输出
    return 0;
}
数据类型scanfprintf
int%d%d
char%c%c
float / double%f%f
long%ld%ld
long long%lld%lld
char[]%s%s
  1. 保留小数(不四舍五入)
#include <iostream>
#include <iomanip>  // 头文件
using namespace std;
int main() {
    double pi = 3.141592653589793;
    cout << fixed << setprecision(2) << pi; // 输出pi到小数点后第2位
    return 0;
}
  1. 保留小数(四舍五入)
#include <iostream>
#include <cstdio>  // 头文件
using namespace std;
int main() {
    double pi = 3.141592653589793;
     printf("%.2f", pi);  // 输出pi到小数点后第2位
    return 0;
}
  1. 随机数
#include <iostream>
#include <cstdlib>  // 头文件
#include <ctime>  // 头文件
using namespace std;
int main() {
    srand(time(0));  // 更新随机数(在程序开头)
    cout << rand() % (100 + 1) + 50;  // 输出一个50~100的随机数(运算顺序不能改变)
    return 0;
}
  1. 数学函数
#include <iostream>
#include <cmath>  // 头文件
using namespace std;
int main() {
    cout << abs(-5);  //求-5的绝对值
    return 0;
}
函数函数用法
取n绝对值(整数)abs(n)
取n的绝对值(小数)fabs(n)
n的m次方pow(n, m)
开n的平方根sqrt(n)
以2为底n的对数log2(n)
以10为底n的对数log10(n)
勾股定理hypot(x, y)
三角函数sinsin(n)
四舍五入(保留整数)round(n)
向上取整ceil(n)
向下取整floor(n)
  1. 反转字符串
#include <iostream>
#include <algorithm>  // 头文件
using namespace std;
int main() {
    string str = "abcdefg";
    reverse(str.begin(), str.end());  // 反转字符串
    cout << str;
    return 0;
}
  1. 排序
#include <iostream>
#include <algorithm>  // 头文件
using namespace std;
bool cmp(int x, int y) {
    return x < y; // 从小到大排序
}
int main() {
    int a[10] = {0, 10, 6, 9, 7, 3, 2, 1, 5, 4};
    sort(a, a + 9, cmp); // 排序
    return 0;
}
  1. 返回x的二进制的个数
#include <iostream>
using namespace std;
int f(int x) {
    int ret = 0;
    for (; x; x &= x - 1) ret++;
    return ret;
}
int main() {
    int x = 2;
    cout << f(x);
    return 0;
}