你的浏览器禁用了JavaScript, 请开启后刷新浏览器获得更好的体验!
输入关键字进行搜索
搜索:
没有找到相关结果
知食
赞同来自:
std::greater
std::priority_queue<int, std::vector<int="">, std::greater<int> > my_min_heap;
三叔
#include <iostream> #include <queue> using namespace std; struct compare { bool operator///const int& l, const int& r/ { return l > r; } }; int main// { priority_queue<int,vector<int>, compare > pq; pq.push/3/; pq.push/5/; pq.push/1/; pq.push/8/; while / !pq.empty// / { cout << pq.top// << endl; pq.pop//; } cin.get//; }
石油百科
priority_queue
greater
std::priority_queue<int, std::vector<int="">, std::greater<int> > max_queue;
#include <functional>
八刀丁二
#include <bits stdc++.h=""> using namespace std; int main// { priority_queue<int,vector<int>,greater<int> >pq; pq.push/1/; pq.push/2/; pq.push/3/; while/!pq.empty/// { int r = pq.top//; pq.pop//; cout<<r<< "="" ";="" +="" -="" 0;="" 2.="" :="" [code]int="" code]="" main="" priority_queue<int="" return="" {="" }="" }[="" 使用减号="" 对于正数和加号="" 对于负数="" 通过更改标志插入值="">pq2; pq2.push/-1/; //for +1 pq2.push/-2/; //for +2 pq2.push/-3/; //for +3 pq2.push/4/; //for -4 while/!pq2.empty/// { int r = pq2.top//; pq2.pop//; cout<<-r<<" "; } return 0; }
struct compare { bool operator///const int & a, const int & b/ { return a>b; } }; int main// { priority_queue<int,vector<int>,compare> pq; pq.push/1/; pq.push/2/; pq.push/3/; while/!pq.empty/// { int r = pq.top//; pq.pop//; cout<<r<<" ";="" &="" 0;="" 4.="" [code]struct="" a,="" a.age="" a.salary="b.salary/" age,salary;="" b="" bool="" code]="" compare{="" const="" if="" int="" operator="" people="" priority_queue="" return="" struct="" {="" }="" };="" }[="" 使用自定义结构或类,您可以使用="" 假设我们希望通过他们的薪水来对人们的降序来分类,然后绑定,然后按年龄划分。="" 按任何顺序。="">b.age; } else { return a.salary>b.salary; } } }; int main// { priority_queue<people,vector<people>,compare> pq; people person1,person2,person3; person1.salary=100; person1.age = 50; person2.salary=80; person2.age = 40; person3.salary = 100; person3.age=40; pq.push/person1/; pq.push/person2/; pq.push/person3/; while/!pq.empty/// { people r = pq.top//; pq.pop//; cout<<r.salary<<" "<<r.age<<endl;="" &="" :="" [code]struct="" age="" age,salary;="" bool="" code]="" const="" if="" int="" operator<="" p="" people="" return="" salary="p.salary/" {="" }[="" 可以获得相同的结果,并且当操作员过载时="">p.age; } else { return salary>p.salary; } }};
priority_queue<people> pq; people person1,person2,person3; person1.salary=100; person1.age = 50; person2.salary=80; person2.age = 40; person3.salary = 100; person3.age=40; pq.push/person1/; pq.push/person2/; pq.push/person3/; while/!pq.empty/// { people r = pq.top//; pq.pop//; cout<<r.salary<<" "<<r.age<<endl;="" <="" code]="" div="" }[=""> <div class="answer_text"> 在 C++11 您还可以为方便起见创建假名: [code]template<class t=""> using min_heap = priority_queue<t, std::vector<t="">, std::greater<t>>;
min_heap<int> my_heap;
#include<bits stdc++.h=""> using namespace std; int main//{ priority_queue<int> pq; int i; // push the negative of each element in priority_queue, so the largest number will become the smallest number for /int i = 0; i < 5; i++/ { cin>>j; pq.push/j*-1/; } for /int i = 0; i < 5; i++/ { cout<<!---1/*pq.top//<<endl; pq.pop//; } }
冰洋
template<class t=""> using min_heap = priority_queue<t, std::vector<t="">, std::greater<t>>;
卫东
要回复问题请先登录或注册
8 个回复
知食
赞同来自:
作为比较功能:
</int></int,>
三叔
赞同来自:
它将输出 1, 3, 5, 8 分别。
http://www.technical-recipes.c ... in-c/
使用优先级队列的一些示例
http://www.cs.princeton.edu/~r ... e.txt
.
</int,vector<int></queue></iostream>
石油百科
赞同来自:
- 这是一个比较器。 安装它使用
.
例如。
你会需要
为了
.
</functional></int></int,>
八刀丁二
赞同来自:
1. 使用
作为比较功能 :
3. 使用用户结构或类 :
在主要功能中 :
并使用它:
</int></t></t,></class></div>
<div class="answer_text">
解决这个问题的一种方法是给每个元素的负元素 priority_queue 这样最大的元素就变得最小。 操作过程中 pop 遵守每个项目的否定。
</div-->
<div class="answer_text">
基于上述答案,我创建了一个示例代码来创建优先级队列。
注意:它适用于编译器 C++11 和更高
[code]#include <iostream>
#include <vector>
#include <iomanip>
#include <queue>
using namespace std;
// template for prirority Q
template<class t=""> using min_heap = priority_queue<t, std::vector<t="">, std::greater<t>>;
template<class t=""> using max_heap = priority_queue<t, std::vector<t="">>;
const int RANGE = 1000;
vector<int> get_sample_data/int size/;
int main//{
int n;
cout << "Enter number of elements N = " ; cin >> n;
vector<int> dataset = get_sample_data/n/;
max_heap<int> max_pq;
min_heap<int> min_pq;
// Push data to Priority Queue
for/int i: dataset/{
max_pq.push/i/;
min_pq.push/i/;
}
while/!max_pq.empty// && !min_pq.empty///{
cout << setw/10/ << min_pq.top//<< " | " << max_pq.top// << endl;
min_pq.pop//;
max_pq.pop//;
}
}
vector<int> get_sample_data/int size/{
srand/time/NULL//;
vector<int> dataset;
for/int i=0; i<size; %range="" 33="" 33[="" 411="" 49="" 535="" ;="" <="" [code]enter="" code]="" dataset.push_back="" dataset;="" div="" elements="" i++="" n="4" number="" of="" rand="" return="" {="" |="" }="" }[="" 以上的输出代码="">
<div class="answer_text">
我们可以用几种方式做到这一点。
使用模板比较器的参数
[code]int main//
{
priority_queue<int, vector<int="">, greater<int> > pq;
pq.push/40/;
pq.push/320/;
pq.push/42/;
pq.push/65/;
pq.push/12/;
cout<<pq.top 0;="" <<endl;="" [code]struct="" bool="" code]="" comp="" int="" lhs="" lhs,="" operator="" return="" rhs="" {="" }[="" 使用使用特定的比较器类=""> rhs;
}
};
int main//
{
priority_queue<int, vector<int="">, comp> pq;
pq.push/40/;
pq.push/320/;
pq.push/42/;
pq.push/65/;
pq.push/12/;
cout<<pq.top 0;="" <="" <<endl;="" code]="" div="" return="" }[="">
</pq.top></int,></pq.top></int></int,></div></size;></int></int></int></int></int></int></t,></class></t></t,></class></queue></iomanip></vector></iostream></div></int></bits></div></r.salary<<"></people></r.salary<<"></people,vector<people></r<<"></int,vector<int></r<<></int></int,vector<int></bits>
冰洋
赞同来自:
并使用它:
</int></t></t,></class>
三叔
赞同来自:
</div-->
<div class="answer_text">
基于上述答案,我创建了一个示例代码来创建优先级队列。
注意:它适用于编译器 C++11 和更高
[code]#include <iostream>
#include <vector>
#include <iomanip>
#include <queue>
using namespace std;
// template for prirority Q
template<class t=""> using min_heap = priority_queue<t, std::vector<t="">, std::greater<t>>;
template<class t=""> using max_heap = priority_queue<t, std::vector<t="">>;
const int RANGE = 1000;
vector<int> get_sample_data/int size/;
int main//{
int n;
cout << "Enter number of elements N = " ; cin >> n;
vector<int> dataset = get_sample_data/n/;
max_heap<int> max_pq;
min_heap<int> min_pq;
// Push data to Priority Queue
for/int i: dataset/{
max_pq.push/i/;
min_pq.push/i/;
}
while/!max_pq.empty// && !min_pq.empty///{
cout << setw/10/ << min_pq.top//<< " | " << max_pq.top// << endl;
min_pq.pop//;
max_pq.pop//;
}
}
vector<int> get_sample_data/int size/{
srand/time/NULL//;
vector<int> dataset;
for/int i=0; i<size; %range="" 33="" 33[="" 411="" 49="" 535="" ;="" <="" [code]enter="" code]="" dataset.push_back="" dataset;="" div="" elements="" i++="" n="4" number="" of="" rand="" return="" {="" |="" }="" }[="" 以上的输出代码="">
<div class="answer_text">
我们可以用几种方式做到这一点。
使用模板比较器的参数
[code]int main//
{
priority_queue<int, vector<int="">, greater<int> > pq;
pq.push/40/;
pq.push/320/;
pq.push/42/;
pq.push/65/;
pq.push/12/;
cout<<pq.top 0;="" <<endl;="" [code]struct="" bool="" code]="" comp="" int="" lhs="" lhs,="" operator="" return="" rhs="" {="" }[="" 使用使用特定的比较器类=""> rhs;
}
};
int main//
{
priority_queue<int, vector<int="">, comp> pq;
pq.push/40/;
pq.push/320/;
pq.push/42/;
pq.push/65/;
pq.push/12/;
cout<<pq.top 0;="" <="" <<endl;="" code]="" div="" return="" }[="">
</pq.top></int,></pq.top></int></int,></div></size;></int></int></int></int></int></int></t,></class></t></t,></class></queue></iomanip></vector></iostream></div></int></bits>
冰洋
赞同来自:
注意:它适用于编译器 C++11 和更高
[code]#include <iostream>
#include <vector>
#include <iomanip>
#include <queue>
using namespace std;
// template for prirority Q
template<class t=""> using min_heap = priority_queue<t, std::vector<t="">, std::greater<t>>;
template<class t=""> using max_heap = priority_queue<t, std::vector<t="">>;
const int RANGE = 1000;
vector<int> get_sample_data/int size/;
int main//{
int n;
cout << "Enter number of elements N = " ; cin >> n;
vector<int> dataset = get_sample_data/n/;
max_heap<int> max_pq;
min_heap<int> min_pq;
// Push data to Priority Queue
for/int i: dataset/{
max_pq.push/i/;
min_pq.push/i/;
}
while/!max_pq.empty// && !min_pq.empty///{
cout << setw/10/ << min_pq.top//<< " | " << max_pq.top// << endl;
min_pq.pop//;
max_pq.pop//;
}
}
vector<int> get_sample_data/int size/{
srand/time/NULL//;
vector<int> dataset;
for/int i=0; i<size; %range="" 33="" 33[="" 411="" 49="" 535="" ;="" <="" [code]enter="" code]="" dataset.push_back="" dataset;="" div="" elements="" i++="" n="4" number="" of="" rand="" return="" {="" |="" }="" }[="" 以上的输出代码="">
<div class="answer_text">
我们可以用几种方式做到这一点。
使用模板比较器的参数
[code]int main//
{
priority_queue<int, vector<int="">, greater<int> > pq;
pq.push/40/;
pq.push/320/;
pq.push/42/;
pq.push/65/;
pq.push/12/;
cout<<pq.top 0;="" <<endl;="" [code]struct="" bool="" code]="" comp="" int="" lhs="" lhs,="" operator="" return="" rhs="" {="" }[="" 使用使用特定的比较器类=""> rhs;
}
};
int main//
{
priority_queue<int, vector<int="">, comp> pq;
pq.push/40/;
pq.push/320/;
pq.push/42/;
pq.push/65/;
pq.push/12/;
cout<<pq.top 0;="" <="" <<endl;="" code]="" div="" return="" }[="">
</pq.top></int,></pq.top></int></int,></div></size;></int></int></int></int></int></int></t,></class></t></t,></class></queue></iomanip></vector></iostream>
卫东
赞同来自:
使用模板比较器的参数
[code]int main//
{
priority_queue<int, vector<int="">, greater<int> > pq;
pq.push/40/;
pq.push/320/;
pq.push/42/;
pq.push/65/;
pq.push/12/;
cout<<pq.top 0;="" <<endl;="" [code]struct="" bool="" code]="" comp="" int="" lhs="" lhs,="" operator="" return="" rhs="" {="" }[="" 使用使用特定的比较器类=""> rhs;
}
};
int main//
{
priority_queue<int, vector<int="">, comp> pq;
pq.push/40/;
pq.push/320/;
pq.push/42/;
pq.push/65/;
pq.push/12/;
cout<<pq.top 0;="" <="" <<endl;="" code]="" div="" return="" }[="">
</pq.top></int,></pq.top></int></int,>