STL(Standard Template Library)是 C++ 標準庫的核心體現泛型編程思想的部分。它通過模板實現數據結構與算法的解耦,提供工業級效能的可復用組件。本文系統梳理 STL 六大組件,重點解析容器分類、迭代器體系、算法分類及容器適配器用法。
vector, list, map...)。sort, find, copy...),通過迭代器實現與容器無關。operator() 的類,行為類似函數,可保存狀態,用於算法策略定製。stack/queue、迭代器適配器、仿函數適配器)。allocator),容器底層內存管理核心。| 容器 | 底層結構 | 隨機訪問 | 頭部插刪 | 尾部插刪 | 中間插刪 | 適用場景 |
|---|---|---|---|---|---|---|
vector | 動態數組 | O(1) | O(n) | O(1) 擴容 | O(n) | 默認首選,需頻繁隨機訪問、尾部操作 |
deque | 分段連續數組 | O(1) | O(1) | O(1) | O(n) | 雙端操作頻繁、需隨機訪問 |
list | 雙向鏈表 | O(n) | O(1) | O(1) | O(1) 已知位置 | 頻繁中間插刪、不需隨機訪問 |
| 容器 | 鍵值關系 | 鍵唯一性 | 有序性 | 查找/插刪 |
|---|---|---|---|---|
set | Key = Value | 唯一 | 升序 | O(log n) |
multiset | Key = Value | 可重複 | 升序 | O(log n) |
map | Key → Value | 唯一 | 按 Key 排序 | O(log n) |
multimap | Key → Value | 可重複 | 按 Key 排序 | O(log n) |
無序關聯容器(C++11):unordered_set/map 基於哈希表,平均 O(1) 查找,無序。
迭代器是智能指針,統一了容器遍歷語法。按能力分五類,能力遞增:
Input Iterator → Output Iterator
↓
Forward Iterator (可多遍歷、只讀/寫)
↓
Bidirectional Iterator (雙向 ++)
↓
Random Access Iterator (支援 +n, -n, [n], <, >)
| 容器 | 迭代器類別 |
|---|---|
| vector, deque | Random Access |
| list, set/multiset, map/multimap | Bidirectional |
| stack, queue, priority_queue | 不支援迭代器 |
// 聲明 vector<int>::iterator it; list<string>::const_reverse_iterator rit; // 關鍵操作 *it, ++it, --it, it == it2, it != it2 // 所有 it + n, it - n, it[n], it < it2 // 隨機訪問
<algorithm>, <numeric>, <functional>)| 類別 | 代表算法 | 說明 |
|---|---|---|
| 非可變序列 | find, count, equal, search, mismatch | 不修改元素,僅讀取/比較 |
| 可變序列 | copy, transform, replace, remove, fill, generate | 修改元素值或位置 |
| 排序相關 | sort, stable_sort, partial_sort, nth_element, binary_search, lower_bound, merge, inplace_merge | 排序、查找、合併有序序列 |
| 數值算法 | accumulate, inner_product, partial_sum, adjacent_difference | 數學運算(需 <numeric>) |
| 集合算法 | set_union, set_intersection, set_difference, set_symmetric_difference | 有序範圍上的集合運算 |
| 堆算法 | make_heap, push_heap, pop_heap, sort_heap | 構建/維護堆結構 |
| 排列組合 | next_permutation, prev_permutation | 生成全排列 |
// 查找
auto it = find(v.begin(), v.end(), 42);
auto it = find_if(v.begin(), v.end(), [](int x){ return x > 10; });
// 排序
sort(v.begin(), v.end(), greater<int>()); // 降序
stable_sort(v.begin(), v.end()); // 穩定排序
// 修改
transform(v1.begin(), v1.end(), v2.begin(), back_inserter(v3), plus<int>());
replace_if(v.begin(), v.end(), [](int x){ return x < 0; }, 0);
// 刪除(需配合 erase)
v.erase(remove(v.begin(), v.end(), 3), v.end()); // Erase-Remove 慣用法
// 數值
int sum = accumulate(v.begin(), v.end(), 0);
適配器封裝底層容器(默認 deque/vector),提供特定語義接口,不支援迭代器。
#include <stack> stack<int> s; // 底層 deque stack<int, vector<int>> s2; // 指定 vector s.push(1); s.top(); s.pop(); s.empty(); s.size();
#include <queue> queue<int> q; // 底層 deque queue<int, list<int>> q2; // 指定 list q.push(1); q.front(); q.back(); q.pop(); q.empty(); q.size();
#include <queue> priority_queue<int> pq; // less,大頂堆 priority_queue<int, vector<int>, greater<int>> pq2; // 小頂堆 // 自定義類型需重載 operator< 或提供比較仿函數 struct Node { int val, pri; }; struct Cmp { bool operator()(const Node& a, const Node& b) { return a.pri < b.pri; } }; priority_queue<Node, vector<Node>, Cmp> pq3;
vector;需雙端操作用 deque;需頻繁中間插刪用 list。set/map;允許重複鍵 → multiset/multimap。unordered_set/map(自定義類型需提供 hash 與 ==)。std::sort, std::find 等),僅在極特殊場景手寫。