搜索
查看: 3744|回复: 0
打印 上一主题 下一主题

[C/C++语言] C++ STL 优先队列(priority_queue代替堆)

[复制链接]
跳转到指定楼层
楼主
发表于 2013-2-20 14:51:36 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include < queue >
#include < iostream >
using namespace std;

struct myComp
{
    bool operator()(const int &a, const int &b)
    {
        return a > b ;
    }
};

int main()
{
    priority_queue < int ,  vector < int > , myComp > pq;
   
    pq.push(1);
    pq.push(10);
    pq.push(9);
    pq.push(4);
    pq.push(6);
    pq.push(7);
   
    cout << pq.size() << endl;
   
    while( pq.empty() != true )
    {
        cout << pq.top() << " ";
        pq.pop();
    }
   
    cout << endl;
   
    getchar();
    getchar();
    return 0;
}

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

广播台
特别关注
快速回复 返回顶部 返回列表