C++ 标准库与文件操作

STL 容器 (vector, sort) 与 freopen 文件读写

标准模板库 (STL) - Vector & Sort

vector 是可变大小数组,sort 是高效排序函数。

Size: 0, Capacity: ?
#include 
#include 
using namespace std;

vector v;
v.push_back(10); // 插入尾部
sort(v.begin(), v.end()); // 排序

文件重定向 (freopen)

竞赛中常用 freopen 将标准输入输出重定向到文件。

Input File (data.in)

Code

freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
int a, b;
cin >> a >> b;
cout << a + b;

Output File (data.out)

(Empty)

常用数学函数 (cmath / algorithm)

函数 说明 示例
min(a, b) 取最小值 min(10, 5) -> 5
max(a, b) 取最大值 max(10, 5) -> 10
swap(a, b) 交换变量 swap(x, y)
sqrt(x) 平方根 sqrt(16) -> 4.0
pow(base, exp) 幂运算 pow(2, 3) -> 8.0