gpt4 book ai didi

c++ - 为什么 visual c++ 2010 不显示 heap[i].left 和 heap[i].right 选项?

转载 作者:行者123 更新时间:2023-11-28 08:09:18 24 4
gpt4 key购买 nike

这是我的区间堆实现,代码如下

#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;
}

问题是当我输入 heap[1] 时。它没有显示选项(左,右)为什么?是我的代码错误还是 c++ 编辑器有一些遗漏?编译器?请帮助我

最佳答案

我怀疑智能感知不适用于您的类(class),因为它是模板化的。它是哪个版本的visual studio? Intellisense 可能因多种原因而失败,它是否真的完成了索引/编译您的代码,您的代码是否真的编译了?

关于c++ - 为什么 visual c++ 2010 不显示 heap[i].left 和 heap[i].right 选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9507265/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com