参考:http://blog.csdn.net/wjf_wzzc/article/details/38851703
《C++的pb_ds库在OI中的应用》大连市第二十四中学 于纪平
http://blog.csdn.net/wmdcstdio/article/details/44596305
一、
pb_ds 库全称 Policy-Based Data Structures
It is designed for high-performance, flexibility, semantic safety, and conformance to the corresponding containers in std and std::tr1.
英文官方文档:https://gcc.gnu.org/onlinedocs/libstdc++/ext/pb_ds/
pb_ds 库封装了很多数据结构,比如哈希(Hash)表,平衡二叉树,字典树(Trie树),堆(优先队列)等
就像 vector、set、map 一样,但比STL 功能更多
pb_ds 只在 Linux 下可以用
二、基本用法
1、优先队列(Priority Queue)
#include<ext/pb_ds/priority_queue.hpp>
using namespace __gnu_pbds;
与 std::priority queue 的基本用法相同,包括 size(),empty(),push(const T),top(),pop(),clear()
模版参数:
template
<
typename Value_Type ,
typename Cmp_Fn = std :: less < Value_Type >,
typename Tag = pairing_heap_tag ,
typename Allocator = std :: allocator <char >
>
class priority_queue
Tag 表示所使用的堆的类型
可以是配对堆(pairing_heap_tag)、二叉堆(binary_heap_tag)、二项堆(binomial_heap_tag)、经改良的斐波那契堆(thin_heap_tag)、
冗余计数二项式堆(rc_binomial_heap_tag)等
可以用 begin() 和 end() 来获取 iterator 从而遍历
可以 increase_key,decrease_key 和删除单个元素
可以合并
* point iterator push(const reference)
* void modify(point iterator, const reference)
* void erase(point iterator)
priority_queue <int > p;
priority_queue <int >:: point_iterator it = p. push (0);
p. push (1); p. push (2);
p. modify (it , 3);
assert (p. top () == 3);
p. erase (it );
assert (p. top () == 2);
* void join(priority queue &other)
把 other 合并到 *this,然后 other 会被清空
共有 5 种操作:push、pop、modify、erase、join
配对堆(pairing_heap_tag):push 和 join 为 O(1),其余为均摊 O(logn)
二叉堆(binary_heap_tag) :只支持 push 和 pop,均为均摊 O(logn)
二项堆(binomial_heap_tag)