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++.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.