c++优先级队列priority_queue用法

本文介绍了C++中的优先级队列priority_queue的使用,包括小根堆和大根堆的初始化,以及如何利用数组和pair、tuple进行初始化。对于pair和tuple,还详细说明了内置的比较器在不同情况下的应用。

priority_queue初始化

小根堆初始化

priority_queue<int, vector, greater> q;

大根堆初始化

priority_queue<int, vector, less> q;
如果使用大根堆(默认为从小到到排序) 且不用自己定义的数据类型,可以直接:
priority_queue q;

数组直接初始化小根堆

vector<pair<int, int>> power;
priority_queue q(grreter<pair<int, int>>(), move(power));
q.pop();

对于pair, tuple这样的数据类型,也定义好了比较器。

1.pair:

//大根堆:
//priority_queue<pair<int, int>> pq0;
//小根堆:按照pair的first排序,再按照second排序
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq1;

2.tuple:

//默认是使用大根堆
priority_queue<tuple<int,int,int>> tp0;
//小根堆,按照tuple的0元素排,再按照1元素排,最后按2元素排
priority_queue<tuple<int,int,int>,vector<tuple<int,int,int>>,greater<tuple<int,int,int>>> tp1;
//大根堆
priority_queue<tuple<int,int,int>,vector<tuple<int,int,int>>,less<tuple<int,int,int>>> tp2;

C++中使用`priority_queue`包含自定义结构体时,需要定义结构体的比较规则。由于`priority_queue`默认使用元素的比较函数来确定元素的优先级顺序,因此对于自定义结构体,需要手动提供比较函数。下面详细介绍两种常用的实现方法: ### 使用仿函数(Functor) 仿函数是一个重载了`()`运算符的类或结构体,它的对象可以像函数一样调用。在`priority_queue`中使用仿函数可以定义自定义结构体的比较规则。 以下是一个示例代码: ```cpp #include <iostream> #include <queue> // 定义自定义结构体 struct Person { std::string name; int age; Person(const std::string& n, int a) : name(n), age(a) {} }; // 定义仿函数,用于比较 Person 结构体 struct Compare { bool operator()(const Person& p1, const Person& p2) { return p1.age < p2.age; // 按照年龄从大到小排序 } }; int main() { // 定义优先队列,使用自定义结构体和仿函数 std::priority_queue<Person, std::vector<Person>, Compare> pq; // 向优先队列中添加元素 pq.push(Person("Alice", 25)); pq.push(Person("Bob", 30)); pq.push(Person("Charlie", 20)); // 输出优先队列中的元素 while (!pq.empty()) { const Person& p = pq.top(); std::cout << p.name << " : " << p.age << std::endl; pq.pop(); } return 0; } ``` 在上述代码中,首先定义了一个自定义结构体`Person`,包含`name`和`age`两个成员变量。然后,定义了一个仿函数`Compare`,重载了`()`运算符,用于比较两个`Person`对象的`age`成员变量。在`main`函数中,创建了一个`priority_queue`对象`pq`,并指定了自定义结构体`Person`、底层容器`std::vector<Person>`和仿函数`Compare`。最后,向优先队列中添加元素,并按照年龄从大到小的顺序输出元素。 ### 重载结构体的`>`运算符 如果不想使用仿函数,可以直接在自定义结构体中重载`>`运算符。这样,`priority_queue`就可以使用默认的比较方式来确定元素的优先级顺序。 以下是一个示例代码: ```cpp #include <iostream> #include <queue> // 定义自定义结构体 struct Point { int x; int y; Point(int _x, int _y) : x(_x), y(_y) {} // 重载 > 运算符,用于比较 Point 结构体 bool operator>(const Point& other) const { return x * x + y * y > other.x * other.x + other.y * other.y; // 按照距离原点的距离从小到大排序 } }; int main() { // 定义优先队列,使用自定义结构体 std::priority_queue<Point, std::vector<Point>, std::greater<Point>> pq; // 向优先队列中添加元素 pq.push(Point(3, 4)); pq.push(Point(1, 2)); pq.push(Point(5, 6)); // 输出优先队列中的元素 while (!pq.empty()) { const Point& p = pq.top(); std::cout << "(" << p.x << ", " << p.y << ")" << std::endl; pq.pop(); } return 0; } ``` 在上述代码中,首先定义了一个自定义结构体`Point`,包含`x`和`y`两个成员变量。然后,在结构体中重载了`>`运算符,用于比较两个`Point`对象到原点的距离。在`main`函数中,创建了一个`priority_queue`对象`pq`,并指定了自定义结构体`Point`、底层容器`std::vector<Point>`和比较函数`std::greater<Point>`。最后,向优先队列中添加元素,并按照距离原点的距离从小到大的顺序输出元素。 ### 总结 通过使用仿函数或重载`>`运算符,可以在C++的`priority_queue`中使用自定义结构体,并定义自定义的比较规则。这样,就可以根据自己的需求对自定义结构体进行排序和优先级管理。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值