pb_ds
pb_ds 介绍
pb_ds 概览:STL 的强力扩展
pb_ds(Policy-Based Data Structures)是 GNU C++ 标准库的扩展组件,诞生于对STL容器的性能与灵活性不足的解决方案。
它采用策略驱动设计理念,通过组合算法实现、内存管理策略和节点更新机制,提供比STL更高效、更可定制化的数据结构。
核心价值:
- 性能碾压:gp_hash_table 比 unordered_map 快 倍,pairing_heap 支持 时间合并。
- 功能扩展:有序树支持名次查询(order_of_key),堆支持原位修改(modify)。
- 策略自由:可定制哈希函数、平衡树类型、堆实现算法等
- 兼容 STL:提供类似 find()/insert() 的接口,学习曲线平缓
ℹ️ 与STL的关系:非替代品而是增强包。STL满足通用性,pb_ds专注高性能场景
使用前提与环境配置 pb_ds仅支持GCC(推荐GCC 9.1+,标准库 GNU libstdc++(Clang需链接libstdc++)。MSVC完全不支持,这是GCC的独占扩展
// 头文件
#include <ext/pb_ds/assoc_container.hpp> // 关联容器+哈希表
#include <ext/pb_ds/tree_policy.hpp> // 树结构策略
#include <ext/pb_ds/priority_queue.hpp> // 优先队列
#include <ext/pb_ds/trie_policy.hpp> // 字典树(可选)
using namespace __gnu_pbds; // 关键命名空间编译选项: g++ -std = c++17 your_file.cpp -o output 需 C++11 及以上标准
⚠️ 避坑提示:避免与STL同名容器混用(如同时使用std::priority_queue 和 pb_ds::priority_queue),建议不使用 using namespace,保留命名空间。
为什么需要pb_ds?
当遇到以下场景时,STL 会暴露短板:
需要统计排名(如查询数据集第 大元素)
高频堆合并操作(如 优化)
超大规模哈希查询(百万级插入/秒)
自定义数据结构行为(如记录子树大小)
典型用例:
// 名次查询:STL需手写平衡树,pb_ds一行搞定
tree <int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> rank_tree;
cout << rank_tree.order_of_key(42); // 输出小于42的元素数量
// 堆合并:STL 的 priority_queue 无法高效合并
priority_queue<int, pairing_heap_tag> heap1, heap2;
heap1.join(heap2); // O(1)时间合并两个堆关联容器(Associative Containers) 提供类似 STL 的 map/set 功能,但支持更丰富的底层实现策略。
哈希表:高性能键值存储
| 类型 | 冲突解决策略 | 性能特点 |
|---|---|---|
| gp_hash_table | 线性探测法 | 默认推荐,查询O(1),内存连续。 |
| cc_hash_table | 链地址法 | 高冲突场景稳定,但缓存不友好。 |
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
// 探测式哈希表(默认)
gp_hash_table<string, int> gp_map;
gp_map["algorithm"] = 97; // 插入
auto it = gp_map.find("algorithm"); // O(1)查询
// 链式哈希表
cc_hash_table<int, string> cc_map;
cc_map[42] = "answer";
cout << cc_map[42]; // 输出"answer"首选gp_hash_table,仅当键冲突极严重时考虑cc_hash_table有序容器:支持排名统计
基于平衡二叉搜索树实现,支持顺序遍历和名次查询。
| 类型 | 底层结构 | 特性 |
|---|---|---|
| tree (默认) | 红黑树 | 稳定O(log n),支持排名查询 |
| splay_tree | 伸展树 | 局部性优化,均摊复杂度 |
| ov_tree | 有序向量 | 小数据集高效,插入O(n) |
#include <ext/pb_ds/tree_policy.hpp>
// 定义支持名次查询的红黑树
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> indexed_set;
indexed_set rb_tree;
rb_tree.insert({10, 20, 30, 40});
// 查询名次(小于15的元素数量)
cout << rb_tree.order_of_key(15); // 输出1
// 查询第k大元素(0-based)
auto it = rb_tree.find_by_order(2);
cout << *it; // 输出30红黑树通用场景最优;伸展树会把最近访问节点移至根部,适合局部性强的场景;有序向量树仅用于 的数据。
优先队列(Priority Queues)
比 STL 的 priority_queue 更灵活,支持堆合并和原位修改。
| 堆类型(标签) | 算法 | 关键优势 |
|---|---|---|
| pairing_heap_tag | 配对堆 | 默认推荐,合并O(1) |
| binomial_heap_tag | 二项堆 | 理论最优合并 |
| binary_heap_tag | 二叉堆 | 内存占用最小 |
| thin_heap_tag | 瘦堆 | Dijkstra优化 |
#include <ext/pb_ds/priority_queue.hpp>
// 定义支持修改的配对堆
typedef __gnu_pbds::priority_queue<
int, less<int>, pairing_heap_tag> heap;
heap pq;
auto it = pq.push(10); // 插入元素,获取迭代器
pq.push(20);
pq.push(5);
// 修改元素值(STL无法实现)
pq.modify(it, 25); // 将10改为25
// 合并两个堆(O(1)操作)
heap pq2;
pq2.push(30);
pq.join(pq2); // pq现在包含{5,20,25,30}| 堆类型 | modify() | join() |
|---|---|---|
| 配对堆 | O(log n) | O(1) |
| 二项堆 | O(log n) | O(log n) |
| 二叉堆 | 不支持 | O(n) |
💡 最佳实践:
需要合并操作 → 必选 pairing_heap_tag
仅需基本操作 → binary_heap_tag 更省内存
图算法优化 → thin_heap_tag 减少比较次数
字典树(Trie)pb_ds 提供两种字典树实现,特别适合前缀查询和字符串处理场景。
类型 结构 特点
trie 标准字典树 结构简单,内存占用低
pat_trie Patricia Trie 默认推荐,压缩前缀优化 #include <ext/pb_ds/trie_policy.hpp> using namespace __gnu_pbds;
// 定义Patricia Trie typedef trie<string, null_type, trie_string_access_traits<>, pat_trie_tag> string_trie;
string_trie t; t.insert(“hello”); t.insert(“world”); t.insert(“hell”);
// 前缀查询:返回匹配前缀的范围迭代器 auto range = t.prefix_range(“he”); for (auto it = range.first; it != range.second; ++it) cout << *it << ” ”; // 输出: hell hello
// 最长公共前缀查询 cout << t.longest_prefix(“hellraiser”); // 输出: hell 特殊容器 list_update list_update是一个支持快速查找的链表变种,结合了链表和哈希表的特性。
核心特性:
支持O(1)时间插入删除 支持O(1)时间查找(通过迭代器缓存) 保持元素插入顺序 典型应用场景:
LRU缓存实现 需要频繁查找/删除的序列 #include <ext/pb_ds/list_update_policy.hpp>
typedef list_update<int, null_type> fast_list; fast_list lst;
// 插入元素并获取迭代器
auto it10 = lst.insert(10);
auto it20 = lst.insert(20);
// 通过迭代器快速查找 auto found = lst.find(20); // O(1)操作
// 删除元素 lst.erase(it10);
// 遍历(保持插入顺序) for (auto& x : lst) cout << x << ” ”; // 输出: 20 与STL list对比:
操作 STL list list_update insert() O(1) O(1) erase() O(1) O(1) find() O(n) O(1) 内存开销 低 中 ⚠️ 注意:迭代器稳定性优于std::vector但弱于std::list
pb_ds的策略定制 pb_ds最强大的特性是可定制策略,通过模板参数深度控制数据结构行为。
- 自定义哈希函数 struct custom_hash { size_t operator()(const string& s) const { size_t h = 0; for (char c : s) h = h * 131 + c; // 自定义哈希算法 return h; } };
// 应用自定义哈希 gp_hash_table<string, int, custom_hash> custom_map; 2. 平衡树统计扩展 // 定义节点更新策略:记录子树大小 template<typename Node_CItr> struct node_statistics { size_t subtree_size;
void operator()(Node_CItr it, Node_CItr end_it) {
subtree_size = 1;
auto l_child = it.get_l_child();
auto r_child = it.get_r_child();
if (l_child != end_it) subtree_size += l_child.get_metadata();
if (r_child != end_it) subtree_size += r_child.get_metadata();
}
};
// 应用自定义策略 typedef tree<int, null_type, less<int>, rb_tree_tag, node_statistics> size_aware_tree; 3. 堆节点修改回调 struct heap_custom_invoke { void operator()(pairing_heap_tag::point_iterator it) { cout << “修改节点: ” << *it << endl; } };
typedef priority_queue<int, less<int>, pairing_heap_tag, heap_custom_invoke> callback_heap;
callback_heap h; auto it = h.push(10); h.modify(it, 20); // 触发回调输出”修改节点: 10”