2012-02-29 10 views
0

がここにある私の間隔ヒープ実装は、コードがなぜvisual C++ 2010にheap [i] .leftとheap [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; 
} 

問題を下回っていること私が入力すると、ヒープ[1]。それは私のオプションが表示されません(左、右)なぜですか?それは私のコードの間違いですか?またはいくつかのミスC++のエディタ?コンパイラ?助けてください

答えて

1

テンプレートされているので、intellisenseはあなたのクラスのために働いている疑いがあります。どのバージョンのVisual Studioですか? Intellisenseはいくつかの理由で失敗する可能性があります。実際にコードのインデックス作成/コンパイルが完了し、コードが実際にコンパイルされますか?

+0

はいいいえ、私はVisual Studio 2010を使用しています –

+0

私は以前vs2010で同じ問題があったので、私はあなたがIDEの限界に遭遇するかもしれないと思います。 –

+0

C++にはVisual Studioで2番目のIntellisenseサポートがあります。 –

関連する問題