priorty_queue用法

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
//#include <queue>
#include <cstdlib>
#include <iomanip>
using namespace std;

#define rep(i, a, b) for( i = (a); i <= (b); i++)
#define reps(i, a, b) for( i = (a); i < (b); i++)
#define pb push_back
#define ps push
#define mp make_pair
#define CLR(x,t) memset(x,t,sizeof x)
#define LEN(X) strlen(X)
#define F first
#define S second
#define Debug(x) cout<<#x<<"="<<x<<endl;


const double euler_r = 0.57721566490153286060651209;
const double pi = 3.141592653589793238462643383279;
const int inf=~0U>>1;
const int MOD = int(1e9) + 7;
const double EPS=1e-6;

typedef long long LL;

//priority_queue<int> q;

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();}
    // 5 4 3 2
    return 0;
}


</pre><pre code_snippet_id="462650" snippet_file_name="blog_20140902_3_9681810" name="code" class="cpp">


#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cstdlib>
#include <iomanip>
using namespace std;

#define rep(i, a, b) for( i = (a); i <= (b); i++)
#define reps(i, a, b) for( i = (a); i < (b); i++)
#define pb push_back
#define ps push
#define mp make_pair
#define CLR(x,t) memset(x,t,sizeof x)
#define LEN(X) strlen(X)
#define F first
#define S second
#define Debug(x) cout<<#x<<"="<<x<<endl;

const int inf=~0U>>1;
const int MOD = int(1e9) + 7;
const double EPS=1e-6;
typedef long long LL;

//priority_queue<Type, Container, Functional>
//三个参数分别是数据类型,容器,比较函数
//后面两个缺省的话,第二个为vector, 第三个为大元素优先,即为大顶堆
int main()
{
    int i, n = 10;

    /*大顶堆
    priority_queue<int> q;
    rep(i, 1, n) q.push(rand());
    while(!q.empty())
    {
        cout << q.top() << " ";
        q.pop();
    }
    */

    //小顶堆,要写三个参数,STL可以用greater<>
    priority_queue<int, vector<int>, greater<int> > q;
    rep(i, 1, n) q.push(rand());
    while(!q.empty())
    {
        cout << q.top() << " ";
        q.pop();
    }
    return 0;
}

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cstdlib>
#include <iomanip>
using namespace std;

#define rep(i, a, b) for( i = (a); i <= (b); i++)
#define reps(i, a, b) for( i = (a); i < (b); i++)
#define pb push_back
#define ps push
#define mp make_pair
#define CLR(x,t) memset(x,t,sizeof x)
#define LEN(X) strlen(X)
#define F first
#define S second
#define Debug(x) cout<<#x<<"="<<x<<endl;

const int inf=~0U>>1;
const int MOD = int(1e9) + 7;
const double EPS=1e-6;
typedef long long LL;

struct Node
{
    int x, y;
    Node(int a = 0, int b = 0)
    :x(a), y(b) {}

};
//对于自定义类型,则必须自己重载 operator< 或者自己写仿函数

bool operator<(Node a, Node b)
{
    return a.x > b.x || (a.x == b.x && a.y > b.y);
}
//x小的优先级高,x相等的时候y小的优先级高
int main()
{
    int i, n = 10;
    priority_queue<Node> q;
    rep(i, 1, n) q.push(Node(rand(), rand()));
    while(!q.empty())
    {
        cout << q.top().x << " " << q.top().y << endl;
        q.pop();
    }
    return 0;
}

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cstdlib>
#include <iomanip>
using namespace std;

#define rep(i, a, b) for( i = (a); i <= (b); i++)
#define reps(i, a, b) for( i = (a); i < (b); i++)
#define pb push_back
#define ps push
#define mp make_pair
#define CLR(x,t) memset(x,t,sizeof x)
#define LEN(X) strlen(X)
#define F first
#define S second
#define Debug(x) cout<<#x<<"="<<x<<endl;

const int inf=~0U>>1;
const int MOD = int(1e9) + 7;
const double EPS=1e-6;
typedef long long LL;

struct Node
{
    int x, y;
    Node(int a = 0, int b = 0)
    :x(a), y(b) {}
};

struct cmp
{
    bool operator() (Node a, Node b)
    {
        return a.x > b.x || (a.x == b.x && a.y > b.y);
    }
};

int main()
{
    int i, n = 10;
    priority_queue<Node, vector<Node>, cmp> q;
    rep(i, 1, n) q.push(Node(rand(), rand()));
    while(!q.empty())
    {
        cout << q.top().x << " " << q.top().y << endl;
        q.pop();
    }
    return 0;
}

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cstdlib>
#include <iomanip>
using namespace std;

#define rep(i, a, b) for( i = (a); i <= (b); i++)
#define reps(i, a, b) for( i = (a); i < (b); i++)
#define pb push_back
#define ps push
#define mp make_pair
#define CLR(x,t) memset(x,t,sizeof x)
#define LEN(X) strlen(X)
#define F first
#define S second
#define Debug(x) cout<<#x<<"="<<x<<endl;

const int inf=~0U>>1;
const int MOD = int(1e9) + 7;
const double EPS=1e-6;
typedef long long LL;

// 使用友元函数
struct Node
{
    int x, y;
    Node(int a = 0, int b = 0)
        :x(a), y(b) {}
    friend bool operator<(Node a, Node b)
    {
        return a.x > b.x || (a.x == b.x && a.y > b.y);
    }

};
//对于自定义类型,则必须自己重载 operator< 或者自己写仿函数


//x小的优先级高,x相等的时候y小的优先级高
int main()
{
    int i, n = 10;
    priority_queue<Node> q;
    rep(i, 1, n) q.push(Node(rand(), rand()));
    while(!q.empty())
    {
        cout << q.top().x << " " << q.top().y << endl;
        q.pop();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值