您的位置:首页 > 产品设计 > UI/UE

priority queue

2016-03-21 09:32 429 查看
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());
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: