C++的万能头文件是:
cpp
#include <bits/stdc++.h>
一、常用 STL 容器
1.vector(动态数组)
cpp
#include<iostream>
#include<string>
#include <vector>
#include <algorithm> // 包含排序所需的头文件
using namespace std;
int main() {
vector<int> v; // 创建一个空的 vector
v.push_back(1); // 尾部插入元素 1
v.push_back(3); // 尾部插入元素 3
v.push_back(2); // 尾部插入元素 2
v.pop_back(); // 尾部删除一个元素(删除 2)
cout << "Size of vector: " << v.size() << endl; // 输出当前 vector 的大小
v[0] = 5; // 修改 vector 第一个元素为 5
sort(v.begin(), v.end()); // 排序 vector 中的元素
cout << "Sorted vector: ";
for (int num : v) {
cout << num << " "; // 输出排序后的 vector 元素
}
cout << endl;
return 0;
}
2.string(字符串)
cpp
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
string s = "hello";
s += " world!"; // 拼接字符串,s 变成 "hello world!"
// 截取子串
string sub = s.substr(0, 5); // 截取 "hello"
cout << "Substr (0, 5): " << sub << endl;
// 查找子串位置
size_t pos = s.find("wo"); // 查找 "wo" 在 s 中的位置
if (pos != string::npos) {
cout << "Found 'wo' at position: " << pos << endl; // 输出找到的位置
} else {
cout << "'wo' not found!" << endl;
}
// 获取字符串的长度
cout << "Length of string: " << s.length() << endl; // 输出字符串的长度
return 0;
}
3.stack(栈)
cpp
#include<iostream>
#include<stack>
using namespace std;
int main() {
// 创建一个整数栈
stack<int> st;
// 入栈操作
st.push(10); // 将 10 入栈
st.push(20); // 将 20 入栈
st.push(30); // 将 30 入栈
// 查看栈顶元素
cout << "Top element: " << st.top() << endl; // 输出栈顶元素 30
// 出栈操作
st.pop(); // 弹出栈顶元素 30
cout << "After pop, top element: " << st.top() << endl; // 输出新的栈顶元素 20
// 再次查看栈顶元素
cout << "Top element after another pop: " << st.top() << endl; // 输出新的栈顶元素 10
// 你也可以查看栈是否为空
if (st.empty()) {
cout << "The stack is empty." << endl;
} else {
cout << "The stack is not empty." << endl;
}
return 0;
}
4.queue(队列)
cpp
#include <iostream>
#include <queue>
using namespace std;
int main() {
// 创建一个整数队列
queue<int> q;
// 入队操作
q.push(10); // 将 10 入队
q.push(20); // 将 20 入队
q.push(30); // 将 30 入队
// 查看队首元素
cout << "Front element: " << q.front() << endl; // 输出队首元素 10
// 出队操作
q.pop(); // 弹出队首元素 10
cout << "After pop, front element: " << q.front() << endl; // 输出新的队首元素 20
// 再次查看队首元素
q.pop(); // 弹出队首元素 20
cout << "After another pop, front element: " << q.front() << endl; // 输出新的队首元素 30
// 检查队列是否为空
if (q.empty()) {
cout << "The queue is empty." << endl;
} else {
cout << "The queue is not empty." << endl;
}
return 0;
}
5.priority_queue(优先队列)
cpp
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
// 默认是大根堆
priority_queue<int> max_pq;
// 插入元素到大根堆
max_pq.push(3);
max_pq.push(1);
max_pq.push(2);
cout << "大根堆操作:" << endl;
cout << "堆顶元素 (最大元素): " << max_pq.top() << endl; // 输出 3
max_pq.pop(); // 移除最大元素 3
cout << "移除最大元素后,堆顶元素: " << max_pq.top() << endl; // 输出 2
// 创建小根堆
priority_queue<int, vector<int>, greater<int>> min_pq;
// 插入元素到小根堆
min_pq.push(3);
min_pq.push(1);
min_pq.push(2);
cout << "\n小根堆操作:" << endl;
cout << "堆顶元素 (最小元素): " << min_pq.top() << endl; // 输出 1
min_pq.pop(); // 移除最小元素 1
cout << "移除最小元素后,堆顶元素: " << min_pq.top() << endl; // 输出 2
return 0;
}
6.set(有序集合)
cpp
#include <iostream>
#include <set>
using namespace std;
int main() {
set<int> s;
// 插入元素
s.insert(5); // 插入元素 5
s.insert(2); // 插入元素 2
s.insert(8); // 插入元素 8
s.insert(3); // 插入元素 3
s.insert(1); // 插入元素 1
// 打印集合中的元素
cout << "集合中的元素:" << endl;
for (auto it = s.begin(); it != s.end(); ++it) {
cout << *it << " ";
}
cout << endl;
// 删除元素 5
s.erase(5);
cout << "删除 5 后的集合:" << endl;
for (auto it = s.begin(); it != s.end(); ++it) {
cout << *it << " ";
}
cout << endl;
// 检查元素 5 是否存在
if (s.count(5) > 0) {
cout << "元素 5 存在" << endl;
} else {
cout << "元素 5 不存在" << endl; // 这行将被打印
}
// 查找第一个大于等于 3 的元素
auto it = s.lower_bound(3);
if (it != s.end()) {
cout << "第一个大于等于 3 的元素是: " << *it << endl; // 输出 3
} else {
cout << "没有大于等于 3 的元素" << endl;
}
return 0;
}
7.map(有序键值对)
cpp
#include <iostream>
#include <map>
using namespace std;
int main() {
map<string, int> mp;
// 插入键值对
mp["apple"] = 5; // 插入或修改键值对 "apple" -> 5
mp["banana"] = 3; // 插入 "banana" -> 3
mp["orange"] = 7; // 插入 "orange" -> 7
// 判断键 "apple" 是否存在
if (mp.count("apple") > 0) {
cout << "键 'apple' 存在,值为 " << mp["apple"] << endl;
} else {
cout << "键 'apple' 不存在" << endl;
}
// 遍历 map 中的所有键值对
cout << "map 中的键值对为:" << endl;
for (auto& p : mp) {
cout << p.first << " -> " << p.second << endl;
}
return 0;
}
8.pair(组合两个值)
cpp
#include <iostream>
#include <utility> // 包含pair定义
#include <string> // 包含string定义
using namespace std;
int main() {
// 创建一个pair对象,包含int和string类型
pair<int, string> p = {1, "abc"};
// 输出pair的第一个值和第二个值
cout << p.first << " " << p.second << endl; // 输出: 1 abc
return 0;
}
二、蓝桥杯常用代码模板
1.快速排序(直接用 STL)
cpp
#include <iostream>
#include <algorithm> // 包含sort函数
#include <vector> // 包含vector定义
#include <functional> // 包含greater
using namespace std;
int main() {
// 创建一个包含整数的vector
vector<int> v = {3, 1, 4, 2};
// 默认升序排序
sort(v.begin(), v.end());
cout << "升序排序: ";
for (int num : v) {
cout << num << " "; // 输出: 1 2 3 4
}
cout << endl;
// 降序排序
sort(v.begin(), v.end(), greater<int>());
cout << "降序排序: ";
for (int num : v) {
cout << num << " "; // 输出: 4 3 2 1
}
cout << endl;
return 0;
}
2.二分查找
cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
// 假设有一个已排序的数组
vector<int> v = {1, 2, 4, 6, 8, 10};
int target = 5;
// 使用 lower_bound 找到第一个 >= target 的位置
int pos = lower_bound(v.begin(), v.end(), target) - v.begin();
cout << "第一个 >= " << target << " 的位置是: " << pos << endl;
return 0;
}
3.DFS
cpp
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> graph; // 图的邻接表表示
vector<bool> visited; // 访问标记
void dfs(int cur) {
visited[cur] = true; // 标记当前节点为已访问
cout << "访问节点: " << cur << endl;
for (auto next : graph[cur]) { // 遍历所有相邻节点
if (!visited[next]) {
dfs(next); // 递归访问未访问的邻居
}
}
}
int main() {
int n = 5; // 节点数
graph.resize(n);
visited.resize(n, false);
// 示例图: 添加边
graph[0].push_back(1);
graph[0].push_back(2);
graph[1].push_back(3);
graph[2].push_back(4);
// 从节点 0 开始 DFS
dfs(0);
return 0;
}
4.BFS 模板
cpp
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector<vector<int>> graph; // 图的邻接表表示
vector<bool> visited; // 访问标记
void bfs(int start) {
queue<int> q;
q.push(start);
visited[start] = true; // 标记起始节点为已访问
while (!q.empty()) {
int cur = q.front();
q.pop();
cout << "访问节点: " << cur << endl;
// 遍历所有相邻节点
for (auto next : graph[cur]) {
if (!visited[next]) {
visited[next] = true; // 标记为已访问
q.push(next); // 加入队列
}
}
}
}
int main() {
int n = 5; // 节点数
graph.resize(n);
visited.resize(n, false);
// 示例图: 添加边
graph[0].push_back(1);
graph[0].push_back(2);
graph[1].push_back(3);
graph[2].push_back(4);
// 从节点 0 开始 BFS
bfs(0);
return 0;
}
5.并查集
cpp
#include <iostream>
using namespace std;
int parent[1000]; // 并查集父节点数组
int find(int x) {
if (parent[x] != x) {
parent[x] = find(parent[x]); // 路径压缩
}
return parent[x];
}
void unite(int x, int y) {
int rootX = find(x);
int rootY = find(y);
if (rootX != rootY) {
parent[rootX] = rootY; // 合并集合
}
}
int main() {
int n = 5; // 元素个数
// 初始化并查集,每个元素的父节点是自己
for (int i = 0; i < n; ++i) {
parent[i] = i;
}
// 合并元素
unite(0, 1);
unite(1, 2);
unite(3, 4);
// 检查是否在同一个集合
if (find(0) == find(2)) {
cout << "0 和 2 在同一个集合" << endl;
} else {
cout << "0 和 2 不在同一个集合" << endl;
}
if (find(0) == find(4)) {
cout << "0 和 4 在同一个集合" << endl;
} else {
cout << "0 和 4 不在同一个集合" << endl;
}
return 0;
}
6.前缀和
cpp
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n = 5;
vector<int> a = {1, 2, 3, 4, 5}; // 原数组
vector<int> s(n + 1, 0); // 前缀和数组
// 计算前缀和
for (int i = 1; i <= n; i++) {
s[i] = s[i - 1] + a[i - 1];
}
// 查询区间 [l, r] 的和
int l = 1, r = 3; // 询问区间 [1, 3]
int sum = s[r] - s[l - 1];
cout << "区间 [" << l << ", " << r << "] 的和为: " << sum << endl;
return 0;
}
三、技巧总结
输入输出加速(写在最开头)
cpp
ios::sync_with_stdio(false);
cin.tie(nullptr);
万能头文件(蓝桥杯可用)
cpp
#include <bits/stdc++.h>
using namespace std;
结构体排序
cpp
struct Node {
int a, b;
bool operator<(const Node& other) const {
return a < other.a; // 按a升序
}
};
vector<Node> nodes;
sort(nodes.begin(), nodes.end());
建议练习方向:多刷贪心、模拟、动态规划类题目,熟练掌握这些容器的基本操作即可应对大部分蓝桥杯题目。