极小堆-Min_heap......
看了一个极大堆的实现,大概明白了,于是心血来潮想写一个极小堆的实现,可是。。。写出来,结果有点尴尬= =!!
(有代码,才说话!)
template <typename T, class ty_>//Wrong in inheriting class vector<T>struct Min_heap:vector<T>{ typedef vector<T> me_; Min_heap(void):me_(){}//Suspected here bool empty(void) const {return me_::empty();} int size(void) const {return me_::size();} //T& operator [](int n){return me_.elem_[n];} T const& top(void) const {return me_[0];} void shiftup_min(T* a, int n) { for (int p = n/2; p>0 && a[p] > a[n]; p = (n = p)/2) swap(a[p],a[n]); } void shiftdown_min(T* a, int n) { for (int p = 1, bc = 2; bc <= n; bc = 2*p) { if (bc < n && a[bc] > a[bc+1]) ++bc; if (a[p] > a[bc]) { swap(a[p],a[bc]); p = bc; } else break; } } void push(T const& v) { me_::push_back(v); T* const a = &me_[0] - 1; shiftup_min(a,me_::size()); } void pop(void) { int const n = me_::size() - 1; swap (me_[0], me_[n]); T* const a = &me_[0] - 1; shiftdown_min(a, n); me_::pop_back(); }};typedef vector<T> me_;//这里me_是类型名me_::size();,me_::empty();//这里empty函数肯定不会是static函数me_[0];//这里me_又是当成对象使用的//如果楼主本意是将me_定义为对象的话,可以做如下修改typedef vector<T> me_; --> vector<T> me_;me_:: --> me_.