标准模板库(STL)学习探究之Priority Queue容器

来源:互联网 发布:浪潮erp软件下载 编辑:程序博客网 时间:2024/04/26 23:24

  C++ Priority Queue(优先队列)
C++优先队列类似队列,但是在这个数据结构中的元素按照一定的断言排列有序。它的头文件为<queue>。由于适配器不支持迭代,一个 priority_queue 将有没有关联的迭代器。

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

/////////////////////////////////////////////////////////////////////////////////////

构造函数
explicit priority_queue(const Pred& pr = Pred(),
    const allocator_type& al = allocator_type());
priority_queue(const value_type *first, const value_type *last,
    const Pred& pr = Pred(), const allocator_type& al = allocator_type());

empty 
语法: 
  bool empty();
empty()函数返回真(true)如果优先队列为空,否则返回假(false)。
pop 
语法: 
  void pop();
pop()函数删除优先队列中的第一个元素。
push 
语法: 
  void push( const TYPE &val );
push()函数添加一个元素到优先队列中,值为val。
size 
语法: 
  size_type size();
size()函数返回优先队列中存储的元素个数。
top 
语法: 
   TYPE &top();
top()返回一个引用,指向优先队列中有最高优先级的元素。注意只有pop()函数删除一个元素。
示例1:
#include <iostream>
#include <queue>
#include <deque>
#include <vector>
#include <functional>
using namespace std;

#if _MSC_VER > 1020   // if VC++ version is > 4.2
   using namespace std;  // std c++ libs implemented in std
   #endif

// Using priority_queue with deque
// Use of function greater sorts the items in ascending order
typedef deque<int, allocator<int> > INTDQU;
typedef priority_queue<int,INTDQU, greater<int> > INTPRQUE;

// Using priority_queue with vector
// Use of function less sorts the items in descending order
typedef vector<char, allocator<char> > CHVECTOR;
typedef priority_queue<char,CHVECTOR,less<char> > CHPRQUE;

void main(void)
{
    
    int size_q;
    INTPRQUE   q;
    CHPRQUE    p;

    // Insert items in the priority_queue(uses deque)
    q.push(42);
    q.push(100);
    q.push(49);
    q.push(201);

    // Output the item at the top using top()
    cout << q.top() << endl;
    // Output the size of priority_queue
    size_q = q.size();
    cout << "size of q is:" << size_q << endl;
    // Output items in priority_queue using top()
    // and use pop() to get to next item until
    // priority_queue is empty
    while (!q.empty())
    {
        cout << q.top() << endl;
        q.pop();
    }

// Insert items in the priority_queue(uses vector)
    p.push('c');
    p.push('a');
    p.push('d');
    p.push('m');
    p.push('h');

    // Output the item at the top using top()
    cout << p.top() << endl;

    // Output the size of priority_queue
    size_q = p.size();
    cout << "size of p is:" << size_q << endl;

    // Output items in priority_queue using top()
    // and use pop() to get to next item until
    // priority_queue is empty
    while (!p.empty())
    {
        cout << p.top() << endl;
        p.pop();
    }
}
输出结果:
        42
        size of q is:4
        42
        49
        100
        201
        m
        size of p is:5
        m
        h
        d
        c
        a

示例2:
#include<iostream>
#include<queue>
using namespace std;
struct cmp
{
 bool operator()(const int &a,const int &b)

 {
             return a<b;//按升序排序
 
 }
};
typedef priority_queue< int, vector<int>, cmp > qu;
void main()
{
     qu p;
     p.push(42);
     p.push(100);
     p.push(49);
     p.push(201);
  while (!p.empty())
     {
        cout << p.top() << endl;
        p.pop();
     }
}
输出结果:
       201
       100
       49
       42

示例3(用priority_queue实现哈夫曼树):
#include<iostream>
#include<queue>
using namespace std;
class Node 
{
public:
    int weight;
    Node* left;
    Node* right;
    Node(int w, Node* l, Node* r): weight(w), left(l), right(r) {}
    Node(int w): weight(w), left(NULL), right(NULL) {}
};

class cmp //用于priority_queue的仿函数类
{
      public :
             bool operator()(Node* a,Node* b)

             {
                  return a->weight>=b->weight;
             }
};

//传入的是指针,如果用对象去实现,你就不知道左右指针的指向了

//中序遍历
void InOrder(Node* p) 
{
     if (p != NULL) 
     {
        InOrder(p->left);
        cout<<p->weight<<'/t';
        InOrder(p->right);
     }
}
    
void freeTree(Node* p)//销毁二叉树
{
if(p->left!=NULL)
   freeTree(p->left);
if(p->right!=NULL)
   freeTree(p->right);
delete(p);
}

int main()
{
    Node* m1,*m2;
    priority_queue<Node*,vector<Node*>,cmp> q;
    for(int i=0;i<6;++i)//6个节点 
    {
            int n=rand()%100;
            q.push(new Node(n));//权值随机产生 
            cout<<n<<'/t'; 
    }
    cout<<endl;
    for(int j=1;j<6;++j)//合并5次 
    {
       m1=q.top(); q.pop();
       m2=q.top(); q.pop();
       int w=m1->weight+m2->weight;
       q.push(new Node(w,m1,m2));
    }
    Node* root=q.top();
    InOrder(root);
    cout<<endl;
    freeTree(root);
    
    system("pause");
    return 0;
}
输出结果:
41      67      34      0       69      24
41      99      0       24      24      58      34      235     67      136
69

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 一本分数线擦边过怎么办 玩具塑料球扁了怎么办 胶皮与海绵开了怎么办 安卓不支持flash了怎么办 看视频要加载flash怎么办 下水道管子铁皮破了怎么办 炸金花牌一样大怎么办 玩棋牌游戏输了怎么办 苹果7插耳机外放怎么办 出国种菠菜抓了怎么办 在菲做菠菜抓到怎么办 3串1中两个怎么办 微博账号封停怎么办 阴阳师账号被永久封停怎么办 寒刃2账号被禁用怎么办 输了好多钱我该怎么办 亲朋打鱼别处在玩怎么办 做糯米蛋的蛋清怎么办 水田地没耙地平怎么办 宝宝拉鸡蛋花样大便怎么办 电子琴伴奏区无旋律音怎么办 手机触摸屏摔坏了怎么办 手机充着电玩游戏卡怎么办? 4个月宝宝拉肚子怎么办 6个月宝宝上火怎么办 1月婴儿大便干燥怎么办 椰子鞋350线开了怎么办 打完篮球小腿肌肉酸痛怎么办 衣服穿少了感冒怎么办 侧手翻翻不过去怎么办 生完孩子胯宽了怎么办 小孩户口性质弄错了怎么办 4岁宝宝咳嗽很厉害怎么办 宝宝深夜咳嗽很厉害怎么办 2岁宝宝发热37.6怎么办 篮球气嘴慢跑气怎么办 4个月宝宝偏胖怎么办 4个月婴儿偏胖怎么办 6岁儿童偏胖怎么办 打篮球撞到头颈椎痛怎么办 血燥热引起的皮肤瘙痒怎么办