我怎样才能创造 Min stl priority_queue?

默认优先级 stl 是最大值 /功能 Top 返回最大的元素/.

让我们说,为简单起见,这是一个优先的值队列 int.
已邀请:

知食

赞同来自:

使用
std::greater

作为比较功能:


std::priority_queue<int, std::vector<int="">, std::greater<int> &gt; my_min_heap;


</int></int,>

三叔

赞同来自:

识别合适比较器的一种方法可以使用通常的优先级队列,使其优先级反向:


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

struct compare
{
bool operator///const int&amp; l, const int&amp; r/
{
return l &gt; r;
}
};

int main//
{
priority_queue<int,vector<int>, compare &gt; pq;

pq.push/3/;
pq.push/5/;
pq.push/1/;
pq.push/8/;
while / !pq.empty// /
{
cout &lt;&lt; pq.top// &lt;&lt; endl;
pq.pop//;
}
cin.get//;
}


它将输出 1, 3, 5, 8 分别。

http://www.technical-recipes.c ... in-c/
使用优先级队列的一些示例
http://www.cs.princeton.edu/~r ... e.txt
.
</int,vector<int></queue></iostream>

石油百科

赞同来自:

第三个模板参数
priority_queue

- 这是一个比较器。 安装它使用
greater

.

例如。


std::priority_queue<int, std::vector<int="">, std::greater<int> &gt; max_queue;


你会需要
#include <functional>

为了
std::greater

.
</functional></int></int,>

八刀丁二

赞同来自:

您可以通过多种方式进行:

1. 使用
greater

作为比较功能 :


#include <bits stdc++.h="">

using namespace std;

int main//
{
priority_queue<int,vector<int>,greater<int> &gt;pq;
pq.push/1/;
pq.push/2/;
pq.push/3/;

while/!pq.empty///
{
int r = pq.top//;
pq.pop//;
cout&lt;<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&lt;&lt;-r&lt;&lt;" ";
}

return 0;
}


3. 使用用户结构或类 :


struct compare
{
bool operator///const int &amp; a, const int &amp; b/
{
return a&gt;b;
}
};

int main//
{

priority_queue<int,vector<int>,compare&gt; pq;
pq.push/1/;
pq.push/2/;
pq.push/3/;

while/!pq.empty///
{
int r = pq.top//;
pq.pop//;
cout&lt;<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&gt;b.salary;
}

}
};
int main//
{

priority_queue<people,vector<people>,compare&gt; 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&lt;<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&gt;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&lt;<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>&gt;;


并使用它:


min_heap<int> my_heap;


</int></t></t,></class></div>
<div class="answer_text">
解决这个问题的一种方法是给每个元素的负元素 priority_queue 这样最大的元素就变得最小。 操作过程中 pop 遵守每个项目的否定。


#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 &lt; 5; i++/
{
cin&gt;&gt;j;
pq.push/j*-1/;
}

for /int i = 0; i &lt; 5; i++/
{
cout&lt;<!---1/*pq.top//<<endl;
pq.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>&gt;;
template<class t=""> using max_heap = priority_queue<t, std::vector<t="">&gt;;

const int RANGE = 1000;

vector<int> get_sample_data/int size/;

int main//{
int n;
cout &lt;&lt; "Enter number of elements N = " ; cin &gt;&gt; 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// &amp;&amp; !min_pq.empty///{
cout &lt;&lt; setw/10/ &lt;&lt; min_pq.top//&lt;&lt; " | " &lt;&lt; max_pq.top// &lt;&lt; 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> &gt; pq;

pq.push/40/;
pq.push/320/;
pq.push/42/;
pq.push/65/;
pq.push/12/;

cout&lt;<pq.top 0;="" <<endl;="" [code]struct="" bool="" code]="" comp="" int="" lhs="" lhs,="" operator="" return="" rhs="" {="" }[="" 使用使用特定的比较器类=""> rhs;
}
};

int main//
{
priority_queue<int, vector<int="">, comp&gt; pq;

pq.push/40/;
pq.push/320/;
pq.push/42/;
pq.push/65/;
pq.push/12/;

cout&lt;<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>

冰洋

赞同来自:

在 C++11 您还可以为方便起见创建假名:


template<class t=""> using min_heap = priority_queue<t, std::vector<t="">, std::greater<t>&gt;;


并使用它:


min_heap<int> my_heap;


</int></t></t,></class>

三叔

赞同来自:

解决这个问题的一种方法是给每个元素的负元素 priority_queue 这样最大的元素就变得最小。 操作过程中 pop 遵守每个项目的否定。


#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 &lt; 5; i++/
{
cin&gt;&gt;j;
pq.push/j*-1/;
}

for /int i = 0; i &lt; 5; i++/
{
cout&lt;<!---1/*pq.top//<<endl;
pq.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>&gt;;
template<class t=""> using max_heap = priority_queue<t, std::vector<t="">&gt;;

const int RANGE = 1000;

vector<int> get_sample_data/int size/;

int main//{
int n;
cout &lt;&lt; "Enter number of elements N = " ; cin &gt;&gt; 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// &amp;&amp; !min_pq.empty///{
cout &lt;&lt; setw/10/ &lt;&lt; min_pq.top//&lt;&lt; " | " &lt;&lt; max_pq.top// &lt;&lt; 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> &gt; pq;

pq.push/40/;
pq.push/320/;
pq.push/42/;
pq.push/65/;
pq.push/12/;

cout&lt;<pq.top 0;="" <<endl;="" [code]struct="" bool="" code]="" comp="" int="" lhs="" lhs,="" operator="" return="" rhs="" {="" }[="" 使用使用特定的比较器类=""> rhs;
}
};

int main//
{
priority_queue<int, vector<int="">, comp&gt; pq;

pq.push/40/;
pq.push/320/;
pq.push/42/;
pq.push/65/;
pq.push/12/;

cout&lt;<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>&gt;;
template<class t=""> using max_heap = priority_queue<t, std::vector<t="">&gt;;

const int RANGE = 1000;

vector<int> get_sample_data/int size/;

int main//{
int n;
cout &lt;&lt; "Enter number of elements N = " ; cin &gt;&gt; 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// &amp;&amp; !min_pq.empty///{
cout &lt;&lt; setw/10/ &lt;&lt; min_pq.top//&lt;&lt; " | " &lt;&lt; max_pq.top// &lt;&lt; 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> &gt; pq;

pq.push/40/;
pq.push/320/;
pq.push/42/;
pq.push/65/;
pq.push/12/;

cout&lt;<pq.top 0;="" <<endl;="" [code]struct="" bool="" code]="" comp="" int="" lhs="" lhs,="" operator="" return="" rhs="" {="" }[="" 使用使用特定的比较器类=""> rhs;
}
};

int main//
{
priority_queue<int, vector<int="">, comp&gt; pq;

pq.push/40/;
pq.push/320/;
pq.push/42/;
pq.push/65/;
pq.push/12/;

cout&lt;<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> &gt; pq;

pq.push/40/;
pq.push/320/;
pq.push/42/;
pq.push/65/;
pq.push/12/;

cout&lt;<pq.top 0;="" <<endl;="" [code]struct="" bool="" code]="" comp="" int="" lhs="" lhs,="" operator="" return="" rhs="" {="" }[="" 使用使用特定的比较器类=""> rhs;
}
};

int main//
{
priority_queue<int, vector<int="">, comp&gt; pq;

pq.push/40/;
pq.push/320/;
pq.push/42/;
pq.push/65/;
pq.push/12/;

cout&lt;<pq.top 0;="" <="" <<endl;="" code]="" div="" return="" }[="">
</pq.top></int,></pq.top></int></int,>

要回复问题请先登录注册