C++ STL 容器簡介(藍橋杯適用精簡版)

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());

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/80280.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/80280.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/80280.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Java語言的進化:JDK的未來版本

作為一名Java開發者&#xff0c;我們正處在一個令人興奮的時代&#xff01;Java語言正在以前所未有的速度進化&#xff0c;每個新版本都帶來令人驚喜的特性。讓我們一起探索JDK未來版本的發展方向&#xff0c;看看Java將如何繼續領跑編程語言界&#xff01;&#x1f4aa; &…

不要使用Round函數保留小數位了

不要使用Round函數保留小數位了 如果你表格不需要保留公式&#xff0c;那么就不要使用Round函數保留小數位了。用Excel工作圈插件&#xff0c;可以輕松以數值形式保留小數位&#xff0c;且支持合并單元格、不連貫區域快速處理。 如下圖&#xff0c;有文本&#xff0c;有跨行合并…

【C++】入門基礎【下】

目錄 一、缺省參數二、函數重載1. 函數類型不同2. 參數個數不同3、函數類型順序不同 三、引用1、引用的概念和定義2、引用的功能2.1 功能1&#xff1a; 做函數形參&#xff0c;修改形參影響實參2.2 功能2&#xff1a; 做函數形參&#xff0c;減少拷貝&#xff0c;提高效率2.3 功…

git比較不同分支的不同提交文件差異

背景&#xff1a;只想比較某2個分支的某2次提交的差異&#xff0c;不需要帶上父提交。 以commitA為基準&#xff0c;用commitB去比較差異 直接上代碼&#xff1a; commitAxxxx1 commitBxxxx2 outputFile"output.txt"# 獲取與第一個父提交的文件列表 filesA$(git di…

Linux內核之struct pt_regs結構

前沿 項目開發最近進行系統hook功能實現相關業務&#xff0c;主要在centos7和8系列環境開發下關功能。調研了相關知識點&#xff0c;發現在系統7和8上內核版本差別比較大&#xff0c;7-3.10.x系列版本&#xff0c;8-4.18.x系列版本。依據兩個系統的內核情況根對應的內核符號表進…

《從混亂到有序:ArkUI項目文件結構改造指南》

在ArkUI開發的廣袤天地里&#xff0c;構建一個清晰、有序的文件結構&#xff0c;是打造優質應用的關鍵。一個合理的文件結構&#xff0c;就像為開發者精心繪制的地圖&#xff0c;在項目的各個階段&#xff0c;都能提供明確的指引&#xff0c;讓開發過程順暢無阻。今天&#xff…

C#基于Sunnyui框架和MVC模式實現用戶登錄管理

C#基于Sunnyui框架和MVC模式實現用戶登錄管理 1 Controller1.1 UserManagementController.cs&#xff08;控制器入口&#xff09; 2 Model2.1 UserRepository.cs&#xff08;用戶管理模型&#xff09;2.2 User.cs&#xff08;用戶結構體&#xff09;2.3 SQLiteHelper.cs&#x…

自然語言處理(NLP)技術的實例

自然語言處理&#xff08;NLP&#xff09;技術在各個領域都有廣泛的應用&#xff0c;以下是幾個例子&#xff1a; 語音識別&#xff1a;通過NLP技術&#xff0c;計算機可以識別和理解語音指令&#xff0c;例如智能助手如Siri和Alexa就是通過語音識別技術實現與用戶的交互。 機…

Spring Boot實戰(三十六)編寫單元測試

目錄 一、什么是單元測試&#xff1f;二、Spring Boot 中的單元測試依賴三、舉例 Spring Boot 中不同層次的單元測試3.1 Service層3.2 Controller 層3.3 Repository層 四、Spring Boot 中 Mock、Spy 對象的使用4.1 使用Mock對象的背景4.2 什么是Mock對象&#xff0c;有哪些好處…

aws服務(四)文件存儲服務S3 介紹使用代碼集成

一、介紹 1、簡介 Amazon S3 是 Amazon Web Services 提供的一種對象存儲服務(Object Storage),用于在云中存儲和檢索任意數量的數據。它以高可用性、高擴展性和高持久性著稱,非常適合用來存儲網站資源、數據備份、日志文件、大數據、機器學習輸入輸出等。 2、主要特性 …

應用信息1.13.0發布

增加工具箱 增加啟動器功能 增加布局查看器 增加手動安裝和卸載應用 增加APK文件解析 增加應用多選功能 增加查看應用預裝版本 增加應用信息和ADB命令導出 修復其它問題... 百度下載&#xff1a;百度網盤 請輸入提取碼 提取碼&#xff1a;1234

【Vue3 實戰】插槽封裝與懶加載

一、為什么需要插槽&#xff1f;從一個面板組件說起 在電商首頁開發中&#xff0c;經常遇到這樣的場景&#xff1a; 「新鮮好物」「人氣推薦」同樣類型模塊都需要相同的標題欄&#xff0c;但內容區布局不同 這時候&#xff0c;插槽&#xff08;Slot&#xff09;就像一個「內容…

虛無隧穿產生宇宙(true nothing tunneling) 是誰提出的

是 亞歷克斯.維連金 英文名&#xff08;alex vilenkin 或者 Alexander Vilenkin)提出來的。 “虛無隧穿產生宇宙”&#xff08;true nothing tunneling&#xff09;這一概念并非一個標準的物理學術語&#xff0c;它更像是對某些現代宇宙學理論的描述&#xff0c;尤其是涉及宇宙…

postgis:添加索引時提示“對訪問方法 gist 數據類型 geometry 沒有默認的操作符表“

問題 在對gis表的geom字段創建空間索引時&#xff0c;出現“對訪問方法 "gist" 數據類型 geometry 沒有默認的操作符表”的提示報錯。 解決方案 按系列步驟進行排查并解決。 1.先確認已安裝postgis -- 查看postgis版本 SELECT postgis_full_version() 若安裝了則…

圖論---Prim堆優化(稀疏圖)

題目通常會提示數據范圍&#xff1a; 若 V ≤ 500&#xff0c;兩種方法均可&#xff08;樸素Prim更穩&#xff09;。 若 V ≤ 1e5&#xff0c;必須用優先隊列Prim vector 存圖。 #include <iostream> #include <vector> #include <queue> #include <…

代碼隨想錄算法訓練營第一天:數組part1

今日學習的文章鏈接和視頻鏈接 ● 自己看到題目的第一想法 ● 看完代碼隨想錄之后的想法 ● 自己實現過程中遇到哪些困難 ● 今日收獲&#xff0c;記錄一下自己的學習時長 狀態 思路理解完成 30% 代碼debug完成 60% 代碼模板總結并抽象出來 100% 題目 704 二分查找 題目鏈接…

企業為何要求禁用缺省口令?安全風險及應對措施分析

在當今數字化時代&#xff0c;企業網絡安全面臨著前所未有的挑戰。缺省口令的使用是網絡安全中的一個重要隱患&#xff0c;許多企業在制定網絡安全紅線時&#xff0c;明確要求禁用缺省口令。本文將探討這一要求的原因及其對企業安全的重要性。 引言&#xff1a;一個真實的入侵場…

PostgreSQL 中的權限視圖

PostgreSQL 中的權限視圖 PostgreSQL 提供了多個系統視圖來查詢權限信息&#xff0c;雖然不像 Oracle 的 DBA_SYS_PRIVS 那樣集中在一個視圖中&#xff0c;但可以通過組合以下視圖獲取完整的系統權限信息。 一 主要權限相關視圖 Oracle 視圖PostgreSQL 對應視圖描述DBA_SYS_…

【防火墻 pfsense】1簡介

&#xff08;1&#xff09; pfSense 有以下可能的用途&#xff1a; 邊界防火墻 路由器 交換機 無線路由器 / 無線接入點 &#xff08;2&#xff09;邊界防火墻 ->要充當邊界防火墻&#xff0c;pfSense 系統至少需要兩個接口&#xff1a;一個廣域網&#xff08;WAN&#xff0…

數據庫+Docker+SSH三合一!深度評測HexHub的全棧開發體驗

作為一名技術博主&#xff0c;我最近一直被各種開發工具切換搞得焦頭爛額。數據庫要用Navicat&#xff0c;服務器管理得開Termius&#xff0c;Docker操作還得切到命令行&#xff0c;每天光在不同工具間切換就浪費了大量時間。直到團隊里的一位架構師向我推薦了HexHub這個一體化…