简体   繁体   English

多路优先级队列实现

[英]multiway priority queue implementation

here is my code for multiway priority queue implementation 这是我的多路优先级队列实现的代码

#include<iostream>
#include<vector>
using namespace std;
template<class T>
class pq
{
    int d,n;
    vector<int>pq;
    vector<int>qp;
    const vector<T>&a;
    void exch(int i,int j)
    {
        int t=pq[i];
        pq[i]=pq[j];
        pq[j]=t;
        qp[pq[i]]=i;
        qp[pq[j]]=j;
    }

    void fixup(int k)
    {
        while(k>1 && a[pq[(k+d-2)/d]]>q[pq[k]])
        {
            exch(k,(k+d-2)/d);
            k=(k+d-2)/d;
        }
    }

    void fixdown(int k,int n)
    {
        int j;
        while((j=d*(k-1)+2)<=n)
        {
            for(int i=j+1;i<j+d&& i<=n;i++)
                if(a[pq[j]]>a[pq[i]])
                    j=i;
            if(!(a[pq[k]]>a[pq[j]]))
                break;
            exch(k,j);
            k=j;
        }
    }

public:
    pq(int n,const vector<T>&a,int d=3):a(a),pq(n+1,0),qp(n+1,0),n(0),d(d){}

    int empty() const
    {
        return n==0;
    }

    void insert(int v)
    {
        pq[++n]=v ;
        qp[v]=n;
        fixup(n);
    }

    int getmin()
    {
        exch(1,n);
        fixdown(1,n-1);
        return pq[--n];
    }

    void lower(int k)
    {
        fixup(qp[k]);
    }
};

int main()
{
    return 0;
}

but it has some bugs,namely i have made some incorrected constructor,here is two error which i see when run code 但是它有一些错误,即我做了一些错误的构造函数,这是我在运行代码时看到的两个错误

1>c:\users\\documents\visual studio 2012\projects\multiway_heap\multiway_heap\multiway_heap.cpp(8): error C2461: 'pq<T>' : constructor syntax missing formal parameters
1>          c:\users\\documents\visual studio 2012\projects\multiway_heap\multiway_heap\multiway_heap.cpp(68) : see reference to class template instantiation 'pq<T>' being compiled
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped =========

so please help me to understand where is bug 所以请帮助我了解错误在哪里

Try to understand what the compiler is telling you - its not that hard. 尝试了解编译器告诉您的内容-并不难。

class pq
{
/* ... */
    vector<int> pq;    // I suppose this is line 8 according to compiler error

You are using the same name for your class template and its member. 您正在为类模板及其成员使用相同的名称。 This is not legal in c++. 这在c ++中是不合法的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM