qingcheng奕  

http://www.cplusplus.com/reference/queue/priority_queue/

priority_queue 的top始终保持着为一堆数据中的最大元素。

读取最小 O(1)

插入和删除 lg(n)

(真是又简便又好用,难怪g不要我,当时连这个都不会写,sigh...)

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

class greater_class{
public:
    bool operator()(int a,int b)
    {
        return a > b;
    }
};
int main()
{
    vector<int> num;

    priority_queue<int,vector<int>,greater_class> myQueue;

    myQueue.push(4);
    myQueue.push(3);
    myQueue.push(2);

    cout<< myQueue.top();
    myQueue.pop();
    myQueue.push(1);
    
    cout<<"print all data in priority_queue"<<endl;
    while(myQueue.empty() == false)
    {
        cout<< myQueue.top() << " ";
        myQueue.pop();
    }
}

 

posted on 2014-08-21 10:52  qingcheng奕  阅读(237)  评论(1编辑  收藏  举报