文章目录
Contest100000601 - 《算法笔记》6.6小节——C++标准模板库(STL)介绍->priority_queue的常见用法详解
6.3 priority_queue的常见用法详解
priority_queue称为优先队列,底层用堆实现,队首优先级最高,队列会根据优先级自动调整使得队首优先级最大。
1. priority_queue的定义
2. priority_queue容器内元素的访问
3. priority_queue常用函数实例解析
(1)push()
(2)top()
(3)pop()
(4)empty()
(5)size()
priority_queue函数整合代码
//priority_queue函数代码整合
#include <iostream>
#include <queue>
using namespace std;
int main(){
priority_queue<int> q;
//---1---push()
q.push(3);
q.push(4);
q.push(1);
//---2---top()
cout<<"优先队列341的队首为:";
printf("%d\n",q.top());
//---3---pop()
q.pop();
cout<<"弹出队首元素后优先队列的队首为:";
printf("%d\n",q.top());
//---4---empty()
cout<<"此时队列为空吗?"<<endl;
if(q.empty() == true)printf("Empty\n");
else printf("Not Empty\n");
//---5---size()
cout<<"此时队列大小为:"<<q.size()<<endl;
return 0;
}
4. priority_queue内元素优先级的设置
(1)基本数据类型的优先级设置
//优先级队列优先级设置
#include <iostream>
#include <queue>
using namespace std;
int main(){
//---1---基本数据类型:设置优先级队列总是把最小的元素放在队首
priority_queue<int,vector<int>,greater<int> > q;//注意<int> >之间的空格
q.push(3);
q.push(4);
q.push(1);
printf("%d\n",q.top());
return 0;
}
(2)结构体的优先级设置
//结构体优先级设置
#include <iostream>
#include <queue>
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<fruit> q;
f1.name = "桃子";
f1.price = 3;
f2.name = "梨子";
f2.price = 4;
f3.name = "苹果";
f3.price = 1;
q.push(f1);
q.push(f2);
q.push(f3);
cout<<q.top().name<<" "<<q.top().price<<endl;
return 0;
}
//结构体优先级设置
#include <iostream>
#include <queue>
using namespace std;
struct fruit{
string name;
int price;
friend bool operator < (fruit f1,fruit f2){
return f1.price < f2.price;
}
}f1,f2,f3;
struct cmp{
bool operator () (fruit f1,fruit f2){
return f1.price > f2.price;
}
};
int main(){
//重载优先级结构体进行排序
priority_queue<fruit> q;
f1.name = "桃子";
f1.price = 3;
f2.name = "梨子";
f2.price = 4;
f3.name = "苹果";
f3.price = 1;
q.push(f1);
q.push(f2);
q.push(f3);
cout<<q.top().name<<" "<<q.top().price<<endl;
//用cmp函数优先级排序
priority_queue<fruit,vector<fruit>,cmp> qq;
f1.name = "桃子";
f1.price = 3;
f2.name = "梨子";
f2.price = 4;
f3.name = "苹果";
f3.price = 1;
qq.push(f1);
qq.push(f2);
qq.push(f3);
cout<<qq.top().name<<" "<<qq.top().price<<endl;
return 0;
}
5. priority_queue的常见用途
Codeup习题
Contest100000601 - 《算法笔记》6.6小节——C++标准模板库(STL)介绍->priority_queue的常见用法详解
链接: http://codeup.cn/contest.php?cid=100000601
1985-ProblemA-任务调度
链接:http://codeup.cn/problem.php?cid=100000601&pid=0
参考大佬代码加上自己理解注释如下:
//1985-ProblemA-任务调度
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <unordered_map>
using namespace std;
//将任务名name和优先级level存入结构体,并使用优先队列设定排序方式
//利用不排序map,将name映射level,存入统计的优先级
struct task{
int level;//任务优先级
string name;//任务名称
friend bool operator <(const task &a, const task &b){//排序规则
if(a.level != b.level) return a.level > b.level;
else return a.name > b.name;
}
};
void display(string str,priority_queue<task> &task1,unordered_map<string,int> &list){
int index = str.find('(');//定位字符串中左括号
string first = str.substr(0,index);//提取任务编号
str.erase(0,index+1);
int firstlevel;
if(list.count(first) == 0){//如果该任务未被处理过,则进行处理
task newtask;
newtask.name = first;//创建一个新任务并将提取的任务号赋值给它
newtask.level = 0;
task1.push(newtask);
firstlevel = list[first] = 0;//设置初始优先级为0
}
else firstlevel = list[first];//如果该任务已经被处理过,则直接将list[first]复制给它
while(!str.empty()){
string last;
index = str.find(',');//定位剩余字符串的逗号
if(index == string::npos){//没有逗号时
index = str.find(')');
last = str.substr(0,index);
str.clear();
}
else{//找到逗号时
last = str.substr(0,index);//将之前的后续任务赋值给substr
str.erase(0,index + 1);
}
if(last != "NULL"){//若有后续任务,进行创建与优先级处理
task newtask;//声明
newtask.name = last;
newtask.level = firstlevel + 1;//后续任务在当前任务优先级的基础上加一
list[last] = newtask.level;
task1.push(newtask);//参数
}
}
}
int main(){
int n;
while(cin>>n){
priority_queue<task> task;
unordered_map<string,int> list;
for(int i = 0;i < n;i++){//输入字符串信息并进行处理
string s;
cin >> s;
display(s,task,list);
}
while(!task.empty()){//输出存储的任务信息
cout<<task.top().name;
task.pop();
if(!task.empty()) cout<<" ";
}
list.clear();
}
return 0;
}
priority_queue小结
priority_queue称为优先队列,底层用堆实现,队首优先级最高,队列会根据优先级自动调整使得队首优先级最大;且优先级可以自主设置;单队列只能通过top()函数访问队首元素;常用函数有push()、top()、pop()、empty()、size();优先级队列常用于解决一些贪心问题,还可以优化Dikstra算法等