比方说,我有一个Rectangle
类,像这样:多态性在C堆类++
class Rectangle {
public:
double width;
double height;
}
现在我想两个可能不同的Rectangles
列表存储为堆,但第一个堆,应根据width
第二个是height
。另外,我希望使用stl的make_heap
函数来heapify。理想情况下,我应该可以在堆上调用.heapify()
,并根据它所属的类,通过将正确的比较函数传递给make_heap
(可能使用动态调度),使堆被占用。我有如下:
class Heap {
public:
vector<Rectangle> data;
virtual bool comp(Rectangle a, Rectangle b);
void heapify() { make_heap(data.begin(), data.end(), comp); }
// other methods that make use of comp directly
}
class WidthHeap : public Heap {
bool comp(Rectangle a, Rectangle b); // compares by width
}
class HeightHeap : public Heap {
bool comp(Rectangle a, Rectangle b); // compares by height
}
这是完全错误的,因为我想我只是不使用C++,这就是为什么我想您的帮助理解的函数。
什么,具体而言,是“全部错误”? – 2012-01-15 19:12:52