STL容器适配器:priority_queue

1. 概念
priority_queue和queue很像,是一个容器适配器,允许在一端插入元素,另一端取出元素。和queue不同的是,priority_queue的元素具有“权值”的概念,即推入的元素不是按照其推入的顺序排列,而是按照权值自动排列,权值可以默认,也可以自定义。
C++ : Reference :
Priority queues are a type of container adaptors, specifically designed such that its first element is always the greatest of the elements it contains, according to some strict weak ordering condition.
This context is similar to a heap where only the max heap element can be retrieved (the one at the top in the priority queue) and elements can be inserted indefinitely.

2. API
priority_queue API:
(constructor) Construct priority queue (public member function)
empty Test whether container is empty (public member function)
size Return size (public member function)
top Access top element (public member function)
push Insert element (public member function)
pop Remove top element (public member function)

3. priority_queue实现
最大堆的特点可以满足priority_queue按权值高低自动升序的特点,STL中提供api结构构造堆,其实现一般是以vector表现的完全二叉树。
堆相关API如下
push_heap
template <class RandomAccessIterator>
void push_heap ( RandomAccessIterator first, RandomAccessIterator last );
template <class RandomAccessIterator, class Compare>
void push_heap ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );
数据入堆,入堆的过程中,需要重构堆,以保持堆的结构

pop_heap
template <class RandomAccessIterator>
void pop_heap ( RandomAccessIterator first, RandomAccessIterator last );
template <class RandomAccessIterator, class Compare>
void pop_heap ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );
pop_heap函数将权值最大函数置于容器尾端,在不包括最后一个元素的前提下,重构堆,以保持堆结构。特别注意的是,pop_heap并不会删除堆顶元素,只是将堆顶元素放置到容器尾端

make_heap:
template <class RandomAccessIterator>
void make_heap ( RandomAccessIterator first, RandomAccessIterator last );
template <class RandomAccessIterator, class Compare>
void make_heap ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );
make_heap通过算法将一段现有数据转化为heap

sort_heap:
template <class RandomAccessIterator>
void sort_heap ( RandomAccessIterator first, RandomAccessIterator last );
template <class RandomAccessIterator, class Compare>
void sort_heap ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );
本质上是通过不断pop_heap调整heap从而形成一个递增序列

一个堆使用实例:

int myints[] = {10,15,20,5,6}; vector<int> v(myints,myints+5); //构造 vector vector<int>::iterator it; make_heap (v.begin(),v.end()); //创建堆,使用默认比较 cout << "The max:" << v.front() << endl; //查看构建堆之后最大的元素 pop_heap (v.begin(),v.end()); //弹出堆顶元素 v.pop_back(); //删除调用pop_heap后放置到容器尾部的元素 cout << "max heap after pop : " << v.front() << endl; //查看重构堆之后最大的元素 sort_heap (v.begin(),v.end()); //堆排序 //查看所有元素 for (unsigned i=0; i<v.size(); i++) { cout << " " << v[i]; }

4. priority_queue使用实例

#include <iostream> #include <queue> using namespace std; //数据节点 struct data{ int x; int y; data( int a, int b ):x(a), y(b) { } }; //data.x+data.y小的,权值大 struct cmp{ bool operator() ( data a, data b ) { return ((a.x+a.y)>(b.x+b.y)); } }; int _tmain(int argc, _TCHAR* argv[]) { priority_queue<data, vector<data>, cmp> q; //数据入堆 for( int i = 0; i < 5; ++i ) { q.push( data(i,i+1) ); } //数据出堆 while( !q.empty() ) { cout << q.top().x << ' ' << q.top().y << endl; q.pop(); } system("pause"); return 0; }

输出:
0 1
1 2
2 3
3 4
4 5

5. 结语
priority_queue是一种特殊的queue,其包含了权值的概念,本质是一个堆,一般情况下,其实现是以vector作为底层容器;由于priority_queue的元素存取规则和queue一样,是一种受限的存取访问,因此,priority_queue没有迭代器。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值