C++ 中优先级队列的结构排序条件

Struct ordering criteria of a priority queue in C++

本文关键字:结构 排序 条件 队列 优先级 C++      更新时间:2023-10-16

我想知道是否可以使用相同的结构但具有不同排序标准的两种不同的priority_queues(stl(进行排序。想象一下:

struct stuff {
int a, b;
}
priority_queue<stuff> a;
priority_queue<stuff> b;

请注意,这不是C++的功能代码,只是伪代码。然后,我想知道 a 和 b 的顺序是否有可能不同,一个在队列开始时有最大的 stuff.a,另一个在队列开始时有最大的 stuff.b。

谢谢!

马克

std::p riority_queue 上的文档有一个如何使用 lambda 的示例,因此只需创建其中的 2 个:

struct stuff {
int a, b;
};
void foo() 
{
auto cmpBiggerA = []( const stuff &s1, const stuff &s2 ) { return s1.a > s2.a; };
auto cmpBiggerB = []( const stuff &s1, const stuff &s2 ) { return s1.b > s2.b; };
priority_queue<stuff,std::vector<stuff>,decltype(cmpBiggerA)> a( cmpBiggerA );
priority_queue<stuff,std::vector<stuff>,decltype(cmpBiggerB)> b( cmpBiggerB );
}