栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

[STL]priority

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

[STL]priority

目录

1. priority_queue的定义

2. priority_queue容器内元素的访问

3. priority_queue常用函数实例解析

(0)top()

(1)push()与pop()

(2)empty()

(3)size()

4. priority_queue内元素优先级的设置

(1)基本数据类型的优先级设置

(2)结构体的优先级设置


priority_queue又称为优先队列,其底层是用堆来实现的。在优先队列中,队首元素一定是当前队列中优先级最高的那一个。例如在队列有如下元素,且定义好了优先级:

桃子(优先级3)
梨子(优先级4)
苹果(优先级1)

那么出队的顺序为梨子(4)->桃子(3)->苹果(1)。

当然,可以在任何时候向优先队列里加入元素,优先队列底层的数据结构(堆)会随时调整结构,使得每次的队首元素都是优先级最大的。

这里的优先级是人为规定出来的,后面会展示如何自定义优先级。

1. priority_queue的定义

添加头文件#include ,并加上using namespace std;

priority_queue name;

这里的typename可以是任意基本数据类型或容器。

2. priority_queue容器内元素的访问

和队列不一样的是,优先队列只能通过top()函数访问队首元素(优先级最高的那个元素),时间复杂度为

#include 
#include 
using namespace std;
int main() {
    priority_queue ay;
    ay.push(3);
    ay.push(6);
    ay.push(2);
    cout << ay.top() << endl;
    return 0;
}

3. priority_queue常用函数实例解析

(0)top()

获得队首元素,示例如上。

(1)push()与pop()

push(x)使得x入队,pop(x)使队首元素(堆顶元素)出队,时间复杂度均为,其中为当前优先队列中的元素个数。

示例如下:

#include 
#include 
using namespace std;
int main() {
    priority_queue ay;
    ay.push(3);
    ay.push(6);
    ay.push(2);
    for(int i = 1; i <= 3; i ++) {
        cout << ay.top() << ' ';
        ay.pop();
    }
    return 0;
}

(2)empty()

empty()检测优先队列是否为空,返回true则空,返回false则非空。时间复杂度为。

#include 
#include 
using namespace std;
int main() {
    priority_queue ay;
    ay.push(3);
    ay.push(6);
    ay.push(2);
    for(int i = 1; i <= 3; i ++) {
        ay.pop();
        if(ay.empty() == true) cout << "Empty" << ' ';
        else cout << "Not Empty" << ' ';
    }
    return 0;
}

(3)size()

返回优先队列内元素的个数,时间复杂度为O(1)。

示例如下:

#include 
#include 
using namespace std;
int main() {
    priority_queue ay;
    ay.push(3);
    ay.push(6);
    ay.push(2);
    for(int i = 1; i <= 3; i ++) {
        ay.pop();
        cout << ay.size() << ' ';
    }
    return 0;
}

4. priority_queue内元素优先级的设置

(1)基本数据类型的优先级设置

此处指的基本数据类型是int、double、char等可以直接使用的数据类型,优先队列对它们的优先级设置一般是数字大的优先级越高,因此队首元素就是优先队列内元素最大的那个(如果是char,则是字典序最大的)。对基本数据类型来说,底下两种定义方式是等价的:

priority_queue ay;
priority_queue, less > ay;

第二种定义方式中的第二个参数填写的是用来承载底层数据结构堆的容器,vector里面的数据类型与优先队列自己的数据类型保持一致。less表示数字大的优先级越大,而greater表示数字小的优先级越大。

#include 
#include 
using namespace std;
int main() {
   priority_queue, greater > ay;
   ay.push(2);
   ay.push(4);
   ay.push(1);
   cout << ay.top() << endl;
   return 0;
}

(2)结构体的优先级设置

本文最开头举了水果的例子,可以对水果名称和价格建立一个结构体,我们希望按水果价格高的为优先级高。这就需要重载(overload)小于号“<”。这里读者只需要知道其写法即可。

#include 
#include 
#include 
using namespace std;
struct fruit{
    string name;
    int price;
    friend bool operator < (fruit f1, fruit f2) {
        return f1.price > f2.price;
    }
}f1, f2, f3;
int main() {
   priority_queue q;
   f1.name = "peach";
   f1.price = 6;
   f2.name = "apple";
   f2.price = 4;
   f3.name = "orange";
   f3.price = 3;
   q.push(f1);
   q.push(f2);
   q.push(f3);
   cout << q.top().name << endl;
   return 0;
}

这里需要注意,重载的作用与sort的cmp函数是相反的。比如上面我们将价格从大到小排序,队首元素却是价格最低的。反之亦然。原因在于优先队列默认规则为优先级高的放在队首,小于号重载成大于号只是将规则反向了一下。

最后,数据量大的时候建议使用引用来提高效率,如下:

friend bool operator < (const fruit &f1, const fruit f2) {
        return f1.price < f2.price;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/718727.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号