[特殊字符]️ STL 容器快速参考手册
🗃️ STL 容器快速参考手册
📌 一、序列式容器 (Sequence Containers)
1. vector
(动态数组)
特点:在尾部插入/删除高效,随机访问快
cpp
#include <vector> std::vector<int> vec;// 增 vec.push_back(10); // 尾插 vec.emplace_back(20); // 尾插(高效) vec.insert(vec.begin() + 1, 30); // 指定位置插入// 删 vec.pop_back(); // 尾删 vec.erase(vec.begin()); // 删除指定位置 vec.clear(); // 清空// 查/访问 int val = vec[0]; // 无检查访问 int val2 = vec.at(1); // 有检查访问(越界抛异常) int first = vec.front(); // 第一个元素 int last = vec.back(); // 最后一个元素// 大小 bool empty = vec.empty();// 是否为空 size_t size = vec.size();// 元素个数 vec.resize(10); // 调整大小
2. deque
(双端队列)
特点:头尾插入/删除都高效
cpp
#include <deque> std::deque<int> dq;// 增 dq.push_back(10); // 尾插 dq.push_front(20); // 头插 dq.emplace_back(30); dq.emplace_front(40);// 删 dq.pop_back(); // 尾删 dq.pop_front(); // 头删// 访问 (同vector) int val = dq[0];
3. list
(双向链表)
特点:任意位置插入/删除快,不支持随机访问
cpp
#include <list> std::list<int> lst;// 增 lst.push_back(10); lst.push_front(20); lst.insert(++lst.begin(), 30); // 在第二个位置插入// 删 lst.pop_back(); lst.pop_front(); lst.erase(lst.begin()); // 删除第一个元素// 特殊操作 lst.sort(); // 排序 lst.unique(); // 去重 lst.reverse(); // 反转
4. forward_list
(单向链表)
特点:更节省内存,只能向前遍历
cpp
#include <forward_list> std::forward_list<int> flst;// 增 (在指定位置后插入) flst.insert_after(flst.before_begin(), 10); flst.push_front(20);// 删 flst.erase_after(flst.before_begin()); // 删除第一个元素
5. array
(固定数组)
特点:栈上分配,大小固定
cpp
#include <array> std::array<int, 5> arr = {1, 2, 3, 4, 5}; // 必须指定大小// 访问 arr[0] = 10; int size = arr.size(); // 固定大小
📌 二、关联式容器 (Associative Containers)
1. set
(集合)
特点:元素唯一,自动排序
cpp
#include <set> std::set<int> s;// 增 s.insert(30); s.insert({10, 20, 30}); // 插入多个// 删 s.erase(20); // 删除值为20的元素 s.erase(s.begin()); // 删除第一个元素// 查 auto it = s.find(20); // 查找,返回迭代器 if (it != s.end()) {// 找到元素 } bool exists = s.count(20) > 0; // 是否存在
2. multiset
(多重集合)
特点:允许重复元素的set
cpp
#include <set> std::multiset<int> ms; ms.insert(10); ms.insert(10); // 允许重复
3. map
(映射)
特点:键值对,键唯一,自动按键排序
cpp
#include <map> std::map<std::string, int> m;// 增/改 m["apple"] = 5; // 不存在则创建,存在则修改 m.insert({"banana", 3});// 插入 m.emplace("orange", 8); // 原地构造// 删 m.erase("apple"); // 删除键为"apple"的元素// 查 auto it = m.find("banana"); if (it != m.end()) {std::string key = it->first;int value = it->second; }// 遍历 for (const auto& pair : m) {std::cout << pair.first << ": " << pair.second << std::endl; }
4. multimap
(多重映射)
特点:允许重复键的map
cpp
#include <map> std::multimap<std::string, int> mm; mm.insert({"fruit", 5}); mm.insert({"fruit", 8}); // 允许重复键
📌 三、无序容器 (Unordered Containers)
1. unordered_set
(哈希集合)
特点:元素唯一,基于哈希表,查找O(1)
cpp
#include <unordered_set> std::unordered_set<int> us; us.insert(10); us.find(10); // 快速查找
2. unordered_map
(哈希映射)
特点:键值对,基于哈希表,查找O(1)
cpp
#include <unordered_map> std::unordered_map<std::string, int> um; um["apple"] = 5; auto it = um.find("apple");
3. unordered_multiset
/ unordered_multimap
特点:允许重复的无序容器
📌 四、容器适配器 (Container Adapters)
1. stack
(栈)
特点:LIFO (后进先出)
cpp
#include <stack> std::stack<int> st;st.push(10); // 入栈 st.push(20); int top = st.top(); // 查看栈顶 st.pop(); // 出栈(不返回元素) bool empty = st.empty();// 是否为空
2. queue
(队列)
特点:FIFO (先进先出)
cpp
#include <queue> std::queue<int> q;q.push(10); // 入队 q.push(20); int front = q.front(); // 队首元素 int back = q.back(); // 队尾元素 q.pop(); // 出队
3. priority_queue
(优先队列)
特点:元素按优先级出队
cpp
#include <queue> std::priority_queue<int> pq; // 默认大顶堆pq.push(30); // 插入 pq.push(10); pq.push(20); int top = pq.top(); // 获取最大元素 pq.pop(); // 删除最大元素// 小顶堆 std::priority_queue<int, std::vector<int>, std::greater<int>> min_pq;
📊 容器选择指南
需求 | 推荐容器 |
---|---|
随机访问频繁 | vector , array |
头部插入删除多 | deque , list |
中间插入删除多 | list , forward_list |
需要排序 | set , map |
快速查找 | unordered_set , unordered_map |
去重 | set , unordered_set |
LIFO 行为 | stack |
FIFO 行为 | queue |
优先级处理 | priority_queue |
🔄 通用操作 (所有容器都支持)
cpp
// 构造和赋值 Container<T> c1; // 默认构造 Container<T> c2(c1); // 拷贝构造 Container<T> c3 = c1; // 拷贝赋值 Container<T> c4(std::move(c1));// 移动构造// 大小和容量 c.size(); // 元素数量 c.empty(); // 是否为空 c.clear(); // 清空所有元素// 迭代器 auto it_begin = c.begin(); // 开始迭代器 auto it_end = c.end(); // 结束迭代器// C++11 遍历 for (const auto& element : c) {// 处理每个元素 }
这个参考手册覆盖了STL中所有主要容器的基本用法,应该能满足日常开发的大部分需求