표준 : make_heap/push_heap/pop_heap :INVALID HEAP 사용하여 STL의 make_heap/push_heap/pop_heap
내가처럼 사용template <typename T, typename Cont = std::vector<T>, typename Compare = std::less<typename Cont::value_type> >
class Heap
{
public:
inline void init() { std::make_heap(m_data.begin(), m_data.end(), Compare()); }
inline void push(const T & elm) { m_data.push_back(elm); std::push_heap(m_data.begin(), m_data.end(), Compare()); }
inline const T & top() const { return m_data.front(); }
inline void pop() { std::pop_heap(m_data.begin(), m_data.end(), Compare()); m_data.pop_back(); }
private:
Cont m_data;
};
:
class DeliveryComparator
{
public:
bool operator()(Delivery * lhs, Delivery * rhs)
{
if(lhs->producer != rhs->producer) return *lhs->producer < *rhs->producer;
if(lhs->rate == rhs->rate) return lhs->diff < rhs->diff;
return lhs->rate < rhs->rate;
}
};
Heap<Delivery*, std::vector<Delivery*>, DeliveryComparator> packages;
하지만 가끔은 I INVALID HEAP 표준 디버그 메시지를받습니다. 적절한 힙 방법을 통해 힙을 사용합니다. 메시지가 발생하면 m_data는 비어 있지 않습니다.
힙이 잘못되었을 수 있습니까?
가 * 내가 (추가 대기 제외) 힙의 데이터를 편집 한 후
왜'std :: priority_queue'를 사용하지 않습니까? – kennytm
'std :: priority_queue'가 이미 힙 함수를 감싸는 래퍼가 아닌가? –
비교기에'* lhs-> producer'가 생기면'lhs-> producer'가 될까요? –