priority queue 是一个加上heap处理规则的queue,是一个容器适配器。
缺省情况下以vector为底部容器
重要的几个函数如下:
template <class InputIterator> priority_queue(InputIterator first, InputIterator last, const Compare& x) : c(first, last), comp(x) { make_heap(c.begin(), c.end(), comp); } template <class InputIterator> priority_queue(InputIterator first, InputIterator last) : c(first, last) { make_heap(c.begin(), c.end(), comp); } // 返回优先级最高的元素 const_reference top() const { return c.front(); } // 插入元素, 并调整heap void push(const value_type& x) { __STL_TRY { c.push_back(x); //利用底层元素将元素插入 // 详细分析见<stl_heap.h>,重新堆排序 push_heap(c.begin(), c.end(), comp); } __STL_UNWIND(c.clear()); } // 弹出优先级最高的元素 void pop() { __STL_TRY { // 详细分析见<stl_heap.h> pop_heap(c.begin(), c.end(), comp); c.pop_back(); } __STL_UNWIND(c.clear()); }