0
這是我的間隔堆實現,代碼如下爲什麼visual C++ 2010不顯示我堆[i] .left和堆[i] .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;
}
問題是,當I型,堆[1]。它不顯示我的選項(左,右)爲什麼?是我的代碼錯誤還是有一些與C + +編輯器錯過?compilator?請幫助我
yes編譯好,我正在使用visual studio 2010 – 2012-03-01 06:54:28
我之前也遇到過與vs2010相同的問題,所以我想你可能會遇到IDE的限制。 – 2012-03-02 01:18:20
C++在Visual Studio中具有二級Intellisense支持。 – 2013-02-13 10:53:20