priority_queue调用 STL里面的 make_heap(), pop_heap(), push_heap() 算法实现,也算是堆的另外一种形式。先写一个用 STL 里面堆算法实现的与真正的STL里面的 priority_queue用法相似的priority_queue, 以加深对 priority_queue 的理解
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class priority_queue
{
private:
vector<int> data;
public:
void push(int t ){
data.push_back(t);
push_heap( data.begin(), data.end());
}
void pop(){
pop_heap( data.begin(), data.end() );
data.pop_back();
}
int top() { return data.front(); }
int size() { return data.size(); }
bool empty() { return data.empty(); }
};
int main()
{
priority_queue test;
test.push( 3 );
test.push( 5 );
test.push( 2 );
test.push( 4 );
while( !test.empty() ){
cout << test.top() << endl;
test.pop(); }
return 0;
}
STL里面的 priority_queue 写法与此相似,只是增加了模板及相关的迭代器什么的。
priority_queue 对于基本类型的使用方法相对简单。他的模板声明带有三个参数:
priority_queue<Type, Container, Functional>
其中Type 为数据类型, Container 为保存数据的容器,Functional 为元素比较方式。
Container 必须是用数组实现的容器,比如 vector, deque 但不能用 list.
STL里面默认用的是 vector. 比较方式默认用 operator< , 所以如果你把后面俩个参数缺省的话,
优先队列就是大顶堆,队头元素最大。
#include <iostream>
#include <queue>
using namespace std;
int main(){
priority_queue<int> q;
for(int i= 0; i< 10; ++i ) q.push( rand() );
while( !q.empty() ){
cout << q.top() << endl;
q.pop();
}
getchar();
return 0;
}
如果要用到小顶堆,则一般要把模板的三个参数都带进去。
STL里面定义了一个仿函数 greater<>,对于基本类型可以用这个仿函数声明小顶堆
#include <iostream>
#include <queue>
using namespace std;
int main(){
priority_queue<int, vector<int>, greater<int> > q;
for(int i= 0; i< 10; ++i ) q.push( rand() );
while( !q.empty() ){
cout << q.top() << endl;
q.pop();
}
getchar();
return 0;
}
对于自定义类型,则必须自己重载 operator< 或者自己写仿函数
#include <iostream>
#include <queue>
using namespace std;
struct Node{
int x, y;
Node(int a= 0, int b= 0 ):
x(a), y(b) {}
};
bool operator<( Node a, Node b ){
if( a.x== b.x ) return a.y> b.y;
return a.x> b.x;
}
int main(){
priority_queue<Node> q;
for(int i= 0; i< 10; ++i )
q.push( Node( rand(),rand() ) );
while( !q.empty() ){
cout << q.top().x << ' ' << q.top().y << endl;
q.pop();
}
getchar();
return 0;
}
自定义类型重载 operator< 后,声明对象时就可以只带一个模板参数。
但此时不能像基本类型这样声明
priority_queue<Node, vector<Node>, greater<Node> >;
原因是 greater<Node> 没有定义,如果想用这种方法定义则可以按如下方式:
#include <iostream>
#include <queue>
using namespace std;
struct Node{
int x, y;
Node(int a= 0, int b= 0 ):
x(a), y(b) {}
};
struct cmp{
bool operator() ( Node a, Node b ){
if( a.x== b.x ) return a.y> b.y;
return a.x> b.x; }
};
int main(){
priority_queue<Node, vector<Node>, cmp> q;
for(int i= 0; i< 10; ++i )
q.push( Node( rand(),rand() ) );
while( !q.empty() ){
cout << q.top().x << ' ' << q.top().y << endl;
q.pop();
}
getchar();
return 0;
}
|
转载:http://blog.chinaunix.net/space.php?uid=533684&do=blog&cuid=2615612
ps:如果重载operator > 可直接使用priority_queue<Node,vector<Node>,greater<Node>>
分享到:
相关推荐
priority_queue用法,希望对大家会有所帮助
用法将此添加到您的Cargo.toml : [ dependencies ]keyed_priority_queue = " 0.3 " 代码示例: use keyed_priority_queue :: {KeyedPriorityQueue, Entry};let mut queue = KeyedPriorityQueue :: new ();// ...
首先要包含头文件#include<queue>, 他和queue不同的就在于我们可以自定义其中数据的优先级, 让优先级高的排在队列前面,优先出队。 优先队列具有队列的所有特性,包括队列的基本操作,只是在这基础上添加了内部的一个...
此实现的独创性在于它提供了一种O(log n) remove方法,该方法使用哈希映射来跟踪堆中元素的位置。 由于维护此映射不是免费的,因此只能通过将fastLookup: true传递给构造函数来有选择地激活它。 有关详细信息,请...
// Creating a new priority queue. var queue = new PriorityQueue(function (a, b) { return a - b; }); // Adding elements to the queue. queue.enqueue(2); queue.enqueue(0); queue.enqueue(1); // Removing...
使用priority_queue(即堆)的不同方式 :mount_fuji: 默认声明 priority_queue< int > pq; // creates max-heap priority_queue< int , vector< int >> pq; // creates max-heap 为 priority_queue 编写...
上传使用方法: NSData *data = UIImageJPEGRepresentation(image, 0.3); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ // [NSThread sleepForTimeInterval:1]; ...
priority_queue 描述 Priority Queue比Queue更专业的数据结构。 与普通队列一样,优先级队列具有相同的方法,但有很大的不同。 在“优先级”队列中,项是按键值排序的,因此,键值最低的项在前,而键值最高的项在后...
可以同时从队列中弹出多个项目,可轻松查看每个队列中有多少个项目redis-priority-queue基于,所有排序集命令都可以与该项目一起使用。客户群基本用法重击例子 -- Genericredis-cli --eval src/redis-priority-queue...
这个文档介绍了PBDS库中的一些数据结构,包括rope、priority_queue以及tree的用法。
STL-ARR,arry, file,list,map,priority_queue,set,share_ptr,stack,string,template,等基本数据类型
// declarea a custom function for comparing the priority values of 2 items in the queue function comparator ( a , b ) { return a < b ? true : false } // create a new priority queue that can hold a...
小编注:此代码没有demo,不过用法很简单,可以去代码出处的github页面查看,或者见下面: dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // This runs in a background ...
颜色查找器 iOS 上 UIImage 上的主要颜色... dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ //inRect is to be implemented, it is useless currently [self.sourceImageView.image
10.2.7优先级队列priority_queue 110 10.2.8Set和multiset容器 111 10.2.9Map和multimap容器 118 10.2.10容器共性机制研究 123 10.2.11其他 124 10.3算法 125 10.3.1算法基础 125 10.3.2STL算法中函数对象和谓词 138...
用法 import { SortedQueue } from "sorted-queue" ; const queue = new SortedQueue ( ) ; // `queue.push()` adds a value to the queue and returns an object // `item` with the `item.value` set to the ...
本文介绍了Amazon’s Simple Queue Service (SQS)的相关的队列使用方法,SQS使用java编程实现
感谢使用过Android Priority JobQueue的每个人。 它是在没有JobScheduler的世界中设计的,RxJava并不流行,甚至Kotlin都不是公开诞生的。 今天,该项目中的大部分学习内容都属于WorkManager(Android官方的延迟任务...
CTL是ISO C11的一种快速编译,类型安全,仅标头的,类似于模板的库。 C模板库(CTL)CTL是ISO C11的快速编译,类型安全,仅...std :: string vec.h = std :: vector用法使用内置或typedef类型T配置CTL容器。 #定义P #d