priority_queue的用法

本文介绍了priority_queue本质是堆,其头文件为#include<queue>。还说明了priority_queue模板申明带3个参数,保存数据的容器须用数组实现,默认用vector。比较方式默认用operator<,缺省后2个参数时为大顶堆,队头元素最大,还提及了pair的比较函数。

priority_queue本质是一个堆。

1. 头文件是#include<queue>

2. 关于priority_queue中元素的比较

  模板申明带3个参数:priority_queue<Type, Container, Functional>,其中Type 为数据类型,Container为保存数据的容器,Functional 为元素比较方式。

  Container必须是用数组实现的容器,比如vector,deque等等,但不能用 list。STL里面默认用的是vector。

2.1 比较方式默认用operator<,所以如果把后面2个参数缺省的话,优先队列就是大顶堆(降序),队头元素最大。特别注意pair的比较函数

  以下代码返回一个降序输出:

#include <iostream>
#include <queue>
#include<vector>
using namespace std;
int main(){
    //下面两种优先队列的定义是等价的
    priority_queue<int> q;
   // priority_queue<int,vector<int>,less<int> >q;//后面有一个空格

    for( int i= 0; i< 10; ++i )
         q.push(i);
    //q.size()返回优先队列的大小
    while( !q.empty() ){
        cout<<q.top()<<endl;
        q.pop();
    }
    return 0;
}
 void test1(){
        //默认情况下,数值大的在队首位置(降序)
        priority_queue<int> q;
        for(int i  = 0;i <= 10;i ++)
            q.push(i);
        while(!q.empty()){
            cout<<q.top()<<" ";
            q.pop();
        }
        cout<<endl;

        //greater<int>表示数值小的优先级越大
        priority_queue<int,vector<int>,greater<int> > Q;
        for(int i  = 0;i <= 10;i ++)
            Q.push(i);
        while(!Q.empty()){
            cout<<Q.top()<<" ";
            Q.pop();
        }

    }

 

C++中的`priority_queue`是一种优先队列,其默认实现是最大堆。它允许以对数时间复杂度插入元素,并且每次操作后队列顶部始终是当前所有元素中优先级最高的一个。对于基本类型,默认的排序准则是通过小于运算符(`operator <`)来定义降序排列,而对于自定义类型(如结构体),需要显式地提供比较逻辑。 ### 使用结构体进行自定义排序 当使用结构体作为`priority_queue`的元素时,需要为其定义排序规则。这可以通过以下两种方式之一完成: 1. **重载小于运算符 (`operator <`)** 在结构体内部重载`<`运算符,这样可以直接在默认模板参数下使用该结构体。例如: ```cpp struct Person { int age; bool operator<(const Person& other) const { return age < other.age; // 最大堆,年龄大的排前面 } }; std::priority_queue<Person> pq; ``` 2. **自定义比较函数对象** 如果希望使用不同的排序准则或者不便于修改结构体定义,则可以传递一个比较函数或函数对象给`priority_queue`的模板参数列表。例如: ```cpp struct Person { int age; }; struct ComparePerson { bool operator()(const Person& a, const Person& b) { return a.age < b.age; // 最大堆 } }; std::priority_queue<Person, std::vector<Person>, ComparePerson> pq; ``` ### 示例代码 下面是一个完整的示例,展示如何使用结构体与自定义比较器来创建一个基于年龄排序的优先队列: ```cpp #include <iostream> #include <queue> struct Person { int age; }; // 自定义比较器 struct ComparePerson { bool operator()(const Person& a, const Person& b) { return a.age < b.age; // 年龄大的优先 } }; int main() { std::priority_queue<Person, std::vector<Person>, ComparePerson> pq; // 插入数据 pq.push({30}); pq.push({25}); pq.push({40}); // 输出并移除队列顶部元素 while (!pq.empty()) { std::cout << pq.top().age << " "; pq.pop(); } return 0; } ``` 在这个例子中,输出将会是按年龄从高到低排序的结果:`40 30 25`。 ### 注意事项 - `priority_queue`底层通常采用`vector`作为容器,并利用堆算法维护堆属性。 - 默认情况下,`priority_queue`构造的是最大堆;若要构造最小堆,可通过指定第三个模板参数为`std::greater<>`[^4]。 - 当处理复杂类型的元素时,确保比较逻辑正确无误,以免导致错误的排序结果。 以上方法适用于大多数情况下的需求,能够灵活地适应不同场景下的自定义排序要求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值