priority_queue(优先队列)用法总结

优先队列(priority queue)

  优先级队列 是不一样于先进先出队列的另外一种队列。每次从队列中取出的是具备最高优先权的元素。ios

       首先它是一个队列,可是它强调了“优先”二字,因此,已经不能算是通常意义上的队列了,它的“优先”意指取队首元素时,有必定的选择性,即根据元素的属性选择某一项值最优的出队~算法

 

关于priority_queue
1,关于STL中的priority_queue:肯定用top()查看顶部元素时,该元素是具备最高优先级的一个元素. 调用pop()删除以后,将促使下一个元素进入该位置. 
2,如同stack和queue,priority_queue是一个基于基本序列容器进行构建的适配器,默认的序列器是vector.
数组

priority_queue<Type,Container,Compare>  
  
//  Type 为数据类型   
//  Container 为保存数据的容器, 必须是用数组实现的容器,好比 vector, deque 但不能用list。STL里面默认用的是 vector  
//  Compare为元素比较方式。. 比较方式默认用 operator< , 因此若是你把后面俩个参数缺省的话,优先队列就是大顶堆,队头元素最大。

经常使用的操做以下:less

empty()  若是优先队列为空,则返回真 
pop()  删除第一个元素 
push()  加入一个元素 
size()  返回优先队列中拥有的元素的个数 
top()  返回优先队列中有最高优先级的元素 
函数

 

可是在使用时必须注意:priority_queue放置元素时,不会判断元素是否重复。(由于在模板的第二个参数时顺序容器,不能保证元素的惟一性)此外能够替代默认的Compare函数spa

 

priority_queue 调用 STL里面的 make_heap(), pop_heap(), push_heap() 算法
实 现,也算是堆的另一种形式。code

先写一个用 STL 里面堆算法实现的与真正的STL里面的 priority_queue 用法相
似的 priority_queue, 以加深对 priority_queue 的理解htm

 

#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() );  
        }  
        int top()    { return data.front(); }  
        int size()   { return data.size();  }  
        bool empty() { return data.empty(); }  
  
};  
// STL里面的 priority_queue 写法与此类似,只是增长了模板及相关的迭代器什么的。  
  
  
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;  
}
看例子:

#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<>,对于基本类型能够用这个仿函数声明小顶堆。( less< >则是从大到小)
例子:
队列

#include <iostream>     
#include <queue>     
using namespace std;    
    
int main()  
{    
    priority_queue<int, vector<int>, greater<int> > q;  //注意 greater<int>和后面一个'>'要有空格分开!   
        
    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;  
    int 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;  
    int 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;  
}

以上例子的
struct cmp{  
    bool operator() ( Node a, Node b ){  
        if( a.x==b.x )  
            return a.y>b.y;  
        return a.x>b.x;  
    }  
};

为重点。

 

例2:

#include<iostream>  
#include<functional>  
#include<cstdlib>  
#include<algorithm>  
#include<queue>  
#include<set>  
#include<vector>  
#include<string>  
using namespace std;  
  
struct Node{  
    int data;  
    Node() {}  
    Node( int n ) { data=n; }  
};  
  
struct myCmp: binary_function< Node,Node,bool >{  
    bool operator () (const Node &a,const Node &b) {  
        return a.data < b.data;  
    }  
};  
  
/* 
根据实践即使不从 binary_function 派生也是可使用的 
 
class myCmp{ 
public:  
    bool operator () (const Node& a,const Node& b){ 
       return a.data < b.data;    
    } 
}; 
 
*/  
int main()  
{  
    priority_queue<Node,vector<Node>,myCmp> PQ;  
  
    set<Node,myCmp> s;  
    Node arr[6]={1,-1,2,-2,3,-3};  
    sort(arr,arr+6,myCmp() );  
  
    return 0;  
}