C++ 区间堆上又出现了一个奇怪的情况
当我编译这段代码时C++ 区间堆上又出现了一个奇怪的情况,c++,heap,C++,Heap,当我编译这段代码时 #include <iostream> #include<algorithm> using namespace std; template <class T> class IntervalHeap; template <class T> class TwoElement { friend class IntervalHeap <T>; public: T left, right;
#include <iostream>
#include<algorithm>
using namespace std;
template <class T> class IntervalHeap;
template <class T>
class TwoElement
{
friend class IntervalHeap <T>;
public:
T left,
right;
};
template<class T>
class IntervalHeap
{
public:
IntervalHeap(int heapsize=10);
~IntervalHeap(){delete[] heap;}
int size()const { return currentsize;}
T Min()
{
if (currentsize==0)
// throw OutOfBounds();
return heap[1].left;
}
T Max() {
if(currentsize==0)
//throw OutOfBounds();
return heap[1].right;
}
IntervalHeap<T>& Insert(const T& x);
IntervalHeap<T>& DeleteMin(T& x);
IntervalHeap<T>& DeleteMax(T& x);
private:
int currentsize;//number of elemnts in heap
int Maxsize;//max elements permited
TwoElement<T>*heap;//element array
};
template<class T>
IntervalHeap<T>::IntervalHeap(int currentsize)
{
Maxsize=heapsize;
//determine number of array positions needed
//array will be heap[0:n-1];
int n=Maxsize/2+Maxsize%2+1;
heap=new TwoElement<T>[n];
currentsize=0;
}
template<class T>
IntervalHeap<T>& IntervalHeap<T>::Insert(const T& x)
{
if (currentsize==Maxsize)
exit(1);
if (currentsize<2)
{
if(x<heap[1].left)
heap[1].left=x;
else heap[1].right=x;
else
{
heap[1].left=x;
heap[1].right=x;
}
curentsize++;
return *this;
}
int lastnode=currentsize/2+currentsize%2;
bool minHeap;
if (currentsize%2)
if (x<heap[lastnode].left)
minHeap=true;
else
{
lastnode++;
if (x<=heap[lastnode/2].left)
minheap=true;
else
minheap=false;
}
if (minHeap) //fix min heap interval heap
{
int i=lastnode;
while (i!=1 && x<heap[i/2].left){
heap[i].left=heap[i/2].left;
i/=2;
}
heap[i].left=x;
currentsize++;
if (currentsize%2)
heap[lastnode].right=heap[lastnode].left;
}
else
{
int i=lastnode;
while(i!=1 && x>heap[i/2].right){
heap[i].right=heap[i/2].right;
i/=2;
}
heap[i].right=x;
currentsize++;
if (currentsize%2)
heap[lastnode].left=heap[lastnode].right;
}
return *this;
}
template<class T>
IntervalHeap<T>& IntervalHeap<T>::DeleteMax(T &x)
{
if (currentsize==0)
exit(1);
x=heap[1].right;
int lastnode=currentsize/2+currentsize%2;
T y;
if (currentsize %2)
{
y=heap[lastnode].left;
lastnode--;
}
else{
y=heap[lastnode].right;
heap[lastnode].right=heap[lastnode].left;
}
currentsize--;
int i=1,ci=2;
while(ci<lastnode){
if (ci<lastnode && heap[ci].right<heap[ci+1]) ci++;
if (y>=heap[ci].right) break;
//can't put y in heap[i]
heap[i].right=heap[ci].right;
if (y<heap[ci].left)
::swap(y,heap[ci].left);
i=ci;
ci*=2;
}
heap[i].right=y;
return *this;
}
template<class T>
IntervalHeap<T>& IntervalHeap<T>::DeleteMin(T &x)
{
if (currentsize==0)
exit(1);
x=heap[1].left;
int lastnode=currentsize/2+currentsize%2;
T y;
if (currentsize%2)
{
y=heap[lastnode].left;
lastnode--;
}
else
{
y=heap[lastnode].right;
heap[lastnode].right=heap[lastnode].left;
}
currentsize--;
int i=1;
int ci=2;
while(ci<=lastnode) //find place for y
{
if (ci<lastnode && heap[ci].left>heap[ci+1].left) ci++;
if (y<=heap[ci].left) break;
heap[i].left=heap[ci].left;
if (y>heap[ci].right)
::swap(y,heap[ci].right);
i=ci;
ci*=2;
}
if (i==lastnode && currentsize%2)
heap[lastnode].left=heap[lastnode].right;
else
heap[i].left=y;
return *this
}
int main(){
return 0;
}
#包括
#包括
使用名称空间std;
模板类间隔堆;
样板
二类元素
{
朋友级间隔;
公众:
我没有离开,
正当
};
样板
类间隔堆
{
公众:
间隔堆(int-heapsize=10);
~IntervalHeap(){delete[]heap;}
int size()常量{return currentsize;}
T Min()
{
如果(currentsize==0)
//抛出边界();
返回堆[1]。左;
}
T Max(){
如果(currentsize==0)
//抛出边界();
返回堆[1]。对;
}
间隔堆和插入(常数T和x);
间隔堆和删除最小值(T&x);
IntervalHeap和DeleteMax(T&x);
私人:
int currentsize;//堆中的元素数
int Maxsize;//允许的最大元素数
TwoElement*堆;//元素数组
};
样板
IntervalHeap::IntervalHeap(int currentsize)
{
Maxsize=heapsize;
//确定所需的阵列位置数
//数组将是堆[0:n-1];
int n=Maxsize/2+Maxsize%2+1;
heap=新的两个元素[n];
currentsize=0;
}
样板
间隔堆和间隔堆::插入(常量T&x)
{
如果(currentsize==Maxsize)
出口(1);
如果(当前大小)
c:\users\daviti\documents\visual studio 2010\projects\heap\u project\heap\u project.cpp(43):错误C2065:“heapsize”:未声明的标识符
这是来自编译器的一条非常明显的错误消息,也可以在代码中验证:
template<class T>
IntervalHeap<T>::IntervalHeap(int currentsize)
{
Maxsize=heapsize; // <-- where does this `heapsize` come from?
//determine number of array positions needed
//array will be heap[0:n-1];
int n=Maxsize/2+Maxsize%2+1;
heap=new TwoElement<T>[n];
currentsize=0;
}
模板
IntervalHeap::IntervalHeap(int currentsize)
{
Maxsize=heapsize;//原因是您将一些变量命名错误:
IntervalHeap(int-heapsize=10);
和
模板
IntervalHeap::IntervalHeap(int currentsize)
,
currentsize++;
而不是currentsize++;
<代码> MIHEAP而不是<代码> MIHEAP。C++是区分大小写的语言,注意这种错误。 < P>这意味着变量<>代码> HeAPeSIZE < /代码>未声明:
template<class T>
IntervalHeap<T>::IntervalHeap(int currentsize)
{
Maxsize=heapsize; // here is the problem
//determine number of array positions needed
//array will be heap[0:n-1];
int n=Maxsize/2+Maxsize%2+1;
heap=new TwoElement<T>[n];
currentsize=0;
}
一条建议:即使在单句中也要使用括号:这样代码更清晰。我明白了,我写这段代码的时候是晚上,我的眼睛很累,非常感谢,因为如果你没有专门化模板,那么它就不会进行语义检查。例如,如何修复我的插入方法?一些人说有错误,这就是结束)
对于if(currentsizeLet's see:您声明了模板构造函数,如IntervalHeap(int-heapsize=10);
但在模板构造函数定义中更改了参数名称:IntervalHeap::IntervalHeap(int-currentsize)
。我认为您必须将Maxsize=heapsize;
更改为Maxsize=currentsize;
。接下来,您在Insert方法中忘记了代码上的一些braket。如果(currentsizei找不到,您能修复这些错误@Tio Pepe并将其发布为答案吗,我已更改了第一个,但找不到括号。)
template<class T>
IntervalHeap<T>::IntervalHeap(int currentsize)
{
Maxsize=heapsize; // <-- where does this `heapsize` come from?
//determine number of array positions needed
//array will be heap[0:n-1];
int n=Maxsize/2+Maxsize%2+1;
heap=new TwoElement<T>[n];
currentsize=0;
}
template<class T>
IntervalHeap<T>::IntervalHeap(int currentsize)
{
Maxsize=heapsize; // here is the problem
//determine number of array positions needed
//array will be heap[0:n-1];
int n=Maxsize/2+Maxsize%2+1;
heap=new TwoElement<T>[n];
currentsize=0;
}
template<class T>
IntervalHeap<T>& IntervalHeap<T>::Insert(const T& x)
{
if (currentsize==Maxsize)
exit(1);
if (currentsize<2)
{
if(x<heap[1].left)
heap[1].left=x;
else heap[1].right=x;
} // this is new bracket
else
{
heap[1].left=x;
heap[1].right=x;
}
curentsize++;
return *this;
.........
}