• 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;

    3.红黑树

    头文件:

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

    定义一棵红黑树(我真的不会)先码上,等学到了继续写

     顺便:所有内容皆出自这篇博客:https://blog.csdn.net/riba2534/article/details/80454602

  • 相关阅读:
    c#命名空间
    MUTC 2 B Meeting point1 二分
    高斯消元模板
    MUTC 2 C Meeting point2 切比雪夫距离orz
    MUTC 2 E Save the dwarfs DP?
    Uva 10859 Placing Lampposts 树形dp
    Uva 11552 Fewest Flops 字符串dp
    Uva 10891 Game of Sum dp博弈
    MUTC 2 D Matrix 并查集
    Uva 1456 Cellular Network 概率dp
  • 原文地址:https://www.cnblogs.com/jiqimin/p/11226809.html
Copyright © 2020-2023  润新知