C++的万能头文件是:
#include <bits/stdc++.h>
一、常用 STL 容器
1.vector(动态数组)
#include<iostream>
#include<string>
#include <vector>
#include <algorithm> // 包含排序所需的头文件
using namespace std;int main() {vector<int> v; // 创建一个空的 vectorv.push_back(1); // 尾部插入元素 1v.push_back(3); // 尾部插入元素 3v.push_back(2); // 尾部插入元素 2v.pop_back(); // 尾部删除一个元素(删除 2)cout << "Size of vector: " << v.size() << endl; // 输出当前 vector 的大小v[0] = 5; // 修改 vector 第一个元素为 5sort(v.begin(), v.end()); // 排序 vector 中的元素cout << "Sorted vector: ";for (int num : v) {cout << num << " "; // 输出排序后的 vector 元素}cout << endl;return 0;
}
2.string(字符串)
#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(栈)
#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(); // 弹出栈顶元素 30cout << "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(队列)
#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(); // 弹出队首元素 10cout << "After pop, front element: " << q.front() << endl; // 输出新的队首元素 20// 再次查看队首元素q.pop(); // 弹出队首元素 20cout << "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(优先队列)
#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; // 输出 3max_pq.pop(); // 移除最大元素 3cout << "移除最大元素后,堆顶元素: " << 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; // 输出 1min_pq.pop(); // 移除最小元素 1cout << "移除最小元素后,堆顶元素: " << min_pq.top() << endl; // 输出 2return 0;
}
6.set(有序集合)
#include <iostream>
#include <set>
using namespace std;int main() {set<int> s;// 插入元素s.insert(5); // 插入元素 5s.insert(2); // 插入元素 2s.insert(8); // 插入元素 8s.insert(3); // 插入元素 3s.insert(1); // 插入元素 1// 打印集合中的元素cout << "集合中的元素:" << endl;for (auto it = s.begin(); it != s.end(); ++it) {cout << *it << " ";}cout << endl;// 删除元素 5s.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(有序键值对)
#include <iostream>
#include <map>
using namespace std;int main() {map<string, int> mp;// 插入键值对mp["apple"] = 5; // 插入或修改键值对 "apple" -> 5mp["banana"] = 3; // 插入 "banana" -> 3mp["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(组合两个值)
#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 abcreturn 0;
}
二、蓝桥杯常用代码模板
1.快速排序(直接用 STL)
#include <iostream>
#include <algorithm> // 包含sort函数
#include <vector> // 包含vector定义
#include <functional> // 包含greaterusing namespace std;int main() {// 创建一个包含整数的vectorvector<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.二分查找
#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
#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 开始 DFSdfs(0);return 0;
}
4.BFS 模板
#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 开始 BFSbfs(0);return 0;
}
5.并查集
#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.前缀和
#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;
}
三、技巧总结
输入输出加速(写在最开头)
ios::sync_with_stdio(false);
cin.tie(nullptr);
万能头文件(蓝桥杯可用)
#include <bits/stdc++.h>
using namespace std;
结构体排序
struct Node {int a, b;bool operator<(const Node& other) const {return a < other.a; // 按a升序}
};
vector<Node> nodes;
sort(nodes.begin(), nodes.end());
建议练习方向:多刷贪心、模拟、动态规划类题目,熟练掌握这些容器的基本操作即可应对大部分蓝桥杯题目。