pb_ds学习

  pb_ds俗称平板电视(bushi)是GNU-C++自带的c++扩展库,实现了许多数据结构。

因为是扩展库要单独写 

using namespace __gnu_pbds;

下面有三种主要的数据结构。

1.哈希表


头文件

#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
using namespace __gnu_pbds

两种定义方式

cc_hash_table<string,int>mp1;//拉链法
gp_hash_table<string,int>mp2;//查探法(快于上一个)

P.S:两种hash函数比map的效率高,且使用方法和map一样

2.堆

头文件:

与普通的优先队列用法一样

#include<ext/pb_ds/priority_queue.hpp>
using namespace __gnu_pbds;
__gnu_pbds::priority_queue<int>q;
__gnu_pbds::priority_queue<int,greater<int>,pairing_heap_tag>pq;//最快
__gnu_pbds::priority_queue<int,greater<int>,binary_heap_tag>pq;
__gnu_pbds::priority_queue<int,greater<int>,binomial_heap_tag>pq;
__gnu_pbds::priority_queue<int,greater<int>,rc_binomial_heap_tag>pq;
__gnu_pbds::priority_queue<int,greater<int>,thin_heap_tag>pq;
__gnu_pbds::priority_queue<int,greater<int>>pq;

pb_ds库的堆提供了五种tag,分别是 pairing_heap_tag, binary_heap_tag ,binomial_heap_tag , rc_binomial_heap_tag ,thin_heap_tag。因为重名的原因要加上 __gnu_pbds::

基本操作:

push()  //会返回一个迭代器
top()  //同 stl 
size()  //同 stl 
empty() //同 stl 
clear()  //同 stl 
pop()  //同 stl 
join(priority_queue &other)  //合并两个堆,other会被清空
split(Pred prd,priority_queue &other)  //分离出两个堆
modify(point_iterator it,const key)  //修改一个节点的值

优先队列迭代器:

__gnu_pbd::priority_queue<int>::point_iterator it;

猜你喜欢

转载自www.cnblogs.com/jiqimin/p/11226809.html
DS