STL的神器,优先队列

本文详细介绍了C++ STL中优先队列的实现与使用方法,包括语法、默认设置、自定义比较类及不同用法示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在STL里有这个priority_queue,实现优先队列的结构。在优先队列中,优先级高的元素先出队列。
现在在这里说说用法吧

先看看语法:

Syntax:

In their implementation in the C++ Standard Template Library,priority queues take three template parameters:1
2 template < class T, class Container =vector,
          class Compare = less > class priority_queue;


Where the template parameters have the following meanings:
T: Type of the elements.
Container: Type of the underlying container object used to storeand access the elements.
Compare: Comparison class: A class such that the expressioncomp(a,b), where comp is an object of this class and a and b areelements of the container, returns true if a is to be placedearlier than b in a strict weak ordering operation. This can eitherbe a class implementing a function call operator or a pointer to afunction. This defaults to less, which returns the same as applyingthe less-than operator (a
The priority_queue object uses this expression when an element isinserted or removed from it (using push or pop, respectively) togrant that the element popped is always the greater in the priorityqueue.

可以自定义一个比较类,Compare

其实就三种用法

第一种,直接使用默认的。

它的模板声明带有三个参数,priority_queue
Type 为数据类型, Container 为保存数据的容器,Functional 为元素比较方式。
Container 必须是用数组实现的容器,比如 vector, deque 但不能用 list.
STL里面默认用的是 vector. 比较方式默认用 operator< ,所以如果你把后面俩个
参数缺省的话,优先队列就是大顶堆,队头元素最大。
看例子


priority_queue qi;

    inta[len] = {3,5,9,6,2};

   priority_queue qi;
    for(i = 0; i< len; i++)
       qi.push(a[i]);

    for(i =0; i < len; i++)
    {
       cout<<qi.top()<<"";
       qi.pop();
    }

通过<操作符可知在整数中元素大的优先级高。
故例子中输出结果为:9 6 5 3 2

第二种:

第二种方法:
在示例1中,如果我们要把元素从小到大输出怎么办呢?
这时我们可以传入一个比较函数,使用functional.h函数对象作为比较函数。

如果要用到小顶堆,则一般要把模板的三个参数都带进去。
STL里面定义了一个仿函数greater<>,对于基本类型可以用这个仿函数声明小顶堆
priority_queue, greater >qi2;

对于自定义类型,则必须自己重载 operator< 或者自己写仿函数

#include
#include

using namespace std;

struct Node{
    int x,y;
    Node( int a=0, int b= 0 ):
       x(a), y(b) {}
};

bool operator<( Node a, Node b ){
    if( a.x==b.x ) return a.y> b.y;
    returna.x> b.x;
}

int main(){
   priority_queue q;
   
    for( int i=0; i< 10; ++i )
    q.push(Node( rand(), rand() ) );
   
    while(!q.empty() ){
       cout << q.top().x<< ' '<< q.top().y<< endl;
       q.pop();
    }
   
   getchar();
    return0;
}

或者这样定义也是能达到效果的:

struct Node{
    int x,y;
    Node( int a=0, int b= 0 ):
       x(a), y(b) {}

    friendoperator<( Node a, Node b ){
    if( a.x==b.x ) return a.y> b.y;
    returna.x> b.x;

    }
};

自定义类型重载 operator< 后,声明对象时就可以只带一个模板参数。
但此时不能像基本类型这样声明
priority_queue, greater >;
原因是 greater 没有定义,如果想用这种方法定义
则可以按如下方式

例子:

#include
#include

using namespace std;

struct Node{
    int x,y;
    Node( int a=0, int b= 0 ):
       x(a), y(b) {}
};

struct cmp{
    booloperator() ( Node a, Node b ){
       if( a.x== b.x ) return a.y> b.y;
       
       return a.x> b.x; }
};

int main(){
   priority_queue, cmp> q;
   
    for( int i=0; i< 10; ++i )
    q.push(Node( rand(), rand() ) );
   
    while(!q.empty() ){
       cout << q.top().x<< ' '<< q.top().y<< endl;
       q.pop();
    }
   
   getchar();
    return0;
}

还有一点要注意的是priority_queue中的三个参数,后两个可以省去,因为有默认参数,不过如果,有第三个参数的话,必定要写第二个参数。

转自:http://hi.baidu.com/einstein17/item/dad993a7bff828258819d3c8


一个简单的测试:#include<iostream>

#include<queue>

using namespace std;

const int len = 5;

int main()

{

priority_queue<int>qi;

int a[len] = {3,5,9,6,2};

for(int i = 0 ; i<len ; i++)

qi.push(a[i]);

while(!q.empty())

{

cout<<qi.top()<<"";

qi.pop();

}

cout<<endl;

}

内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具与自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、CO和SO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性和便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算和结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征和技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析和模拟,揭示了生物质炉具在实际应用中的优点和挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向和政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值