C++<优先队列>详解,一次吃透

目录

一、相关定义

二、priority_queue

基本操作:

头文件:

声明方式:

1、普通方法:

2、自定义优先级:

3、结构体声明方式:


一、相关定义

普通的队列具有先进先出的特性,元素追加在队尾,如果删除的话,从队头删除。

而在优先队列中,队列中的数据被赋予了优先级

当访问元素时,优先级最高的会先被删除;

所以说优先队列是最高级数据先出

优先级队列可以用向量(vector)双向队列(deque)来实现(注意list容器不能用来实现queue),STL默认使用vector,而且是大堆(less<int>)

因为list的迭代器不是任意存取iterator,而pop中用到堆排序时是要求随机存取iterator 的!):

priority_queue<vector<int>, less<int> > pq1;     // 使用递增less<int>函数对象排序

priority_queue<deque<int>, greater<int> > pq2;   // 使用递减greater<int>函数对象排序

//greater和less是std实现的两个仿函数(就是使一个类的使用看上去像一个函数。其实现就是类中实现一个operator(),这个类就有了类似函数的行为,就是一个仿函数类了)

其成员函数有判空(empty)” 、“尺寸(Size)” 、“栈顶元素(top)” 、“压栈(push)” 、“弹栈(pop)等。

二、priority_queue

基本操作:

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

在默认的优先队列中,优先级高的先出队。

在默认的int型中先出队的为较大的数。

头文件:

#include <queue>

声明方式:

1、普通方法:

priority_queue<int> q;                 //通过操作,按照元素从大到小的顺序出队

priority_queue<int,vector<int>, greater<int> > q;    //通过操作,按照元素从小到大的顺序出队

2、自定义优先级:

struct cmp {     

  operator bool ()(int x, int y)     

  {        

     return x > y;   // x小的优先级高       //也可以写成其他方式,如: return p[x] > p[y];表示p[i]小的优先级高

  }

};

priority_queue<int, vector<int>, cmp> q;    //定义方法

//其中,第二个参数为容器类型。第三个参数为比较函数。

3、结构体声明方式:

struct node {     

  int x, y;     

  friend bool operator < (node a, node b)     

  {         

    return a.x > b.x;    //结构体中,x小的优先级高     

  }

};

priority_queue<node>q;   //定义方法

//在该结构中,y为值, x为优先级。

//通过自定义operator<操作符来比较元素中的优先级。

//在重载”<”时,最好不要重载”>”,可能会发生编译错误

三、那么如何使用呢?

1、基本类型优先队列的例子:

#include<iostream>
#include <queue>
using namespace std;
int main()
{
    //对于基础类型 默认是大顶堆
    priority_queue<int> a;
    //等同于 priority_queue<int, vector<int>, less<int> > a;

    priority_queue<int, vector<int>, greater<int> > c;  //这样就是小顶堆
    priority_queue<string> b;

    for (int i = 0; i < 5; i++)
    {
        a.push(i);
        c.push(i);
    }
    while (!a.empty())
    {
        cout << a.top() << ' ';
        a.pop();
    }
    cout << endl;

    while (!c.empty())
    {
        cout << c.top() << ' ';
        c.pop();
    }
    cout << endl;

    b.push("abc");
    b.push("abcd");
    b.push("cbd");
    while (!b.empty())
    {
        cout << b.top() << ' ';
        b.pop();
    }
    cout << endl;
    return 0;
}

2、用pair做优先队列元素的例子:

先比较frist,如果frist相等,那么就比较第二个

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main()
{
    priority_queue<pair<int, int> > a;
    pair<int, int> b(1, 2);
    pair<int, int> c(1, 3);
    pair<int, int> d(2, 5);
    a.push(d);
    a.push(c);
    a.push(b);
    while (!a.empty())
    {
        cout << a.top().first << ' ' << a.top().second << '\n';
        a.pop();
    }
}

3、用自定义类型做优先队列元素的例子

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

//方法1
struct tmp1 //运算符重载<
{
    int x;
    tmp1(int a)
    {
        x = a;
    }

    bool operator<(const tmp1& a) const
    {
        return x < a.x; //大顶堆
    }
};

//方法2
struct tmp2 //重写仿函数
{
    bool operator() (tmp1 a, tmp1 b)
    {
        return a.x < b.x; //大顶堆
    }
};

int main()
{
    tmp1 a(1);
    tmp1 b(2);
    tmp1 c(3);
    priority_queue<tmp1> d;
    d.push(b);
    d.push(c);
    d.push(a);
    while (!d.empty())
    {
        cout << d.top().x << '\n';
        d.pop();
    }
    cout << endl;

    priority_queue<tmp1, vector<tmp1>, tmp2> f;
    f.push(b);
    f.push(c);
    f.push(a);
    while (!f.empty())
    {
        cout << f.top().x << '\n';
        f.pop();
    }
}

那么此次的文章也到此结束啦,觉得有帮助的朋友们,不妨点个蛋,支持一下,谢谢!

 

  • 20
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我可以为您解答这个问题。 首先,让我们简单介绍一下什么是优先队列和01背包问题。 优先队列是一种数据结构,它可以维护一组元素,并按照一定的优先级对它们进行排序。在优先队列中,每个元素都有一个与之关联的优先级,队列会按照优先级从高到低对元素进行排序。 01背包问题是指在给定的一组物品中,选择一些物品放进容量为V的背包中,使得放进背包中的物品总价值最大。其中,每个物品只有一个,可以选择放或不放。 那么,如何使用优先队列解决01背包问题呢?我们可以使用贪心算法,每次从优先队列中取出价值最高的物品放入背包中,直到背包装满或优先队列为空。 下面是使用C语言实现的代码: ```c #include <stdio.h> #include <stdlib.h> #define MAX_N 100 // 物品的最大数量 #define MAX_V 10000 // 背包的最大容量 typedef struct { int v; // 物品的体积 int w; // 物品的价值 } item; // 用于比较两个item的优先级 int cmp(const void* a, const void* b) { item* x = *(item**)a; item* y = *(item**)b; double ratio_x = (double)x->w / x->v; double ratio_y = (double)y->w / y->v; if (ratio_x > ratio_y) { return -1; } else if (ratio_x < ratio_y) { return 1; } else { return 0; } } // 使用优先队列解决01背包问题 int knapsack(item* items[], int n, int capacity) { int i; int used = 0; int value = 0; // 定义一个优先队列 item** pq = malloc(n * sizeof(item*)); // 将物品按照单位价值从高到低排序 for (i = 0; i < n; i++) { pq[i] = items[i]; } qsort(pq, n, sizeof(item*), cmp); // 依次将优先队列中的物品放入背包中 for (i = 0; i < n; i++) { if (used + pq[i]->v <= capacity) { used += pq[i]->v; value += pq[i]->w; } else { break; } } free(pq); return value; } int main() { int n, capacity; item items[MAX_N]; int i; // 读入物品的数量和背包的容量 scanf("%d %d", &n, &capacity); // 读入每个物品的体积和价值 for (i = 0; i < n; i++) { scanf("%d %d", &items[i].v, &items[i].w); } // 使用优先队列解决01背包问题 int max_value = knapsack(items, n, capacity); // 输出结果 printf("%d\n", max_value); return 0; } ``` 以上就是使用C语言实现的优先队列解决01背包问题的代码。希望能帮到您!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值