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());
建議練習方向:多刷貪心、模擬、動態規劃類題目,熟練掌握這些容器的基本操作即可應對大部分藍橋杯題目。