pb_ds(Policy-Based Data Structures)

本文详细介绍了C++的Policy-Based Data Structures(pb_ds库),包括其设计理念和功能。重点讲解了优先队列(Priority Queue)的使用,如push、pop、modify和erase等操作,并对比了不同类型的堆。同时,文章还探讨了平衡二叉树(Balanced Binary Tree)如红黑树和伸展树在数据结构中的应用,以及如何利用pb_ds库进行区间查询和操作。
摘要由CSDN通过智能技术生成

参考: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)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值