C++ priority_queue的替代品

原文链接: http://www.cnblogs.com/Yuhuger/p/9902343.html

好无聊啊,写博客吧。

众所周知,pq跑的比set快

好吧,但是pq的默认容器是vector,不吸氧不够快啊。

在数次尝试用pq卡常失败后,我决定看一看pq的实现。

发现这东西不是很行,就几十行代码,调用了三个函数,make_heap push_heap pop_heap

好吧,有内部实现还不能魔改吗?

#include <bits/stdc++.h>
using namespace std;
struct mypq{
    static const int MAX_LEN=2333333;
    int tp,a[MAX_LEN];
    mypq(){
        tp=0;
    }
    mypq(int *be,int *en){
        tp=0;
        for (int *i=be; i!=en; ++i) a[++tp]=*i;
        make_heap(a+1,a+tp+1);
    }
    void push(int v){
        a[++tp]=v;
        push_heap(a+1,a+tp+1);
    }
    void pop(){
        pop_heap(a+1,a+tp+1);
        --tp;
    }
};
int main(){
    
}

好吧,我不是很懂内存分配那套理论,所以还不支持可变长度,但至少跑的比vector做容器的快吧,heap的三个函数也支持传入仿函数。

转载于:https://www.cnblogs.com/Yuhuger/p/9902343.html

猜你喜欢

转载自blog.csdn.net/weixin_30797199/article/details/94882031