queue队列
- 先进先出

priority_queue优先队列
- 底层逻辑是堆
- 按照一定的优先级进行排序的
- 从大到小

修改比较函数的方法
- 自定义比较函数
struct Compare
{
bool operator()(int a,int b)
{
return a > b;
}
};
int main()
{
priority_queue<int,vector<int>,Compare> pq;
- 自定义比较函数
auto compare = [](int a,int b){
return a > b;
};
priority_queue<int,vector<int>,decltype(compare)> pq(compare);
- 如果队列中的元素类型比较简单,可以直接使用greater来修饰比较方法
priority_queue<int,vector<int>,greater<int>>pq;
greater函数对象定义在<functional>
头文件中
deque双端队列
- 允许两端进行插入和删除
- 常用函数

示例1

#include<bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int m;cin >> m;
queue<string> V,N;
while(m --)
{
string op;cin >> op;
if(op == "IN")
{
string name,q;cin >> name >> q;
if(q == "V")V.push(name);
else N.push(name);
}else
{
string q;cin >> q;
if(q == "V")V.pop();
else N.pop();
}
}
while(V.size())
{
cout << V.front() << '\n';
}
while(N.size())
{
cout << N.front() << '\n';
N.pop();
}
return 0;
}
示例2

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n;cin >> n;
priority_queue<ll,vector<ll>,greater<ll>> pq;
for(int i = 1;i <= n; ++ i)
{
ll x;cin >> x;
pq.push(x);
}
ll ans = 0;
while(pq.size() >= 2)
{
ll x = pq.top();pq.pop();
ll y = pq.top();pq.pop();
ans += x + y;
pq.push(x + y);
}
cout << ans <<'\n';
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具